Json schema default value improvement (#964)
* Ensure value has property before using its value * Add tests
This commit is contained in:
parent
75734de7eb
commit
9e57509e25
@ -637,7 +637,8 @@ class JsonSchemaValidator {
|
||||
if (propertySchema === null) { continue; }
|
||||
info.valuePush(property, value);
|
||||
info.schemaPush(property, propertySchema);
|
||||
value[property] = this._getValidValueOrDefault(propertySchema, value[property], info);
|
||||
const hasValue = Object.prototype.hasOwnProperty.call(value, property);
|
||||
value[property] = this._getValidValueOrDefault(propertySchema, hasValue ? value[property] : void 0, info);
|
||||
info.schemaPop();
|
||||
info.valuePop();
|
||||
}
|
||||
|
@ -540,6 +540,60 @@ function testGetValidValueOrDefault1() {
|
||||
{test: 'value', test2: 2, test3: 10}
|
||||
]
|
||||
]
|
||||
},
|
||||
|
||||
// Test value defaulting where hasOwnProperty is false
|
||||
{
|
||||
schema: {
|
||||
type: 'object',
|
||||
required: ['test'],
|
||||
properties: {
|
||||
test: {
|
||||
type: 'string',
|
||||
default: 'default'
|
||||
}
|
||||
}
|
||||
},
|
||||
inputs: [
|
||||
[
|
||||
{},
|
||||
{test: 'default'}
|
||||
],
|
||||
[
|
||||
{test: 'value'},
|
||||
{test: 'value'}
|
||||
],
|
||||
[
|
||||
Object.create({test: 'value'}),
|
||||
{test: 'default'}
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
schema: {
|
||||
type: 'object',
|
||||
required: ['toString'],
|
||||
properties: {
|
||||
toString: {
|
||||
type: 'string',
|
||||
default: 'default'
|
||||
}
|
||||
}
|
||||
},
|
||||
inputs: [
|
||||
[
|
||||
{},
|
||||
{toString: 'default'}
|
||||
],
|
||||
[
|
||||
{toString: 'value'},
|
||||
{toString: 'value'}
|
||||
],
|
||||
[
|
||||
Object.create({toString: 'value'}),
|
||||
{toString: 'default'}
|
||||
]
|
||||
]
|
||||
}
|
||||
];
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user