acornejo/jjv

useDefault fails validation for nullable properties

Opened this issue · 1 comments

SomeType: {
       ....
         nullableItem: {
                    type: ['null', 'object'],
                    properties: {
                        shouldChangeUrl: {
                            type: 'boolean',
                            default: false
                        }
                    }
                }
       ....
}

and then call validate with useDefault set to true

validator.validate('SomeType', { nullableItem: null }, {useDefault: true});

Expected: validation succeeded.
Actual: validation failed.

This code throws an exception because options.useDefault == true:

if (options.useDefault && hasProp && !malformed) {
                for (p in schema.properties)
                    if (schema.properties.hasOwnProperty(p) && !prop.hasOwnProperty(p) && schema.properties[p].hasOwnProperty('default'))
                        prop[p] = schema.properties[p]['default'];
            }

What I want to do is to say that nullableItem can be an object or null.
And if it's an object then default values should be applied.
Maybe there is another way to achieve this?