2019-11-28 17:19:15 +00:00
|
|
|
/*
|
2021-01-01 19:50:41 +00:00
|
|
|
* Copyright (C) 2019-2021 Yomichan Authors
|
2019-11-28 17:19:15 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
2020-08-09 18:18:59 +00:00
|
|
|
/* global
|
|
|
|
* CacheMap
|
|
|
|
*/
|
2019-11-28 17:19:15 +00:00
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
class JsonSchema {
|
|
|
|
constructor(schema, rootSchema) {
|
|
|
|
this._schema = null;
|
|
|
|
this._startSchema = schema;
|
|
|
|
this._rootSchema = typeof rootSchema !== 'undefined' ? rootSchema : schema;
|
|
|
|
this._regexCache = null;
|
2021-05-23 19:49:25 +00:00
|
|
|
this._refCache = null;
|
2021-05-22 21:56:44 +00:00
|
|
|
this._valueStack = [];
|
|
|
|
this._schemaStack = [];
|
2019-11-28 17:19:15 +00:00
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
this._schemaPush(null, null);
|
|
|
|
this._valuePush(null, null);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
get schema() {
|
|
|
|
return this._startSchema;
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
get rootSchema() {
|
|
|
|
return this._rootSchema;
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
createProxy(value) {
|
|
|
|
return (
|
|
|
|
typeof value === 'object' && value !== null ?
|
|
|
|
new Proxy(value, new JsonSchemaProxyHandler(this)) :
|
|
|
|
value
|
|
|
|
);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
isValid(value) {
|
|
|
|
try {
|
|
|
|
this.validate(value);
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
validate(value) {
|
2021-05-22 21:56:44 +00:00
|
|
|
this._schemaPush(this._startSchema, null);
|
|
|
|
this._valuePush(value, null);
|
2021-05-22 19:45:20 +00:00
|
|
|
try {
|
|
|
|
this._validate(value);
|
|
|
|
} finally {
|
|
|
|
this._valuePop();
|
|
|
|
this._schemaPop();
|
|
|
|
}
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
getValidValueOrDefault(value) {
|
2021-05-22 21:56:44 +00:00
|
|
|
return this._getValidValueOrDefault(null, value, {schema: this._startSchema, path: null});
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
getObjectPropertySchema(property) {
|
2021-05-23 19:49:25 +00:00
|
|
|
const startSchemaInfo = this._getResolveSchemaInfo({schema: this._startSchema, path: null});
|
|
|
|
this._schemaPush(startSchemaInfo.schema, startSchemaInfo.path);
|
2021-05-22 19:45:20 +00:00
|
|
|
try {
|
2021-05-22 22:01:16 +00:00
|
|
|
const schemaInfo = this._getObjectPropertySchemaInfo(property);
|
2021-05-22 21:56:44 +00:00
|
|
|
return schemaInfo !== null ? new JsonSchema(schemaInfo.schema, this._rootSchema) : null;
|
2021-05-22 19:45:20 +00:00
|
|
|
} finally {
|
|
|
|
this._schemaPop();
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
getArrayItemSchema(index) {
|
2021-05-23 19:49:25 +00:00
|
|
|
const startSchemaInfo = this._getResolveSchemaInfo({schema: this._startSchema, path: null});
|
|
|
|
this._schemaPush(startSchemaInfo.schema, startSchemaInfo.path);
|
2021-05-22 19:45:20 +00:00
|
|
|
try {
|
2021-05-22 22:01:16 +00:00
|
|
|
const schemaInfo = this._getArrayItemSchemaInfo(index);
|
2021-05-22 21:56:44 +00:00
|
|
|
return schemaInfo !== null ? new JsonSchema(schemaInfo.schema, this._rootSchema) : null;
|
2021-05-22 19:45:20 +00:00
|
|
|
} finally {
|
|
|
|
this._schemaPop();
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
isObjectPropertyRequired(property) {
|
|
|
|
const {required} = this._startSchema;
|
|
|
|
return Array.isArray(required) && required.includes(property);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
// Stack
|
2019-11-28 17:19:15 +00:00
|
|
|
|
2021-05-22 21:56:44 +00:00
|
|
|
_valuePush(value, path) {
|
|
|
|
this._valueStack.push({value, path});
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
_valuePop() {
|
2021-05-22 21:56:44 +00:00
|
|
|
this._valueStack.pop();
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
2020-08-09 18:18:59 +00:00
|
|
|
|
2021-05-22 21:56:44 +00:00
|
|
|
_schemaPush(schema, path) {
|
|
|
|
this._schemaStack.push({schema, path});
|
2021-05-22 19:45:20 +00:00
|
|
|
this._schema = schema;
|
2020-08-09 18:18:59 +00:00
|
|
|
}
|
2019-11-28 17:19:15 +00:00
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
_schemaPop() {
|
2021-05-22 21:56:44 +00:00
|
|
|
this._schemaStack.pop();
|
|
|
|
this._schema = this._schemaStack[this._schemaStack.length - 1].schema;
|
2020-08-15 21:23:09 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
// Private
|
|
|
|
|
|
|
|
_createError(message) {
|
2021-05-22 21:56:44 +00:00
|
|
|
const valueStack = [];
|
|
|
|
for (let i = 1, ii = this._valueStack.length; i < ii; ++i) {
|
|
|
|
const {value, path} = this._valueStack[i];
|
|
|
|
valueStack.push({value, path});
|
2020-08-15 21:23:09 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 21:56:44 +00:00
|
|
|
const schemaStack = [];
|
|
|
|
for (let i = 1, ii = this._schemaStack.length; i < ii; ++i) {
|
|
|
|
const {schema, path} = this._schemaStack[i];
|
|
|
|
schemaStack.push({schema, path});
|
2021-05-22 19:45:20 +00:00
|
|
|
}
|
2020-08-15 21:23:09 +00:00
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
const error = new Error(message);
|
2021-05-22 21:56:44 +00:00
|
|
|
error.value = valueStack[valueStack.length - 1].value;
|
2021-05-22 19:45:20 +00:00
|
|
|
error.schema = this._schema;
|
2021-05-22 21:56:44 +00:00
|
|
|
error.valueStack = valueStack;
|
|
|
|
error.schemaStack = schemaStack;
|
2021-05-22 19:45:20 +00:00
|
|
|
return error;
|
2020-08-15 21:23:09 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
_isObject(value) {
|
|
|
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
2020-08-15 21:23:09 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
_getRegex(pattern, flags) {
|
|
|
|
if (this._regexCache === null) {
|
|
|
|
this._regexCache = new CacheMap(100);
|
|
|
|
}
|
2020-09-04 21:44:00 +00:00
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
const key = `${flags}:${pattern}`;
|
|
|
|
let regex = this._regexCache.get(key);
|
|
|
|
if (typeof regex === 'undefined') {
|
|
|
|
regex = new RegExp(pattern, flags);
|
|
|
|
this._regexCache.set(key, regex);
|
|
|
|
}
|
|
|
|
return regex;
|
2021-05-22 18:01:32 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
_getUnconstrainedSchema() {
|
|
|
|
return {};
|
|
|
|
}
|
2019-11-28 17:19:15 +00:00
|
|
|
|
2021-05-22 22:01:16 +00:00
|
|
|
_getObjectPropertySchemaInfo(property) {
|
2021-05-22 19:45:20 +00:00
|
|
|
const {properties} = this._schema;
|
|
|
|
if (this._isObject(properties)) {
|
|
|
|
const propertySchema = properties[property];
|
|
|
|
if (this._isObject(propertySchema)) {
|
2021-05-22 21:56:44 +00:00
|
|
|
return {schema: propertySchema, path: ['properties', property]};
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
2021-05-22 19:45:20 +00:00
|
|
|
}
|
2020-01-26 17:34:27 +00:00
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
const {additionalProperties} = this._schema;
|
|
|
|
if (additionalProperties === false) {
|
|
|
|
return null;
|
|
|
|
} else if (this._isObject(additionalProperties)) {
|
2021-05-22 21:56:44 +00:00
|
|
|
return {schema: additionalProperties, path: 'additionalProperties'};
|
2021-05-22 19:45:20 +00:00
|
|
|
} else {
|
|
|
|
const result = this._getUnconstrainedSchema();
|
2021-05-22 21:56:44 +00:00
|
|
|
return {schema: result, path: null};
|
2021-05-22 19:45:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 22:01:16 +00:00
|
|
|
_getArrayItemSchemaInfo(index) {
|
2021-05-22 19:45:20 +00:00
|
|
|
const {items} = this._schema;
|
|
|
|
if (this._isObject(items)) {
|
2021-05-22 21:56:44 +00:00
|
|
|
return {schema: items, path: 'items'};
|
2021-05-22 19:45:20 +00:00
|
|
|
}
|
|
|
|
if (Array.isArray(items)) {
|
|
|
|
if (index >= 0 && index < items.length) {
|
|
|
|
const propertySchema = items[index];
|
|
|
|
if (this._isObject(propertySchema)) {
|
2021-05-22 21:56:44 +00:00
|
|
|
return {schema: propertySchema, path: ['items', index]};
|
2020-01-26 17:34:27 +00:00
|
|
|
}
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
2021-05-22 19:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const {additionalItems} = this._schema;
|
|
|
|
if (additionalItems === false) {
|
|
|
|
return null;
|
|
|
|
} else if (this._isObject(additionalItems)) {
|
2021-05-22 21:56:44 +00:00
|
|
|
return {schema: additionalItems, path: 'additionalItems'};
|
2021-05-22 19:45:20 +00:00
|
|
|
} else {
|
|
|
|
const result = this._getUnconstrainedSchema();
|
2021-05-22 21:56:44 +00:00
|
|
|
return {schema: result, path: null};
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
_getSchemaOrValueType(value) {
|
|
|
|
const {type} = this._schema;
|
2020-01-26 20:57:39 +00:00
|
|
|
|
|
|
|
if (Array.isArray(type)) {
|
|
|
|
if (typeof value !== 'undefined') {
|
2020-08-15 21:23:09 +00:00
|
|
|
const valueType = this._getValueType(value);
|
2020-01-26 20:57:39 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
if (typeof type !== 'undefined') { return type; }
|
|
|
|
return (typeof value !== 'undefined') ? this._getValueType(value) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
_getValueType(value) {
|
|
|
|
const type = typeof value;
|
|
|
|
if (type === 'object') {
|
|
|
|
if (value === null) { return 'null'; }
|
|
|
|
if (Array.isArray(value)) { return 'array'; }
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
_isValueTypeAny(value, type, schemaTypes) {
|
|
|
|
if (typeof schemaTypes === 'string') {
|
|
|
|
return this._isValueType(value, type, schemaTypes);
|
|
|
|
} else if (Array.isArray(schemaTypes)) {
|
|
|
|
for (const schemaType of schemaTypes) {
|
|
|
|
if (this._isValueType(value, type, schemaType)) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-01-26 20:57:39 +00:00
|
|
|
}
|
2021-05-22 19:45:20 +00:00
|
|
|
return false;
|
2020-01-26 20:57:39 +00:00
|
|
|
}
|
2021-05-22 19:45:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-01-26 20:57:39 +00:00
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
_isValueType(value, type, schemaType) {
|
|
|
|
return (
|
|
|
|
type === schemaType ||
|
|
|
|
(schemaType === 'integer' && Math.floor(value) === value)
|
|
|
|
);
|
2020-01-26 20:57:39 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
_valuesAreEqualAny(value1, valueList) {
|
|
|
|
for (const value2 of valueList) {
|
|
|
|
if (this._valuesAreEqual(value1, value2)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_valuesAreEqual(value1, value2) {
|
|
|
|
return value1 === value2;
|
|
|
|
}
|
|
|
|
|
2021-05-23 19:49:25 +00:00
|
|
|
_getResolveSchemaInfo(schemaInfo) {
|
|
|
|
const ref = schemaInfo.schema.$ref;
|
|
|
|
if (typeof ref !== 'string') { return schemaInfo; }
|
|
|
|
|
|
|
|
const {path: basePath} = schemaInfo;
|
|
|
|
const {schema, path} = this._getReference(ref);
|
|
|
|
if (Array.isArray(basePath)) {
|
|
|
|
path.unshift(...basePath);
|
|
|
|
} else {
|
|
|
|
path.unshift(basePath);
|
|
|
|
}
|
|
|
|
return {schema, path};
|
|
|
|
}
|
|
|
|
|
|
|
|
_getReference(ref) {
|
|
|
|
if (!ref.startsWith('#/')) {
|
|
|
|
throw this._createError(`Unsupported reference path: ${ref}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
let info;
|
|
|
|
if (this._refCache !== null) {
|
|
|
|
info = this._refCache.get(ref);
|
|
|
|
} else {
|
|
|
|
this._refCache = new Map();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof info === 'undefined') {
|
|
|
|
info = this._getReferenceUncached(ref);
|
|
|
|
this._refCache.set(ref, info);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {schema: info.schema, path: [...info.path]};
|
|
|
|
}
|
|
|
|
|
|
|
|
_getReferenceUncached(ref) {
|
|
|
|
const visited = new Set();
|
|
|
|
const path = [];
|
|
|
|
while (true) {
|
|
|
|
if (visited.has(ref)) {
|
|
|
|
throw this._createError(`Recursive reference: ${ref}`);
|
|
|
|
}
|
|
|
|
visited.add(ref);
|
|
|
|
|
|
|
|
const pathParts = ref.substring(2).split('/');
|
|
|
|
let schema = this._rootSchema;
|
|
|
|
try {
|
|
|
|
for (const pathPart of pathParts) {
|
|
|
|
schema = schema[pathPart];
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
throw this._createError(`Invalid reference: ${ref}`);
|
|
|
|
}
|
|
|
|
if (!this._isObject(schema)) {
|
|
|
|
throw this._createError(`Invalid reference: ${ref}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
path.push(null, ...pathParts);
|
|
|
|
|
|
|
|
ref = schema.$ref;
|
|
|
|
if (typeof ref !== 'string') {
|
|
|
|
return {schema, path};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
// Validation
|
|
|
|
|
|
|
|
_isValidCurrent(value) {
|
|
|
|
try {
|
|
|
|
this._validate(value);
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-26 15:57:52 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
_validate(value) {
|
2021-05-23 19:49:25 +00:00
|
|
|
const ref = this._schema.$ref;
|
|
|
|
const schemaInfo = (typeof ref === 'string') ? this._getReference(ref) : null;
|
|
|
|
|
|
|
|
if (schemaInfo === null) {
|
|
|
|
this._validateInner(value);
|
|
|
|
} else {
|
|
|
|
this._schemaPush(schemaInfo.schema, schemaInfo.path);
|
|
|
|
try {
|
|
|
|
this._validateInner(value);
|
|
|
|
} finally {
|
|
|
|
this._schemaPop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_validateInner(value) {
|
2021-05-22 19:45:20 +00:00
|
|
|
this._validateSingleSchema(value);
|
|
|
|
this._validateConditional(value);
|
|
|
|
this._validateAllOf(value);
|
|
|
|
this._validateAnyOf(value);
|
|
|
|
this._validateOneOf(value);
|
|
|
|
this._validateNoneOf(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
_validateConditional(value) {
|
|
|
|
const ifSchema = this._schema.if;
|
2020-08-15 21:23:09 +00:00
|
|
|
if (!this._isObject(ifSchema)) { return; }
|
2020-01-26 16:13:13 +00:00
|
|
|
|
2020-02-02 15:17:16 +00:00
|
|
|
let okay = true;
|
2021-05-22 21:56:44 +00:00
|
|
|
this._schemaPush(ifSchema, 'if');
|
2020-02-02 15:17:16 +00:00
|
|
|
try {
|
2021-05-22 19:45:20 +00:00
|
|
|
this._validate(value);
|
2020-02-02 15:17:16 +00:00
|
|
|
} catch (e) {
|
|
|
|
okay = false;
|
2021-05-22 19:45:20 +00:00
|
|
|
} finally {
|
|
|
|
this._schemaPop();
|
2020-01-26 16:13:13 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
const nextSchema = okay ? this._schema.then : this._schema.else;
|
|
|
|
if (this._isObject(nextSchema)) { return; }
|
|
|
|
|
2021-05-22 21:56:44 +00:00
|
|
|
this._schemaPush(nextSchema, okay ? 'then' : 'else');
|
2021-05-22 19:45:20 +00:00
|
|
|
try {
|
|
|
|
this._validate(value);
|
|
|
|
} finally {
|
|
|
|
this._schemaPop();
|
2020-01-26 16:13:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
_validateAllOf(value) {
|
|
|
|
const subSchemas = this._schema.allOf;
|
2020-02-02 05:13:46 +00:00
|
|
|
if (!Array.isArray(subSchemas)) { return; }
|
2020-01-26 15:57:52 +00:00
|
|
|
|
2021-05-22 21:56:44 +00:00
|
|
|
this._schemaPush(subSchemas, 'allOf');
|
2021-05-22 19:45:20 +00:00
|
|
|
try {
|
|
|
|
for (let i = 0, ii = subSchemas.length; i < ii; ++i) {
|
|
|
|
const subSchema = subSchemas[i];
|
|
|
|
if (!this._isObject(subSchema)) { continue; }
|
|
|
|
|
2021-05-22 21:56:44 +00:00
|
|
|
this._schemaPush(subSchema, i);
|
2021-05-22 19:45:20 +00:00
|
|
|
try {
|
|
|
|
this._validate(value);
|
|
|
|
} finally {
|
|
|
|
this._schemaPop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
this._schemaPop();
|
2020-01-26 15:57:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
_validateAnyOf(value) {
|
|
|
|
const subSchemas = this._schema.anyOf;
|
2020-02-02 05:13:46 +00:00
|
|
|
if (!Array.isArray(subSchemas)) { return; }
|
2020-01-26 15:57:52 +00:00
|
|
|
|
2021-05-22 21:56:44 +00:00
|
|
|
this._schemaPush(subSchemas, 'anyOf');
|
2021-05-22 19:45:20 +00:00
|
|
|
try {
|
|
|
|
for (let i = 0, ii = subSchemas.length; i < ii; ++i) {
|
|
|
|
const subSchema = subSchemas[i];
|
|
|
|
if (!this._isObject(subSchema)) { continue; }
|
|
|
|
|
2021-05-22 21:56:44 +00:00
|
|
|
this._schemaPush(subSchema, i);
|
2021-05-22 19:45:20 +00:00
|
|
|
try {
|
|
|
|
this._validate(value);
|
|
|
|
return;
|
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
|
|
|
} finally {
|
|
|
|
this._schemaPop();
|
|
|
|
}
|
2020-02-02 05:13:46 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
throw this._createError('0 anyOf schemas matched');
|
|
|
|
} finally {
|
|
|
|
this._schemaPop();
|
|
|
|
}
|
2020-01-26 15:57:52 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
_validateOneOf(value) {
|
|
|
|
const subSchemas = this._schema.oneOf;
|
2020-02-02 05:13:46 +00:00
|
|
|
if (!Array.isArray(subSchemas)) { return; }
|
2020-01-26 15:57:52 +00:00
|
|
|
|
2021-05-22 21:56:44 +00:00
|
|
|
this._schemaPush(subSchemas, 'oneOf');
|
2021-05-22 19:45:20 +00:00
|
|
|
try {
|
|
|
|
let count = 0;
|
|
|
|
for (let i = 0, ii = subSchemas.length; i < ii; ++i) {
|
|
|
|
const subSchema = subSchemas[i];
|
|
|
|
if (!this._isObject(subSchema)) { continue; }
|
|
|
|
|
2021-05-22 21:56:44 +00:00
|
|
|
this._schemaPush(subSchema, i);
|
2021-05-22 19:45:20 +00:00
|
|
|
try {
|
|
|
|
this._validate(value);
|
|
|
|
++count;
|
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
|
|
|
} finally {
|
|
|
|
this._schemaPop();
|
|
|
|
}
|
2020-02-02 05:13:46 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
if (count !== 1) {
|
|
|
|
throw this._createError(`${count} oneOf schemas matched`);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
this._schemaPop();
|
2020-01-26 15:57:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
_validateNoneOf(value) {
|
|
|
|
const subSchemas = this._schema.not;
|
2020-02-02 05:13:46 +00:00
|
|
|
if (!Array.isArray(subSchemas)) { return; }
|
2020-01-26 15:57:52 +00:00
|
|
|
|
2021-05-22 21:56:44 +00:00
|
|
|
this._schemaPush(subSchemas, 'not');
|
2021-05-22 19:45:20 +00:00
|
|
|
try {
|
|
|
|
for (let i = 0, ii = subSchemas.length; i < ii; ++i) {
|
|
|
|
const subSchema = subSchemas[i];
|
|
|
|
if (!this._isObject(subSchema)) { continue; }
|
|
|
|
|
2021-05-22 21:56:44 +00:00
|
|
|
this._schemaPush(subSchema, i);
|
2021-05-22 19:45:20 +00:00
|
|
|
try {
|
|
|
|
this._validate(value);
|
|
|
|
} catch (e) {
|
|
|
|
continue;
|
|
|
|
} finally {
|
|
|
|
this._schemaPop();
|
|
|
|
}
|
|
|
|
throw this._createError(`not[${i}] schema matched`);
|
2020-02-02 05:13:46 +00:00
|
|
|
}
|
2021-05-22 19:45:20 +00:00
|
|
|
} finally {
|
|
|
|
this._schemaPop();
|
2020-01-26 15:57:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
_validateSingleSchema(value) {
|
2020-08-15 21:23:09 +00:00
|
|
|
const type = this._getValueType(value);
|
2021-05-22 19:45:20 +00:00
|
|
|
const schemaType = this._schema.type;
|
2020-08-15 21:23:09 +00:00
|
|
|
if (!this._isValueTypeAny(value, type, schemaType)) {
|
2021-05-22 19:45:20 +00:00
|
|
|
throw this._createError(`Value type ${type} does not match schema type ${schemaType}`);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
const schemaConst = this._schema.const;
|
2020-08-15 21:23:09 +00:00
|
|
|
if (typeof schemaConst !== 'undefined' && !this._valuesAreEqual(value, schemaConst)) {
|
2021-05-22 19:45:20 +00:00
|
|
|
throw this._createError('Invalid constant value');
|
2020-08-11 23:21:26 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
const schemaEnum = this._schema.enum;
|
2020-08-15 21:23:09 +00:00
|
|
|
if (Array.isArray(schemaEnum) && !this._valuesAreEqualAny(value, schemaEnum)) {
|
2021-05-22 19:45:20 +00:00
|
|
|
throw this._createError('Invalid enum value');
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 'number':
|
2021-05-22 19:45:20 +00:00
|
|
|
this._validateNumber(value);
|
2020-02-02 05:13:46 +00:00
|
|
|
break;
|
2019-11-28 17:19:15 +00:00
|
|
|
case 'string':
|
2021-05-22 19:45:20 +00:00
|
|
|
this._validateString(value);
|
2020-02-02 05:13:46 +00:00
|
|
|
break;
|
2019-11-28 17:19:15 +00:00
|
|
|
case 'array':
|
2021-05-22 19:45:20 +00:00
|
|
|
this._validateArray(value);
|
2020-02-02 05:13:46 +00:00
|
|
|
break;
|
2019-11-28 17:19:15 +00:00
|
|
|
case 'object':
|
2021-05-22 19:45:20 +00:00
|
|
|
this._validateObject(value);
|
2020-02-02 05:13:46 +00:00
|
|
|
break;
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
_validateNumber(value) {
|
|
|
|
const {multipleOf} = this._schema;
|
2019-11-28 17:19:15 +00:00
|
|
|
if (typeof multipleOf === 'number' && Math.floor(value / multipleOf) * multipleOf !== value) {
|
2021-05-22 19:45:20 +00:00
|
|
|
throw this._createError(`Number is not a multiple of ${multipleOf}`);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
const {minimum} = this._schema;
|
2019-11-28 17:19:15 +00:00
|
|
|
if (typeof minimum === 'number' && value < minimum) {
|
2021-05-22 19:45:20 +00:00
|
|
|
throw this._createError(`Number is less than ${minimum}`);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
const {exclusiveMinimum} = this._schema;
|
2019-11-28 17:19:15 +00:00
|
|
|
if (typeof exclusiveMinimum === 'number' && value <= exclusiveMinimum) {
|
2021-05-22 19:45:20 +00:00
|
|
|
throw this._createError(`Number is less than or equal to ${exclusiveMinimum}`);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
const {maximum} = this._schema;
|
2019-11-28 17:19:15 +00:00
|
|
|
if (typeof maximum === 'number' && value > maximum) {
|
2021-05-22 19:45:20 +00:00
|
|
|
throw this._createError(`Number is greater than ${maximum}`);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
const {exclusiveMaximum} = this._schema;
|
2019-11-28 17:19:15 +00:00
|
|
|
if (typeof exclusiveMaximum === 'number' && value >= exclusiveMaximum) {
|
2021-05-22 19:45:20 +00:00
|
|
|
throw this._createError(`Number is greater than or equal to ${exclusiveMaximum}`);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
_validateString(value) {
|
|
|
|
const {minLength} = this._schema;
|
2019-11-28 17:19:15 +00:00
|
|
|
if (typeof minLength === 'number' && value.length < minLength) {
|
2021-05-22 19:45:20 +00:00
|
|
|
throw this._createError('String length too short');
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
const {maxLength} = this._schema;
|
2019-11-28 17:19:15 +00:00
|
|
|
if (typeof maxLength === 'number' && value.length > maxLength) {
|
2021-05-22 19:45:20 +00:00
|
|
|
throw this._createError('String length too long');
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
2020-08-09 18:18:59 +00:00
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
const {pattern} = this._schema;
|
2020-08-09 18:18:59 +00:00
|
|
|
if (typeof pattern === 'string') {
|
2021-05-22 19:45:20 +00:00
|
|
|
let {patternFlags} = this._schema;
|
2020-08-09 18:18:59 +00:00
|
|
|
if (typeof patternFlags !== 'string') { patternFlags = ''; }
|
|
|
|
|
|
|
|
let regex;
|
|
|
|
try {
|
|
|
|
regex = this._getRegex(pattern, patternFlags);
|
|
|
|
} catch (e) {
|
2021-05-22 19:45:20 +00:00
|
|
|
throw this._createError(`Pattern is invalid (${e.message})`);
|
2020-08-09 18:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!regex.test(value)) {
|
2021-05-22 19:45:20 +00:00
|
|
|
throw this._createError('Pattern match failed');
|
2020-08-09 18:18:59 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
_validateArray(value) {
|
|
|
|
const {length} = value;
|
|
|
|
|
|
|
|
const {minItems} = this._schema;
|
|
|
|
if (typeof minItems === 'number' && length < minItems) {
|
|
|
|
throw this._createError('Array length too short');
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
const {maxItems} = this._schema;
|
|
|
|
if (typeof maxItems === 'number' && length > maxItems) {
|
|
|
|
throw this._createError('Array length too long');
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
this._validateArrayContains(value);
|
2020-08-11 23:21:26 +00:00
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
for (let i = 0; i < length; ++i) {
|
2021-05-22 22:01:16 +00:00
|
|
|
const schemaInfo = this._getArrayItemSchemaInfo(i);
|
2021-05-22 21:56:44 +00:00
|
|
|
if (schemaInfo === null) {
|
2021-05-22 19:45:20 +00:00
|
|
|
throw this._createError(`No schema found for array[${i}]`);
|
2020-01-26 17:34:27 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 15:47:30 +00:00
|
|
|
const propertyValue = value[i];
|
|
|
|
|
2021-05-22 21:56:44 +00:00
|
|
|
this._schemaPush(schemaInfo.schema, schemaInfo.path);
|
|
|
|
this._valuePush(propertyValue, i);
|
2021-05-22 19:45:20 +00:00
|
|
|
try {
|
|
|
|
this._validate(propertyValue);
|
|
|
|
} finally {
|
|
|
|
this._valuePop();
|
2021-05-22 21:56:44 +00:00
|
|
|
this._schemaPop();
|
2021-05-22 19:45:20 +00:00
|
|
|
}
|
2020-01-26 17:34:27 +00:00
|
|
|
}
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
_validateArrayContains(value) {
|
|
|
|
const containsSchema = this._schema.contains;
|
2020-08-15 21:23:09 +00:00
|
|
|
if (!this._isObject(containsSchema)) { return; }
|
2020-08-11 23:21:26 +00:00
|
|
|
|
2021-05-22 21:56:44 +00:00
|
|
|
this._schemaPush(containsSchema, 'contains');
|
2021-05-22 19:45:20 +00:00
|
|
|
try {
|
|
|
|
for (let i = 0, ii = value.length; i < ii; ++i) {
|
|
|
|
const propertyValue = value[i];
|
2021-05-22 21:56:44 +00:00
|
|
|
this._valuePush(propertyValue, i);
|
2021-05-22 19:45:20 +00:00
|
|
|
try {
|
|
|
|
this._validate(propertyValue);
|
|
|
|
return;
|
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
|
|
|
} finally {
|
|
|
|
this._valuePop();
|
|
|
|
}
|
2020-08-11 23:21:26 +00:00
|
|
|
}
|
2021-05-22 19:45:20 +00:00
|
|
|
throw this._createError('contains schema didn\'t match');
|
|
|
|
} finally {
|
|
|
|
this._schemaPop();
|
2020-08-11 23:21:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
_validateObject(value) {
|
2019-11-28 17:19:15 +00:00
|
|
|
const properties = new Set(Object.getOwnPropertyNames(value));
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
const {required} = this._schema;
|
2019-11-28 17:19:15 +00:00
|
|
|
if (Array.isArray(required)) {
|
|
|
|
for (const property of required) {
|
|
|
|
if (!properties.has(property)) {
|
2021-05-22 19:45:20 +00:00
|
|
|
throw this._createError(`Missing property ${property}`);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
const {minProperties} = this._schema;
|
2019-11-28 17:19:15 +00:00
|
|
|
if (typeof minProperties === 'number' && properties.length < minProperties) {
|
2021-05-22 19:45:20 +00:00
|
|
|
throw this._createError('Not enough object properties');
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
const {maxProperties} = this._schema;
|
2019-11-28 17:19:15 +00:00
|
|
|
if (typeof maxProperties === 'number' && properties.length > maxProperties) {
|
2021-05-22 19:45:20 +00:00
|
|
|
throw this._createError('Too many object properties');
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const property of properties) {
|
2021-05-22 22:01:16 +00:00
|
|
|
const schemaInfo = this._getObjectPropertySchemaInfo(property);
|
2021-05-22 21:56:44 +00:00
|
|
|
if (schemaInfo === null) {
|
2021-05-22 19:45:20 +00:00
|
|
|
throw this._createError(`No schema found for ${property}`);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
2020-02-02 15:47:30 +00:00
|
|
|
|
|
|
|
const propertyValue = value[property];
|
|
|
|
|
2021-05-22 21:56:44 +00:00
|
|
|
this._schemaPush(schemaInfo.schema, schemaInfo.path);
|
|
|
|
this._valuePush(propertyValue, property);
|
2021-05-22 19:45:20 +00:00
|
|
|
try {
|
|
|
|
this._validate(propertyValue);
|
|
|
|
} finally {
|
|
|
|
this._valuePop();
|
2021-05-22 21:56:44 +00:00
|
|
|
this._schemaPop();
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
// Creation
|
2019-11-28 17:19:15 +00:00
|
|
|
|
2020-08-15 21:23:09 +00:00
|
|
|
_getDefaultTypeValue(type) {
|
2019-11-28 17:19:15 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
_getDefaultSchemaValue() {
|
|
|
|
const {type: schemaType, default: schemaDefault} = this._schema;
|
2020-11-30 23:56:28 +00:00
|
|
|
return (
|
|
|
|
typeof schemaDefault !== 'undefined' &&
|
|
|
|
this._isValueTypeAny(schemaDefault, this._getValueType(schemaDefault), schemaType) ?
|
|
|
|
clone(schemaDefault) :
|
|
|
|
this._getDefaultTypeValue(schemaType)
|
|
|
|
);
|
|
|
|
}
|
2020-10-27 23:27:27 +00:00
|
|
|
|
2021-05-22 21:56:44 +00:00
|
|
|
_getValidValueOrDefault(path, value, schemaInfo) {
|
2021-05-23 19:49:25 +00:00
|
|
|
schemaInfo = this._getResolveSchemaInfo(schemaInfo);
|
2021-05-22 21:56:44 +00:00
|
|
|
this._schemaPush(schemaInfo.schema, schemaInfo.path);
|
|
|
|
this._valuePush(value, path);
|
2021-05-22 19:45:20 +00:00
|
|
|
try {
|
|
|
|
return this._getValidValueOrDefaultInner(value);
|
|
|
|
} finally {
|
|
|
|
this._valuePop();
|
2021-05-22 21:56:44 +00:00
|
|
|
this._schemaPop();
|
2021-05-22 19:45:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_getValidValueOrDefaultInner(value) {
|
2020-11-30 23:56:28 +00:00
|
|
|
let type = this._getValueType(value);
|
2021-05-22 19:45:20 +00:00
|
|
|
if (typeof value === 'undefined' || !this._isValueTypeAny(value, type, this._schema.type)) {
|
|
|
|
value = this._getDefaultSchemaValue();
|
2020-11-30 23:56:28 +00:00
|
|
|
type = this._getValueType(value);
|
2020-10-27 23:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 'object':
|
2021-05-22 19:45:20 +00:00
|
|
|
value = this._populateObjectDefaults(value);
|
2020-10-27 23:27:27 +00:00
|
|
|
break;
|
|
|
|
case 'array':
|
2021-05-22 19:45:20 +00:00
|
|
|
value = this._populateArrayDefaults(value);
|
2020-10-27 23:27:27 +00:00
|
|
|
break;
|
2020-11-30 23:56:28 +00:00
|
|
|
default:
|
2021-05-22 19:45:20 +00:00
|
|
|
if (!this._isValidCurrent(value)) {
|
|
|
|
const schemaDefault = this._getDefaultSchemaValue();
|
|
|
|
if (this._isValidCurrent(schemaDefault)) {
|
2020-11-30 23:56:28 +00:00
|
|
|
value = schemaDefault;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2020-10-27 23:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
_populateObjectDefaults(value) {
|
2019-11-28 17:19:15 +00:00
|
|
|
const properties = new Set(Object.getOwnPropertyNames(value));
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
const {required} = this._schema;
|
2019-11-28 17:19:15 +00:00
|
|
|
if (Array.isArray(required)) {
|
|
|
|
for (const property of required) {
|
|
|
|
properties.delete(property);
|
2021-05-22 22:01:16 +00:00
|
|
|
const schemaInfo = this._getObjectPropertySchemaInfo(property);
|
2021-05-22 21:56:44 +00:00
|
|
|
if (schemaInfo === null) { continue; }
|
2021-05-22 19:45:20 +00:00
|
|
|
const propertyValue = Object.prototype.hasOwnProperty.call(value, property) ? value[property] : void 0;
|
2021-05-22 21:56:44 +00:00
|
|
|
value[property] = this._getValidValueOrDefault(property, propertyValue, schemaInfo);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const property of properties) {
|
2021-05-22 22:01:16 +00:00
|
|
|
const schemaInfo = this._getObjectPropertySchemaInfo(property);
|
2021-05-22 21:56:44 +00:00
|
|
|
if (schemaInfo === null) {
|
2019-11-28 17:19:15 +00:00
|
|
|
Reflect.deleteProperty(value, property);
|
|
|
|
} else {
|
2021-05-22 21:56:44 +00:00
|
|
|
value[property] = this._getValidValueOrDefault(property, value[property], schemaInfo);
|
2019-11-28 17:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
2019-12-29 23:57:29 +00:00
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
_populateArrayDefaults(value) {
|
2019-12-29 23:57:29 +00:00
|
|
|
for (let i = 0, ii = value.length; i < ii; ++i) {
|
2021-05-22 22:01:16 +00:00
|
|
|
const schemaInfo = this._getArrayItemSchemaInfo(i);
|
2021-05-22 21:56:44 +00:00
|
|
|
if (schemaInfo === null) { continue; }
|
2021-05-22 19:45:20 +00:00
|
|
|
const propertyValue = value[i];
|
2021-05-22 21:56:44 +00:00
|
|
|
value[i] = this._getValidValueOrDefault(i, propertyValue, schemaInfo);
|
2019-12-29 23:57:29 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
const {minItems, maxItems} = this._schema;
|
2020-09-13 23:59:02 +00:00
|
|
|
if (typeof minItems === 'number' && value.length < minItems) {
|
|
|
|
for (let i = value.length; i < minItems; ++i) {
|
2021-05-22 22:01:16 +00:00
|
|
|
const schemaInfo = this._getArrayItemSchemaInfo(i);
|
2021-05-22 21:56:44 +00:00
|
|
|
if (schemaInfo === null) { break; }
|
|
|
|
const item = this._getValidValueOrDefault(i, void 0, schemaInfo);
|
2020-09-13 23:59:02 +00:00
|
|
|
value.push(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof maxItems === 'number' && value.length > maxItems) {
|
|
|
|
value.splice(maxItems, value.length - maxItems);
|
|
|
|
}
|
|
|
|
|
2019-12-29 23:57:29 +00:00
|
|
|
return value;
|
|
|
|
}
|
2021-05-22 19:45:20 +00:00
|
|
|
}
|
2020-01-26 16:06:03 +00:00
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
class JsonSchemaProxyHandler {
|
|
|
|
constructor(schema) {
|
|
|
|
this._schema = schema;
|
|
|
|
this._numberPattern = /^(?:0|[1-9]\d*)$/;
|
2020-01-26 16:06:03 +00:00
|
|
|
}
|
2020-08-09 18:18:59 +00:00
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
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]; }
|
|
|
|
|
|
|
|
let propertySchema;
|
|
|
|
if (Array.isArray(target)) {
|
2021-05-24 22:53:42 +00:00
|
|
|
const index = this._getArrayIndex(property);
|
|
|
|
if (index === null) {
|
2021-05-22 19:45:20 +00:00
|
|
|
// Note: this does not currently wrap mutating functions like push, pop, shift, unshift, splice
|
|
|
|
return target[property];
|
|
|
|
}
|
2021-05-24 22:53:42 +00:00
|
|
|
property = index;
|
2021-05-22 19:45:20 +00:00
|
|
|
propertySchema = this._schema.getArrayItemSchema(property);
|
|
|
|
} else {
|
|
|
|
propertySchema = this._schema.getObjectPropertySchema(property);
|
2021-01-22 03:49:54 +00:00
|
|
|
}
|
2021-05-22 19:45:20 +00:00
|
|
|
|
|
|
|
if (propertySchema === null) { return void 0; }
|
|
|
|
|
|
|
|
const value = target[property];
|
|
|
|
return value !== null && typeof value === 'object' ? propertySchema.createProxy(value) : value;
|
2020-08-09 18:18:59 +00:00
|
|
|
}
|
2019-11-28 17:19:15 +00:00
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
set(target, property, value) {
|
|
|
|
if (typeof property === 'symbol') { throw new Error(`Cannot assign symbol property ${property}`); }
|
|
|
|
|
|
|
|
let propertySchema;
|
|
|
|
if (Array.isArray(target)) {
|
|
|
|
property = this._getArrayIndex(property);
|
|
|
|
if (property === null) { throw new Error(`Property ${property} cannot be assigned to array`); }
|
|
|
|
if (property > target.length) { throw new Error('Array index out of range'); }
|
|
|
|
propertySchema = this._schema.getArrayItemSchema(property);
|
|
|
|
} else {
|
|
|
|
propertySchema = this._schema.getObjectPropertySchema(property);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (propertySchema === null) { throw new Error(`Property ${property} not supported`); }
|
|
|
|
|
|
|
|
value = clone(value);
|
|
|
|
propertySchema.validate(value);
|
2020-01-26 20:45:31 +00:00
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
target[property] = value;
|
|
|
|
return true;
|
2020-02-02 15:47:30 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
deleteProperty(target, property) {
|
|
|
|
const required = (
|
|
|
|
(typeof target === 'object' && target !== null) ?
|
|
|
|
(Array.isArray(target) || this._schema.isObjectPropertyRequired(property)) :
|
|
|
|
true
|
|
|
|
);
|
|
|
|
if (required) {
|
|
|
|
throw new Error(`${property} cannot be deleted`);
|
|
|
|
}
|
|
|
|
return Reflect.deleteProperty(target, property);
|
2020-02-02 15:47:30 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
ownKeys(target) {
|
|
|
|
return Reflect.ownKeys(target);
|
2020-02-02 15:47:30 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
apply() {
|
|
|
|
throw new Error('apply not supported');
|
2020-02-02 15:47:30 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
construct() {
|
|
|
|
throw new Error('construct not supported');
|
2020-02-02 15:47:30 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:45:20 +00:00
|
|
|
// Private
|
|
|
|
|
|
|
|
_getArrayIndex(property) {
|
|
|
|
if (typeof property === 'string' && this._numberPattern.test(property)) {
|
|
|
|
return Number.parseInt(property, 10);
|
|
|
|
} else if (typeof property === 'number' && Math.floor(property) === property && property >= 0) {
|
|
|
|
return property;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2020-02-02 05:13:46 +00:00
|
|
|
}
|
|
|
|
}
|