Arbitrarily nesting some attributes in rabl

Hi,
I’m designing a new API for my project, and I want to return objects
that have nested children as json. For that purpose i’ve decided to use
RABL.

I want the client side to be able to understand whether the object is
valid, and if not which fields are missing in order to save it
correctly.

The design I thought of should include some fields as optional, under an
optional hash, and the rest are required. The required fields should
appear right under the root of the json. So the output I try to describe
should look something like this:

{
“name”: “John”,
“last_name”: “Doe”,
“optional”: {
“address”: “Beverly Hills 90210”,
“phones”:[{“number”:“123456”,“name”:“work”},
{“number”:“654321”,“name”:“mobile”}]
}
}

The above output example describes the required fields name and last
name, and the not required address and phones (which is associated in a
belongs_to-has_many relationship to the object). name, last_name and
address are User’s DB fields.

Playing with RABL I didn’t manage so far to create this kind of
structure.

Any suggestions? I’m looking for a DRY way to implement this for all my
models.

Thanks