diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-01-26 10:57:52 -0500 |
---|---|---|
committer | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-02-02 10:12:01 -0500 |
commit | 6595715f7cdf1c4d0de743443b88eba05d7d6ae1 (patch) | |
tree | 6f5d919d6e7f07460aceee2061aa582a252187fb /ext/bg/js | |
parent | 0b474751b5fb994d402caf3d0515d95680684b60 (diff) |
Add support for allOf, anyOf, oneOf, and not
Diffstat (limited to 'ext/bg/js')
-rw-r--r-- | ext/bg/js/json-schema.js | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/ext/bg/js/json-schema.js b/ext/bg/js/json-schema.js index 2b7c9f27..8129b6d2 100644 --- a/ext/bg/js/json-schema.js +++ b/ext/bg/js/json-schema.js @@ -151,6 +151,70 @@ class JsonSchemaProxyHandler { } static validate(value, schema) { + let result = JsonSchemaProxyHandler.validateSingleSchema(value, schema); + if (result !== null) { return result; } + + result = JsonSchemaProxyHandler.validateAllOf(value, schema); + if (result !== null) { return result; } + + result = JsonSchemaProxyHandler.validateAnyOf(value, schema); + if (result !== null) { return result; } + + result = JsonSchemaProxyHandler.validateOneOf(value, schema); + if (result !== null) { return result; } + + result = JsonSchemaProxyHandler.validateNoneOf(value, schema); + if (result !== null) { return result; } + + return null; + } + + static validateAllOf(value, schema) { + const subSchemas = schema.allOf; + if (!Array.isArray(subSchemas)) { return null; } + + for (let i = 0; i < subSchemas.length; ++i) { + const result = JsonSchemaProxyHandler.validate(value, subSchemas[i]); + if (result !== null) { return `allOf[${i}] schema didn't match: ${result}`; } + } + return null; + } + + static validateAnyOf(value, schema) { + const subSchemas = schema.anyOf; + if (!Array.isArray(subSchemas)) { return null; } + + for (let i = 0; i < subSchemas.length; ++i) { + const result = JsonSchemaProxyHandler.validate(value, subSchemas[i]); + if (result === null) { return null; } + } + return '0 anyOf schemas matched'; + } + + static validateOneOf(value, schema) { + const subSchemas = schema.oneOf; + if (!Array.isArray(subSchemas)) { return null; } + + let count = 0; + for (let i = 0; i < subSchemas.length; ++i) { + const result = JsonSchemaProxyHandler.validate(value, subSchemas[i]); + if (result === null) { ++count; } + } + return count === 1 ? null : `${count} oneOf schemas matched`; + } + + static validateNoneOf(value, schema) { + const subSchemas = schema.not; + if (!Array.isArray(subSchemas)) { return null; } + + for (let i = 0; i < subSchemas.length; ++i) { + const result = JsonSchemaProxyHandler.validate(value, subSchemas[i]); + if (result === null) { return `not[${i}] schema matched`; } + } + return null; + } + + static validateSingleSchema(value, schema) { const type = JsonSchemaProxyHandler.getValueType(value); const schemaType = schema.type; if (!JsonSchemaProxyHandler.isValueTypeAny(value, type, schemaType)) { |