[Dojo-interest] JSON Schema: optional array property with default

Michael Schuerig michael at schuerig.de
Fri Apr 3 09:46:10 EDT 2009


On Friday 03 April 2009, Kris Zyp wrote:
> Michael Schuerig wrote:

> >     awards: {
> >       type: [],
> >       optional: true,
> >       default: []
> >     }
[...]
> > with the intention that awards is an optional property of array
> > type. The default value of [] should be used on newly created items
> > or on fetched items lacking this property. So I thought, at least.
> > Apparently, either I'm misunderstanding how the schema is supposed
> > to work, or it isn't implemented in that way yet.
>
> Using a default value of [] is usually undesirable, since you are
> creating a single array object that will be the default value for all
> instances. All instances will share a reference to the same default
> array, and mutating that array will be mutating the same array shared
> by all instances.
> Also, the type should be "array".

I see. Giving a clone of the default value to each instance that needs 
it might be a better choice at the expense of a larger memory 
consumption.

> I think you want:
> ...
> properties: {
>   awards: {
>      type:"array",
>      optional: true,
>      items: awardSchema
>   }

Yes, thanks.

Michael

-- 
Michael Schuerig
mailto:michael at schuerig.de
http://www.schuerig.de/michael/


More information about the Dojo-interest mailing list