rjrodger/parambulator — pull request #17 by mirceaalexandru (2012-10-25 20:56:34+00:00)
Title: Implemented support for default$ rule. See #6

Implemented support for default$ rule. If the path to the default$ rule does not exists in the validated object then it will be created.

If a property from this path has defined the type$ this will be used following the rules:
- if type$ is 'array' then an array will be created and process continues
- if type$ is 'object' then an object is created and process continues
- if type$ has any other value the process will fail with default$ code as it cannot continue.

Examples:
{ d: {type$: 'array', __0: {default$:'arraytest0'}, __1: {default$:'arraytest1'}} }
will create
{ d: ['arraytest0', 'arraytest1']}

but

{ d: {type$: 'number', __0: {default$:'arraytest0'}, __1: {default$:'arraytest1'}} }
will generate error

If a property do not have the type$ defined then, by default an object will be defined.

Note: Maybe we should not accept this default behavior and report error if a property do not have a type$ defined.

Example:
{
.......
b: {
c: {default$:23, type$:'number'}
}
.......
}

will create

{
...
b: {c:23}
...
}
As the type of property 'b' is unknown, in current implementation an object will be created and added to the validated object. Maybe we should report an error for this.

After the default values are added to object this will be validated using normal validation rule processing. This means that if the default value do not match the other rules of that property then validation will report error.

Example:

{
...
a: {default$:'aaa', type$:number}
...
}

if validated object is
{
...
a:123
...
}
no error will be generated (because values 123 is a number), but if the validated object do not contain property 'a' in the correct path then it will generate a type$ error (it will be added value 'aaa' which is not a number and validation will fail)

Note: we could take in consideration that in case of default$ error maybe we need to revert all changes on the validated object as this will be in an inconsistent state.