Add support for conditionals

This commit is contained in:
toasted-nutbread 2020-01-26 11:13:13 -05:00
parent 6595715f7c
commit 203216986e

View File

@ -154,6 +154,9 @@ class JsonSchemaProxyHandler {
let result = JsonSchemaProxyHandler.validateSingleSchema(value, schema);
if (result !== null) { return result; }
result = JsonSchemaProxyHandler.validateConditional(value, schema);
if (result !== null) { return result; }
result = JsonSchemaProxyHandler.validateAllOf(value, schema);
if (result !== null) { return result; }
@ -169,6 +172,25 @@ class JsonSchemaProxyHandler {
return null;
}
static validateConditional(value, schema) {
const ifCondition = schema.if;
if (!JsonSchemaProxyHandler.isObject(ifCondition)) { return null; }
const thenSchema = schema.then;
if (JsonSchemaProxyHandler.isObject(thenSchema)) {
const result = JsonSchemaProxyHandler.validate(value, thenSchema);
if (result !== null) { return `then conditional didn't match: ${result}`; }
}
const elseSchema = schema.else;
if (JsonSchemaProxyHandler.isObject(elseSchema)) {
const result = JsonSchemaProxyHandler.validate(value, thenSchema);
if (result !== null) { return `else conditional didn't match: ${result}`; }
}
return null;
}
static validateAllOf(value, schema) {
const subSchemas = schema.allOf;
if (!Array.isArray(subSchemas)) { return null; }