From 9e57509e25a7d750e7113b1d41f1d3af732ff934 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Tue, 27 Oct 2020 19:40:19 -0400 Subject: Json schema default value improvement (#964) * Ensure value has property before using its value * Add tests --- test/test-schema.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'test') diff --git a/test/test-schema.js b/test/test-schema.js index a79650d6..20f72518 100644 --- a/test/test-schema.js +++ b/test/test-schema.js @@ -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'} + ] + ] } ]; -- cgit v1.2.3