Skip to content
This repository has been archived by the owner on Feb 25, 2023. It is now read-only.

Json schema default value improvement #964

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ext/bg/js/json-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
54 changes: 54 additions & 0 deletions test/test-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
]
]
}
];

Expand Down