@sinclair/typebox
Version:
JSONSchema Type Builder with Static Type Resolution for TypeScript
233 lines (225 loc) • 8.83 kB
JavaScript
"use strict";
/*--------------------------------------------------------------------------
@sinclair/typebox/value
The MIT License (MIT)
Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.CheckValue = void 0;
const Types = require("../typebox");
var CheckValue;
(function (CheckValue) {
const referenceMap = new Map();
function Any(schema, value) {
return true;
}
function Array(schema, value) {
if (typeof value !== 'object' || !globalThis.Array.isArray(value))
return false;
for (let i = 0; i < value.length; i++) {
if (!Visit(schema.items, value[i]))
return false;
}
return true;
}
function Boolean(schema, value) {
return typeof value === 'boolean';
}
function Constructor(schema, value) {
return Visit(schema.returns, value.prototype); // check return type only (as an object)
}
function Enum(schema, value) {
for (const subschema of schema.anyOf) {
if (subschema.const === value)
return true;
}
return false;
}
function Function(schema, value) {
return typeof value === 'function';
}
function Integer(schema, value) {
return typeof value === 'number' && globalThis.Number.isInteger(value);
}
function Intersect(schema, value) {
return Object(schema, value);
}
function Literal(schema, value) {
return schema.const === value;
}
function Null(schema, value) {
return value === null;
}
function Number(schema, value) {
return typeof value === 'number';
}
function Object(schema, value) {
if (typeof value !== 'object' || value === null)
return false;
const propertyKeys = globalThis.Object.keys(schema.properties);
if (schema.additionalProperties === false) {
const valueKeys = globalThis.Object.keys(value);
for (const valueKey of valueKeys) {
if (!propertyKeys.includes(valueKey))
return false;
}
}
for (const propertyKey of propertyKeys) {
const propertySchema = schema.properties[propertyKey];
const propertyValue = value[propertyKey];
if (propertyValue === undefined && schema.required !== undefined && !schema.required.includes(propertyKey))
return true;
if (!Visit(propertySchema, propertyValue))
return false;
}
return true;
}
function Promise(schema, value) {
return typeof value === 'object' && typeof value['then'] === 'function';
}
function Record(schema, value) {
if (typeof value !== 'object' || value === null)
return false;
const propertySchema = globalThis.Object.values(schema.patternProperties)[0];
for (const key of globalThis.Object.keys(value)) {
const propertyValue = value[key];
if (!Visit(propertySchema, propertyValue))
return false;
}
return true;
}
function Recursive(schema, value) {
throw new Error('Cannot typeof recursive types');
}
function Ref(schema, value) {
throw new Error('Cannot typeof reference types');
}
function Self(schema, value) {
if (!referenceMap.has(schema.$ref))
throw new Error(`Check: Cannot locate schema with $id '${schema.$id}' for referenced type`);
const referenced = referenceMap.get(schema.$ref);
return Visit(referenced, value);
}
function String(schema, value) {
if (typeof value !== 'string')
return false;
if (schema.pattern !== undefined) {
const regex = new RegExp(schema.pattern);
return value.match(regex) !== null;
}
return true;
}
function Tuple(schema, value) {
if (typeof value !== 'object' || !globalThis.Array.isArray(value))
return false;
if (schema.items === undefined && value.length === 0)
return true;
if (schema.items === undefined)
return false;
if (value.length < schema.minItems || value.length > schema.maxItems)
return false;
for (let i = 0; i < schema.items.length; i++) {
if (!Visit(schema.items[i], value[i]))
return false;
}
return true;
}
function Undefined(schema, value) {
return value === undefined;
}
function Union(schema, value) {
for (let i = 0; i < schema.anyOf.length; i++) {
if (Visit(schema.anyOf[i], value))
return true;
}
return false;
}
function Uint8Array(schema, value) {
return value instanceof globalThis.Uint8Array;
}
function Unknown(schema, value) {
return true;
}
function Void(schema, value) {
return value === null;
}
function Visit(schema, value) {
if (schema.$id !== undefined)
referenceMap.set(schema.$id, schema);
const anySchema = schema;
switch (anySchema[Types.Kind]) {
case 'Any':
return Any(anySchema, value);
case 'Array':
return Array(anySchema, value);
case 'Boolean':
return Boolean(anySchema, value);
case 'Constructor':
return Constructor(anySchema, value);
case 'Enum':
return Enum(anySchema, value);
case 'Function':
return Function(anySchema, value);
case 'Integer':
return Integer(anySchema, value);
case 'Intersect':
return Intersect(anySchema, value);
case 'Literal':
return Literal(anySchema, value);
case 'Null':
return Null(anySchema, value);
case 'Number':
return Number(anySchema, value);
case 'Object':
return Object(anySchema, value);
case 'Promise':
return Promise(anySchema, value);
case 'Record':
return Record(anySchema, value);
case 'Rec':
return Recursive(anySchema, value);
case 'Ref':
return Ref(anySchema, value);
case 'Self':
return Self(anySchema, value);
case 'String':
return String(anySchema, value);
case 'Tuple':
return Tuple(anySchema, value);
case 'Undefined':
return Undefined(anySchema, value);
case 'Union':
return Union(anySchema, value);
case 'Uint8Array':
return Uint8Array(anySchema, value);
case 'Unknown':
return Unknown(anySchema, value);
case 'Void':
return Void(anySchema, value);
default:
throw Error(`Unknown schema kind '${schema[Types.Kind]}'`);
}
}
CheckValue.Visit = Visit;
function Check(schema, value) {
if (referenceMap.size > 0)
referenceMap.clear();
return Visit(schema, value);
}
CheckValue.Check = Check;
})(CheckValue = exports.CheckValue || (exports.CheckValue = {}));