Json schema default value improvement (#964)

* Ensure value has property before using its value

* Add tests
This commit is contained in:
toasted-nutbread 2020-10-27 19:40:19 -04:00 committed by GitHub
parent 75734de7eb
commit 9e57509e25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 1 deletions

View File

@ -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();
}

View File

@ -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'}
]
]
}
];