From 486d44f7197a2dedc5ea487345e598c668d47638 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 9 Aug 2020 14:18:59 -0400 Subject: Json schema improvements (#722) * Add support for regex pattern testing * Add tests * Separate JsonSchemaProxyHandler statics into JsonSchemaValidator * Use this instead of JsonSchemaValidator * Make JsonSchemaValidator non-static * Use cache map for regex --- test/dictionary-validate.js | 1 + test/schema-validate.js | 1 + test/test-database.js | 1 + test/test-schema.js | 120 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 123 insertions(+) (limited to 'test') diff --git a/test/dictionary-validate.js b/test/dictionary-validate.js index 446de38d..716e8df4 100644 --- a/test/dictionary-validate.js +++ b/test/dictionary-validate.js @@ -23,6 +23,7 @@ const {VM} = require('./yomichan-vm'); const vm = new VM(); vm.execute([ 'mixed/js/core.js', + 'mixed/js/cache-map.js', 'bg/js/json-schema.js' ]); const JsonSchema = vm.get('JsonSchema'); diff --git a/test/schema-validate.js b/test/schema-validate.js index 4c01fa70..aa9450dd 100644 --- a/test/schema-validate.js +++ b/test/schema-validate.js @@ -21,6 +21,7 @@ const {VM} = require('./yomichan-vm'); const vm = new VM(); vm.execute([ 'mixed/js/core.js', + 'mixed/js/cache-map.js', 'bg/js/json-schema.js' ]); const JsonSchema = vm.get('JsonSchema'); diff --git a/test/test-database.js b/test/test-database.js index 6d0aae07..e8fb07b3 100644 --- a/test/test-database.js +++ b/test/test-database.js @@ -113,6 +113,7 @@ vm.context.window = vm.context; vm.execute([ 'mixed/js/core.js', + 'mixed/js/cache-map.js', 'bg/js/json-schema.js', 'bg/js/dictionary.js', 'bg/js/media-utility.js', diff --git a/test/test-schema.js b/test/test-schema.js index f0a99c3b..b976d81f 100644 --- a/test/test-schema.js +++ b/test/test-schema.js @@ -21,6 +21,7 @@ const {VM} = require('./yomichan-vm'); const vm = new VM(); vm.execute([ 'mixed/js/core.js', + 'mixed/js/cache-map.js', 'bg/js/json-schema.js' ]); const JsonSchema = vm.get('JsonSchema'); @@ -86,6 +87,124 @@ function testValidate1() { } } +function testValidate2() { + const data = [ + // String tests + { + schema: { + type: 'string' + }, + inputs: [ + {expected: false, value: null}, + {expected: false, value: void 0}, + {expected: false, value: 0}, + {expected: false, value: {}}, + {expected: false, value: []}, + {expected: true, value: ''} + ] + }, + { + schema: { + type: 'string', + minLength: 2 + }, + inputs: [ + {expected: false, value: ''}, + {expected: false, value: '1'}, + {expected: true, value: '12'}, + {expected: true, value: '123'} + ] + }, + { + schema: { + type: 'string', + maxLength: 2 + }, + inputs: [ + {expected: true, value: ''}, + {expected: true, value: '1'}, + {expected: true, value: '12'}, + {expected: false, value: '123'} + ] + }, + { + schema: { + type: 'string', + pattern: 'test' + }, + inputs: [ + {expected: false, value: ''}, + {expected: true, value: 'test'}, + {expected: false, value: 'TEST'}, + {expected: true, value: 'ABCtestDEF'}, + {expected: false, value: 'ABCTESTDEF'} + ] + }, + { + schema: { + type: 'string', + pattern: '^test$' + }, + inputs: [ + {expected: false, value: ''}, + {expected: true, value: 'test'}, + {expected: false, value: 'TEST'}, + {expected: false, value: 'ABCtestDEF'}, + {expected: false, value: 'ABCTESTDEF'} + ] + }, + { + schema: { + type: 'string', + pattern: '^test$', + patternFlags: 'i' + }, + inputs: [ + {expected: false, value: ''}, + {expected: true, value: 'test'}, + {expected: true, value: 'TEST'}, + {expected: false, value: 'ABCtestDEF'}, + {expected: false, value: 'ABCTESTDEF'} + ] + }, + { + schema: { + type: 'string', + pattern: '*' + }, + inputs: [ + {expected: false, value: ''} + ] + }, + { + schema: { + type: 'string', + pattern: '.', + patternFlags: '?' + }, + inputs: [ + {expected: false, value: ''} + ] + } + ]; + + const schemaValidate = (value, schema) => { + try { + JsonSchema.validate(value, schema); + return true; + } catch (e) { + return false; + } + }; + + for (const {schema, inputs} of data) { + for (const {expected, value} of inputs) { + const actual = schemaValidate(value, schema); + assert.strictEqual(actual, expected); + } + } +} + function testGetValidValueOrDefault1() { // Test value defaulting on objects with additionalProperties=false @@ -246,6 +365,7 @@ function testGetValidValueOrDefault3() { function main() { testValidate1(); + testValidate2(); testGetValidValueOrDefault1(); testGetValidValueOrDefault2(); testGetValidValueOrDefault3(); -- cgit v1.2.3