aboutsummaryrefslogtreecommitdiff
path: root/test/test-schema.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-08-09 14:18:59 -0400
committerGitHub <noreply@github.com>2020-08-09 14:18:59 -0400
commit486d44f7197a2dedc5ea487345e598c668d47638 (patch)
tree2620a0ed45ebebe786722739c8ce1816cc08d952 /test/test-schema.js
parentfbe575c577b9e6c8279aaa064cf62732c643240a (diff)
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
Diffstat (limited to 'test/test-schema.js')
-rw-r--r--test/test-schema.js120
1 files changed, 120 insertions, 0 deletions
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();