2019-11-28 17:19:15 +00:00
|
|
|
/*
|
2020-01-01 17:00:00 +00:00
|
|
|
* Copyright (C) 2019-2020 Alex Yatskov <alex@foosoft.net>
|
2019-11-28 17:19:15 +00:00
|
|
|
* Author: Alex Yatskov <alex@foosoft.net>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2020-01-01 17:00:31 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-11-28 17:19:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
class JsonSchemaProxyHandler {
|
|
|
|
constructor(schema) {
|
|
|
|
this._schema = schema;
|
|
|
|
}
|
|
|
|
|
|
|
|
getPrototypeOf(target) {
|
|
|
|
return Object.getPrototypeOf(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
setPrototypeOf() {
|
|
|
|
throw new Error('setPrototypeOf not supported');
|
|
|
|
}
|
|
|
|
|
|
|
|
isExtensible(target) {
|
|
|
|
return Object.isExtensible(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
preventExtensions(target) {
|
|
|
|
Object.preventExtensions(target);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
getOwnPropertyDescriptor(target, property) {
|
|
|
|
return Object.getOwnPropertyDescriptor(target, property);
|
|
|
|
}
|
|
|
|
|
|
|
|
defineProperty() {
|
|
|
|
throw new Error('defineProperty not supported');
|
|
|
|
}
|
|
|
|
|
|
|
|
has(target, property) {
|
|
|
|
return property in target;
|
|
|
|
}
|
|
|
|
|
|
|
|
get(target, property) {
|
|
|
|
if (typeof property === 'symbol') {
|
|
|
|
return target[property];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(target)) {
|
|
|
|
if (typeof property === 'string' && /^\d+$/.test(property)) {
|
|
|
|
property = parseInt(property, 10);
|
|
|
|
} else if (typeof property === 'string') {
|
|
|
|
return target[property];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-26 20:57:39 +00:00
|
|
|
const propertySchema = JsonSchemaProxyHandler.getPropertySchema(this._schema, property, target);
|
2019-11-28 17:19:15 +00:00
|
|
|
if (propertySchema === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const value = target[property];
|
|
|
|
return value !== null && typeof value === 'object' ? JsonSchema.createProxy(value, propertySchema) : value;
|
|
|
|
}
|
|
|
|
|
|
|
|
set(target, property, value) {
|
|
|
|
if (Array.isArray(target)) {
|
|
|
|
if (typeof property === 'string' && /^\d+$/.test(property)) {
|
|
|
|
property = parseInt(property, 10);
|
|
|
|
if (property > target.length) {
|
|
|
|
throw new Error('Array index out of range');
|
|
|
|
}
|
|
|
|
} else if (typeof property === 'string') {
|
|
|
|
target[property] = value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-26 20:57:39 +00:00
|
|
|
const propertySchema = JsonSchemaProxyHandler.getPropertySchema(this._schema, property, target);
|
2019-11-28 17:19:15 +00:00
|
|
|
if (propertySchema === null) {
|
|
|
|
throw new Error(`Property ${property} not supported`);
|
|
|
|
}
|
|
|
|
|
|
|
|
value = JsonSchema.isolate(value);
|
|
|
|
|
2020-02-02 15:47:30 +00:00
|
|
|
JsonSchemaProxyHandler.validate(value, propertySchema, new JsonSchemaTraversalInfo(value, propertySchema));
|
2019-11-28 17:19:15 +00:00
|
|
|
|
|
|
|
target[property] = value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteProperty(target, property) {
|
|
|
|
const required = this._schema.required;
|
|
|
|
if (Array.isArray(required) && required.includes(property)) {
|
|
|
|
throw new Error(`${property} cannot be deleted`);
|
|
|
|
}
|
|
|
|
return Reflect.deleteProperty(target, property);
|
|
|
|
}
|
|
|
|
|
|
|
|
ownKeys(target) {
|
|
|
|
return Reflect.ownKeys(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
apply() {
|
|
|
|
throw new Error('apply not supported');
|
|
|
|
}
|
|
|
|
|
|
|
|
construct() {
|
|
|
|
throw new Error('construct not supported');
|
|
|
|
}
|
|
|
|
|
2020-02-02 16:18:13 +00:00
|
|
|
static getPropertySchema(schema, property, value, path=null) {
|
2020-01-26 20:57:39 +00:00
|
|
|
const type = JsonSchemaProxyHandler.getSchemaOrValueType(schema, value);
|
2019-11-28 17:19:15 +00:00
|
|
|
switch (type) {
|
|
|
|
case 'object':
|
|
|
|
{
|
|
|
|
const properties = schema.properties;
|
2020-01-26 16:06:03 +00:00
|
|
|
if (JsonSchemaProxyHandler.isObject(properties)) {
|
2020-02-02 16:13:26 +00:00
|
|
|
const propertySchema = properties[property];
|
|
|
|
if (JsonSchemaProxyHandler.isObject(propertySchema)) {
|
2020-02-02 16:18:13 +00:00
|
|
|
if (path !== null) { path.push(['properties', properties], [property, propertySchema]); }
|
2020-02-02 16:13:26 +00:00
|
|
|
return propertySchema;
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const additionalProperties = schema.additionalProperties;
|
2020-01-26 20:45:31 +00:00
|
|
|
if (additionalProperties === false) {
|
|
|
|
return null;
|
2020-02-02 16:04:38 +00:00
|
|
|
} else if (JsonSchemaProxyHandler.isObject(additionalProperties)) {
|
2020-02-02 16:18:13 +00:00
|
|
|
if (path !== null) { path.push(['additionalProperties', additionalProperties]); }
|
2020-01-26 20:45:31 +00:00
|
|
|
return additionalProperties;
|
|
|
|
} else {
|
2020-02-02 16:18:13 +00:00
|
|
|
const result = JsonSchemaProxyHandler._unconstrainedSchema;
|
|
|
|
if (path !== null) { path.push([null, result]); }
|
|
|
|
return result;
|
2020-01-26 20:45:31 +00:00
|
|
|
}
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
case 'array':
|
|
|
|
{
|
|
|
|
const items = schema.items;
|
2020-01-26 17:34:27 +00:00
|
|
|
if (JsonSchemaProxyHandler.isObject(items)) {
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
if (Array.isArray(items)) {
|
|
|
|
if (property >= 0 && property < items.length) {
|
2020-02-02 16:13:26 +00:00
|
|
|
const propertySchema = items[property];
|
|
|
|
if (JsonSchemaProxyHandler.isObject(propertySchema)) {
|
2020-02-02 16:18:13 +00:00
|
|
|
if (path !== null) { path.push(['items', items], [property, propertySchema]); }
|
2020-02-02 16:13:26 +00:00
|
|
|
return propertySchema;
|
|
|
|
}
|
2020-01-26 17:34:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const additionalItems = schema.additionalItems;
|
|
|
|
if (additionalItems === false) {
|
|
|
|
return null;
|
|
|
|
} else if (JsonSchemaProxyHandler.isObject(additionalItems)) {
|
2020-02-02 16:18:13 +00:00
|
|
|
if (path !== null) { path.push(['additionalItems', additionalItems]); }
|
2020-01-26 17:34:27 +00:00
|
|
|
return additionalItems;
|
|
|
|
} else {
|
2020-02-02 16:18:13 +00:00
|
|
|
const result = JsonSchemaProxyHandler._unconstrainedSchema;
|
|
|
|
if (path !== null) { path.push([null, result]); }
|
|
|
|
return result;
|
2020-01-26 17:34:27 +00:00
|
|
|
}
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-26 20:57:39 +00:00
|
|
|
static getSchemaOrValueType(schema, value) {
|
|
|
|
const type = schema.type;
|
|
|
|
|
|
|
|
if (Array.isArray(type)) {
|
|
|
|
if (typeof value !== 'undefined') {
|
|
|
|
const valueType = JsonSchemaProxyHandler.getValueType(value);
|
|
|
|
if (type.indexOf(valueType) >= 0) {
|
|
|
|
return valueType;
|
|
|
|
}
|
|
|
|
}
|
2020-02-02 03:57:27 +00:00
|
|
|
return null;
|
2020-01-26 20:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof type === 'undefined') {
|
|
|
|
if (typeof value !== 'undefined') {
|
|
|
|
return JsonSchemaProxyHandler.getValueType(value);
|
|
|
|
}
|
2020-02-02 03:57:27 +00:00
|
|
|
return null;
|
2020-01-26 20:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:47:30 +00:00
|
|
|
static validate(value, schema, info) {
|
|
|
|
JsonSchemaProxyHandler.validateSingleSchema(value, schema, info);
|
|
|
|
JsonSchemaProxyHandler.validateConditional(value, schema, info);
|
|
|
|
JsonSchemaProxyHandler.validateAllOf(value, schema, info);
|
|
|
|
JsonSchemaProxyHandler.validateAnyOf(value, schema, info);
|
|
|
|
JsonSchemaProxyHandler.validateOneOf(value, schema, info);
|
|
|
|
JsonSchemaProxyHandler.validateNoneOf(value, schema, info);
|
2020-01-26 15:57:52 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 15:47:30 +00:00
|
|
|
static validateConditional(value, schema, info) {
|
2020-02-02 15:17:16 +00:00
|
|
|
const ifSchema = schema.if;
|
|
|
|
if (!JsonSchemaProxyHandler.isObject(ifSchema)) { return; }
|
2020-01-26 16:13:13 +00:00
|
|
|
|
2020-02-02 15:17:16 +00:00
|
|
|
let okay = true;
|
2020-02-02 15:47:30 +00:00
|
|
|
info.schemaPush('if', ifSchema);
|
2020-02-02 15:17:16 +00:00
|
|
|
try {
|
2020-02-02 15:35:41 +00:00
|
|
|
JsonSchemaProxyHandler.validate(value, ifSchema, info);
|
2020-02-02 15:17:16 +00:00
|
|
|
} catch (e) {
|
|
|
|
okay = false;
|
2020-01-26 16:13:13 +00:00
|
|
|
}
|
2020-02-02 15:47:30 +00:00
|
|
|
info.schemaPop();
|
2020-01-26 16:13:13 +00:00
|
|
|
|
2020-02-02 15:17:16 +00:00
|
|
|
const nextSchema = okay ? schema.then : schema.else;
|
|
|
|
if (JsonSchemaProxyHandler.isObject(nextSchema)) {
|
2020-02-02 15:47:30 +00:00
|
|
|
info.schemaPush(okay ? 'then' : 'else', nextSchema);
|
|
|
|
JsonSchemaProxyHandler.validate(value, nextSchema, info);
|
|
|
|
info.schemaPop();
|
2020-01-26 16:13:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:47:30 +00:00
|
|
|
static validateAllOf(value, schema, info) {
|
2020-01-26 15:57:52 +00:00
|
|
|
const subSchemas = schema.allOf;
|
2020-02-02 05:13:46 +00:00
|
|
|
if (!Array.isArray(subSchemas)) { return; }
|
2020-01-26 15:57:52 +00:00
|
|
|
|
2020-02-02 15:47:30 +00:00
|
|
|
info.schemaPush('allOf', subSchemas);
|
2020-01-26 15:57:52 +00:00
|
|
|
for (let i = 0; i < subSchemas.length; ++i) {
|
2020-02-02 15:47:30 +00:00
|
|
|
const subSchema = subSchemas[i];
|
|
|
|
info.schemaPush(i, subSchema);
|
|
|
|
JsonSchemaProxyHandler.validate(value, subSchema, info);
|
|
|
|
info.schemaPop();
|
2020-01-26 15:57:52 +00:00
|
|
|
}
|
2020-02-02 15:47:30 +00:00
|
|
|
info.schemaPop();
|
2020-01-26 15:57:52 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 15:47:30 +00:00
|
|
|
static validateAnyOf(value, schema, info) {
|
2020-01-26 15:57:52 +00:00
|
|
|
const subSchemas = schema.anyOf;
|
2020-02-02 05:13:46 +00:00
|
|
|
if (!Array.isArray(subSchemas)) { return; }
|
2020-01-26 15:57:52 +00:00
|
|
|
|
2020-02-02 15:47:30 +00:00
|
|
|
info.schemaPush('anyOf', subSchemas);
|
2020-01-26 15:57:52 +00:00
|
|
|
for (let i = 0; i < subSchemas.length; ++i) {
|
2020-02-02 15:47:30 +00:00
|
|
|
const subSchema = subSchemas[i];
|
|
|
|
info.schemaPush(i, subSchema);
|
2020-02-02 05:13:46 +00:00
|
|
|
try {
|
2020-02-02 15:47:30 +00:00
|
|
|
JsonSchemaProxyHandler.validate(value, subSchema, info);
|
2020-02-02 05:13:46 +00:00
|
|
|
return;
|
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
|
|
|
}
|
2020-02-02 15:47:30 +00:00
|
|
|
info.schemaPop();
|
2020-01-26 15:57:52 +00:00
|
|
|
}
|
2020-02-02 05:13:46 +00:00
|
|
|
|
2020-02-02 15:47:30 +00:00
|
|
|
throw new JsonSchemaValidationError('0 anyOf schemas matched', value, schema, info);
|
|
|
|
// info.schemaPop(); // Unreachable
|
2020-01-26 15:57:52 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 15:47:30 +00:00
|
|
|
static validateOneOf(value, schema, info) {
|
2020-01-26 15:57:52 +00:00
|
|
|
const subSchemas = schema.oneOf;
|
2020-02-02 05:13:46 +00:00
|
|
|
if (!Array.isArray(subSchemas)) { return; }
|
2020-01-26 15:57:52 +00:00
|
|
|
|
2020-02-02 15:47:30 +00:00
|
|
|
info.schemaPush('oneOf', subSchemas);
|
2020-01-26 15:57:52 +00:00
|
|
|
let count = 0;
|
|
|
|
for (let i = 0; i < subSchemas.length; ++i) {
|
2020-02-02 15:47:30 +00:00
|
|
|
const subSchema = subSchemas[i];
|
|
|
|
info.schemaPush(i, subSchema);
|
2020-02-02 05:13:46 +00:00
|
|
|
try {
|
2020-02-02 15:47:30 +00:00
|
|
|
JsonSchemaProxyHandler.validate(value, subSchema, info);
|
2020-02-02 05:13:46 +00:00
|
|
|
++count;
|
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
|
|
|
}
|
2020-02-02 15:47:30 +00:00
|
|
|
info.schemaPop();
|
2020-02-02 05:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count !== 1) {
|
2020-02-02 15:47:30 +00:00
|
|
|
throw new JsonSchemaValidationError(`${count} oneOf schemas matched`, value, schema, info);
|
2020-01-26 15:57:52 +00:00
|
|
|
}
|
2020-02-02 15:47:30 +00:00
|
|
|
|
|
|
|
info.schemaPop();
|
2020-01-26 15:57:52 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 15:47:30 +00:00
|
|
|
static validateNoneOf(value, schema, info) {
|
2020-01-26 15:57:52 +00:00
|
|
|
const subSchemas = schema.not;
|
2020-02-02 05:13:46 +00:00
|
|
|
if (!Array.isArray(subSchemas)) { return; }
|
2020-01-26 15:57:52 +00:00
|
|
|
|
2020-02-02 15:47:30 +00:00
|
|
|
info.schemaPush('not', subSchemas);
|
2020-01-26 15:57:52 +00:00
|
|
|
for (let i = 0; i < subSchemas.length; ++i) {
|
2020-02-02 15:47:30 +00:00
|
|
|
const subSchema = subSchemas[i];
|
|
|
|
info.schemaPush(i, subSchema);
|
2020-02-02 05:13:46 +00:00
|
|
|
try {
|
2020-02-02 15:47:30 +00:00
|
|
|
JsonSchemaProxyHandler.validate(value, subSchema, info);
|
2020-02-02 05:13:46 +00:00
|
|
|
} catch (e) {
|
2020-02-02 15:47:30 +00:00
|
|
|
info.schemaPop();
|
2020-02-02 05:13:46 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-02-02 15:47:30 +00:00
|
|
|
throw new JsonSchemaValidationError(`not[${i}] schema matched`, value, schema, info);
|
2020-01-26 15:57:52 +00:00
|
|
|
}
|
2020-02-02 15:47:30 +00:00
|
|
|
info.schemaPop();
|
2020-01-26 15:57:52 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 15:47:30 +00:00
|
|
|
static validateSingleSchema(value, schema, info) {
|
2019-11-28 17:19:15 +00:00
|
|
|
const type = JsonSchemaProxyHandler.getValueType(value);
|
|
|
|
const schemaType = schema.type;
|
|
|
|
if (!JsonSchemaProxyHandler.isValueTypeAny(value, type, schemaType)) {
|
2020-02-02 15:47:30 +00:00
|
|
|
throw new JsonSchemaValidationError(`Value type ${type} does not match schema type ${schemaType}`, value, schema, info);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const schemaEnum = schema.enum;
|
|
|
|
if (Array.isArray(schemaEnum) && !JsonSchemaProxyHandler.valuesAreEqualAny(value, schemaEnum)) {
|
2020-02-02 15:47:30 +00:00
|
|
|
throw new JsonSchemaValidationError('Invalid enum value', value, schema, info);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 'number':
|
2020-02-02 15:47:30 +00:00
|
|
|
JsonSchemaProxyHandler.validateNumber(value, schema, info);
|
2020-02-02 05:13:46 +00:00
|
|
|
break;
|
2019-11-28 17:19:15 +00:00
|
|
|
case 'string':
|
2020-02-02 15:47:30 +00:00
|
|
|
JsonSchemaProxyHandler.validateString(value, schema, info);
|
2020-02-02 05:13:46 +00:00
|
|
|
break;
|
2019-11-28 17:19:15 +00:00
|
|
|
case 'array':
|
2020-02-02 15:47:30 +00:00
|
|
|
JsonSchemaProxyHandler.validateArray(value, schema, info);
|
2020-02-02 05:13:46 +00:00
|
|
|
break;
|
2019-11-28 17:19:15 +00:00
|
|
|
case 'object':
|
2020-02-02 15:47:30 +00:00
|
|
|
JsonSchemaProxyHandler.validateObject(value, schema, info);
|
2020-02-02 05:13:46 +00:00
|
|
|
break;
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:47:30 +00:00
|
|
|
static validateNumber(value, schema, info) {
|
2019-11-28 17:19:15 +00:00
|
|
|
const multipleOf = schema.multipleOf;
|
|
|
|
if (typeof multipleOf === 'number' && Math.floor(value / multipleOf) * multipleOf !== value) {
|
2020-02-02 15:47:30 +00:00
|
|
|
throw new JsonSchemaValidationError(`Number is not a multiple of ${multipleOf}`, value, schema, info);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const minimum = schema.minimum;
|
|
|
|
if (typeof minimum === 'number' && value < minimum) {
|
2020-02-02 15:47:30 +00:00
|
|
|
throw new JsonSchemaValidationError(`Number is less than ${minimum}`, value, schema, info);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const exclusiveMinimum = schema.exclusiveMinimum;
|
|
|
|
if (typeof exclusiveMinimum === 'number' && value <= exclusiveMinimum) {
|
2020-02-02 15:47:30 +00:00
|
|
|
throw new JsonSchemaValidationError(`Number is less than or equal to ${exclusiveMinimum}`, value, schema, info);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const maximum = schema.maximum;
|
|
|
|
if (typeof maximum === 'number' && value > maximum) {
|
2020-02-02 15:47:30 +00:00
|
|
|
throw new JsonSchemaValidationError(`Number is greater than ${maximum}`, value, schema, info);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const exclusiveMaximum = schema.exclusiveMaximum;
|
|
|
|
if (typeof exclusiveMaximum === 'number' && value >= exclusiveMaximum) {
|
2020-02-02 15:47:30 +00:00
|
|
|
throw new JsonSchemaValidationError(`Number is greater than or equal to ${exclusiveMaximum}`, value, schema, info);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:47:30 +00:00
|
|
|
static validateString(value, schema, info) {
|
2019-11-28 17:19:15 +00:00
|
|
|
const minLength = schema.minLength;
|
|
|
|
if (typeof minLength === 'number' && value.length < minLength) {
|
2020-02-02 15:47:30 +00:00
|
|
|
throw new JsonSchemaValidationError('String length too short', value, schema, info);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 03:20:47 +00:00
|
|
|
const maxLength = schema.maxLength;
|
2019-11-28 17:19:15 +00:00
|
|
|
if (typeof maxLength === 'number' && value.length > maxLength) {
|
2020-02-02 15:47:30 +00:00
|
|
|
throw new JsonSchemaValidationError('String length too long', value, schema, info);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:47:30 +00:00
|
|
|
static validateArray(value, schema, info) {
|
2019-11-28 17:19:15 +00:00
|
|
|
const minItems = schema.minItems;
|
|
|
|
if (typeof minItems === 'number' && value.length < minItems) {
|
2020-02-02 15:47:30 +00:00
|
|
|
throw new JsonSchemaValidationError('Array length too short', value, schema, info);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const maxItems = schema.maxItems;
|
|
|
|
if (typeof maxItems === 'number' && value.length > maxItems) {
|
2020-02-02 15:47:30 +00:00
|
|
|
throw new JsonSchemaValidationError('Array length too long', value, schema, info);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 17:34:27 +00:00
|
|
|
for (let i = 0, ii = value.length; i < ii; ++i) {
|
2020-02-02 16:18:13 +00:00
|
|
|
const schemaPath = [];
|
|
|
|
const propertySchema = JsonSchemaProxyHandler.getPropertySchema(schema, i, value, schemaPath);
|
2020-01-26 17:34:27 +00:00
|
|
|
if (propertySchema === null) {
|
2020-02-02 15:47:30 +00:00
|
|
|
throw new JsonSchemaValidationError(`No schema found for array[${i}]`, value, schema, info);
|
2020-01-26 17:34:27 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 15:47:30 +00:00
|
|
|
const propertyValue = value[i];
|
|
|
|
|
2020-02-02 16:18:13 +00:00
|
|
|
for (const [p, s] of schemaPath) { info.schemaPush(p, s); }
|
2020-02-02 15:47:30 +00:00
|
|
|
info.valuePush(i, propertyValue);
|
|
|
|
JsonSchemaProxyHandler.validate(propertyValue, propertySchema, info);
|
|
|
|
info.valuePop();
|
2020-02-02 16:18:13 +00:00
|
|
|
for (let i = 0; i < schemaPath.length; ++i) { info.schemaPop(); }
|
2020-01-26 17:34:27 +00:00
|
|
|
}
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 15:47:30 +00:00
|
|
|
static validateObject(value, schema, info) {
|
2019-11-28 17:19:15 +00:00
|
|
|
const properties = new Set(Object.getOwnPropertyNames(value));
|
|
|
|
|
|
|
|
const required = schema.required;
|
|
|
|
if (Array.isArray(required)) {
|
|
|
|
for (const property of required) {
|
|
|
|
if (!properties.has(property)) {
|
2020-02-02 15:47:30 +00:00
|
|
|
throw new JsonSchemaValidationError(`Missing property ${property}`, value, schema, info);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const minProperties = schema.minProperties;
|
|
|
|
if (typeof minProperties === 'number' && properties.length < minProperties) {
|
2020-02-02 15:47:30 +00:00
|
|
|
throw new JsonSchemaValidationError('Not enough object properties', value, schema, info);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const maxProperties = schema.maxProperties;
|
|
|
|
if (typeof maxProperties === 'number' && properties.length > maxProperties) {
|
2020-02-02 15:47:30 +00:00
|
|
|
throw new JsonSchemaValidationError('Too many object properties', value, schema, info);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const property of properties) {
|
2020-02-02 16:18:13 +00:00
|
|
|
const schemaPath = [];
|
|
|
|
const propertySchema = JsonSchemaProxyHandler.getPropertySchema(schema, property, value, schemaPath);
|
2019-11-28 17:19:15 +00:00
|
|
|
if (propertySchema === null) {
|
2020-02-02 15:47:30 +00:00
|
|
|
throw new JsonSchemaValidationError(`No schema found for ${property}`, value, schema, info);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
2020-02-02 15:47:30 +00:00
|
|
|
|
|
|
|
const propertyValue = value[property];
|
|
|
|
|
2020-02-02 16:18:13 +00:00
|
|
|
for (const [p, s] of schemaPath) { info.schemaPush(p, s); }
|
2020-02-02 15:47:30 +00:00
|
|
|
info.valuePush(property, propertyValue);
|
|
|
|
JsonSchemaProxyHandler.validate(propertyValue, propertySchema, info);
|
|
|
|
info.valuePop();
|
2020-02-02 16:18:13 +00:00
|
|
|
for (let i = 0; i < schemaPath.length; ++i) { info.schemaPop(); }
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static isValueTypeAny(value, type, schemaTypes) {
|
|
|
|
if (typeof schemaTypes === 'string') {
|
|
|
|
return JsonSchemaProxyHandler.isValueType(value, type, schemaTypes);
|
|
|
|
} else if (Array.isArray(schemaTypes)) {
|
|
|
|
for (const schemaType of schemaTypes) {
|
|
|
|
if (JsonSchemaProxyHandler.isValueType(value, type, schemaType)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static isValueType(value, type, schemaType) {
|
|
|
|
return (
|
|
|
|
type === schemaType ||
|
|
|
|
(schemaType === 'integer' && Math.floor(value) === value)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static getValueType(value) {
|
|
|
|
const type = typeof value;
|
|
|
|
if (type === 'object') {
|
|
|
|
if (value === null) { return 'null'; }
|
|
|
|
if (Array.isArray(value)) { return 'array'; }
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static valuesAreEqualAny(value1, valueList) {
|
|
|
|
for (const value2 of valueList) {
|
|
|
|
if (JsonSchemaProxyHandler.valuesAreEqual(value1, value2)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static valuesAreEqual(value1, value2) {
|
|
|
|
return value1 === value2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static getDefaultTypeValue(type) {
|
|
|
|
if (typeof type === 'string') {
|
|
|
|
switch (type) {
|
|
|
|
case 'null':
|
|
|
|
return null;
|
|
|
|
case 'boolean':
|
|
|
|
return false;
|
|
|
|
case 'number':
|
|
|
|
case 'integer':
|
|
|
|
return 0;
|
|
|
|
case 'string':
|
|
|
|
return '';
|
|
|
|
case 'array':
|
|
|
|
return [];
|
|
|
|
case 'object':
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
static getValidValueOrDefault(schema, value) {
|
|
|
|
let type = JsonSchemaProxyHandler.getValueType(value);
|
|
|
|
const schemaType = schema.type;
|
|
|
|
if (!JsonSchemaProxyHandler.isValueTypeAny(value, type, schemaType)) {
|
|
|
|
let assignDefault = true;
|
|
|
|
|
|
|
|
const schemaDefault = schema.default;
|
|
|
|
if (typeof schemaDefault !== 'undefined') {
|
|
|
|
value = JsonSchema.isolate(schemaDefault);
|
|
|
|
type = JsonSchemaProxyHandler.getValueType(value);
|
|
|
|
assignDefault = !JsonSchemaProxyHandler.isValueTypeAny(value, type, schemaType);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (assignDefault) {
|
|
|
|
value = JsonSchemaProxyHandler.getDefaultTypeValue(schemaType);
|
|
|
|
type = JsonSchemaProxyHandler.getValueType(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-29 23:57:29 +00:00
|
|
|
switch (type) {
|
|
|
|
case 'object':
|
|
|
|
value = JsonSchemaProxyHandler.populateObjectDefaults(value, schema);
|
|
|
|
break;
|
|
|
|
case 'array':
|
|
|
|
value = JsonSchemaProxyHandler.populateArrayDefaults(value, schema);
|
|
|
|
break;
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static populateObjectDefaults(value, schema) {
|
|
|
|
const properties = new Set(Object.getOwnPropertyNames(value));
|
|
|
|
|
|
|
|
const required = schema.required;
|
|
|
|
if (Array.isArray(required)) {
|
|
|
|
for (const property of required) {
|
|
|
|
properties.delete(property);
|
|
|
|
|
2020-01-26 20:57:39 +00:00
|
|
|
const propertySchema = JsonSchemaProxyHandler.getPropertySchema(schema, property, value);
|
2019-11-28 17:19:15 +00:00
|
|
|
if (propertySchema === null) { continue; }
|
|
|
|
value[property] = JsonSchemaProxyHandler.getValidValueOrDefault(propertySchema, value[property]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const property of properties) {
|
2020-01-26 20:57:39 +00:00
|
|
|
const propertySchema = JsonSchemaProxyHandler.getPropertySchema(schema, property, value);
|
2019-11-28 17:19:15 +00:00
|
|
|
if (propertySchema === null) {
|
|
|
|
Reflect.deleteProperty(value, property);
|
|
|
|
} else {
|
|
|
|
value[property] = JsonSchemaProxyHandler.getValidValueOrDefault(propertySchema, value[property]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
2019-12-29 23:57:29 +00:00
|
|
|
|
|
|
|
static populateArrayDefaults(value, schema) {
|
|
|
|
for (let i = 0, ii = value.length; i < ii; ++i) {
|
2020-01-26 20:57:39 +00:00
|
|
|
const propertySchema = JsonSchemaProxyHandler.getPropertySchema(schema, i, value);
|
2019-12-29 23:57:29 +00:00
|
|
|
if (propertySchema === null) { continue; }
|
|
|
|
value[i] = JsonSchemaProxyHandler.getValidValueOrDefault(propertySchema, value[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
2020-01-26 16:06:03 +00:00
|
|
|
|
|
|
|
static isObject(value) {
|
|
|
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
|
|
}
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 20:45:31 +00:00
|
|
|
JsonSchemaProxyHandler._unconstrainedSchema = {};
|
|
|
|
|
2020-02-02 15:47:30 +00:00
|
|
|
class JsonSchemaTraversalInfo {
|
|
|
|
constructor(value, schema) {
|
|
|
|
this.valuePath = [];
|
|
|
|
this.schemaPath = [];
|
2020-02-02 16:22:22 +00:00
|
|
|
this.valuePush(null, value);
|
|
|
|
this.schemaPush(null, schema);
|
2020-02-02 15:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
valuePush(path, value) {
|
|
|
|
this.valuePath.push([path, value]);
|
|
|
|
}
|
|
|
|
|
|
|
|
valuePop() {
|
|
|
|
this.valuePath.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
schemaPush(path, schema) {
|
|
|
|
this.schemaPath.push([path, schema]);
|
|
|
|
}
|
|
|
|
|
|
|
|
schemaPop() {
|
|
|
|
this.schemaPath.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 05:13:46 +00:00
|
|
|
class JsonSchemaValidationError extends Error {
|
2020-02-02 15:47:30 +00:00
|
|
|
constructor(message, value, schema, info) {
|
2020-02-02 05:13:46 +00:00
|
|
|
super(message);
|
|
|
|
this.value = value;
|
|
|
|
this.schema = schema;
|
2020-02-02 15:47:30 +00:00
|
|
|
this.info = info;
|
2020-02-02 05:13:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 17:19:15 +00:00
|
|
|
class JsonSchema {
|
|
|
|
static createProxy(target, schema) {
|
|
|
|
return new Proxy(target, new JsonSchemaProxyHandler(schema));
|
|
|
|
}
|
|
|
|
|
2020-01-26 19:59:37 +00:00
|
|
|
static validate(value, schema) {
|
2020-02-02 15:47:30 +00:00
|
|
|
return JsonSchemaProxyHandler.validate(value, schema, new JsonSchemaTraversalInfo(value, schema));
|
2020-01-26 19:59:37 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 17:19:15 +00:00
|
|
|
static getValidValueOrDefault(schema, value) {
|
|
|
|
return JsonSchemaProxyHandler.getValidValueOrDefault(schema, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isolate(value) {
|
|
|
|
if (value === null) { return null; }
|
|
|
|
|
|
|
|
switch (typeof value) {
|
|
|
|
case 'boolean':
|
|
|
|
case 'number':
|
|
|
|
case 'string':
|
|
|
|
case 'bigint':
|
|
|
|
case 'symbol':
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
const stringValue = JSON.stringify(value);
|
|
|
|
return typeof stringValue === 'string' ? JSON.parse(stringValue) : null;
|
|
|
|
}
|
|
|
|
}
|