@sinclair/typebox
Version:
JSONSchema Type Builder with Static Type Resolution for TypeScript
337 lines (329 loc) • 14.8 kB
JavaScript
;
/*--------------------------------------------------------------------------
@sinclair/typebox/compiler
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.TypeCompiler = exports.TypeCheck = void 0;
const errors_1 = require("./errors");
const Types = require("../typebox");
// -------------------------------------------------------------------
// TypeCheck
// -------------------------------------------------------------------
class TypeCheck {
schema;
additional;
checkFunc;
code;
constructor(schema, additional, checkFunc, code) {
this.schema = schema;
this.additional = additional;
this.checkFunc = checkFunc;
this.code = code;
}
/** Returns the generated validation code used to validate this type */
Code() {
return this.code;
}
/** Returns an iterator for each type error found in this value */
Errors(value) {
return errors_1.TypeErrors.Errors(this.schema, this.additional, value);
}
/** Returns true if the value matches the given type. */
Check(value) {
return this.checkFunc(value);
}
}
exports.TypeCheck = TypeCheck;
// -------------------------------------------------------------------
// TypeCompiler
// -------------------------------------------------------------------
var TypeCompiler;
(function (TypeCompiler) {
// -------------------------------------------------------------------
// Schemas
// -------------------------------------------------------------------
function* Any(schema, path) {
yield '(true)';
}
function* Array(schema, path) {
const expr = [...Visit(schema.items, `value`)].map((condition) => condition).join(' && ');
yield `(Array.isArray(${path}) && ${path}.every(value => ${expr}))`;
}
function* Boolean(schema, path) {
yield `(typeof ${path} === 'boolean')`;
}
function* Constructor(schema, path) {
yield* Visit(schema.yields, path);
}
function* Function(schema, path) {
yield `(typeof ${path} === 'function')`;
}
function* Integer(schema, path) {
yield `(typeof ${path} === 'number' && Number.isInteger(${path}))`;
if (schema.multipleOf)
yield `(${path} % ${schema.multipleOf} === 0)`;
if (schema.exclusiveMinimum)
yield `(${path} > ${schema.exclusiveMinimum})`;
if (schema.exclusiveMaximum)
yield `(${path} < ${schema.exclusiveMaximum})`;
if (schema.minimum)
yield `(${path} >= ${schema.minimum})`;
if (schema.maximum)
yield `(${path} <= ${schema.maximum})`;
}
function* Literal(schema, path) {
if (typeof schema.const === 'string') {
yield `(${path} === '${schema.const}')`;
}
else {
yield `(${path} === ${schema.const})`;
}
}
function* Null(schema, path) {
yield `(${path} === null)`;
}
function* Number(schema, path) {
yield `(typeof ${path} === 'number')`;
if (schema.multipleOf)
yield `(${path} % ${schema.multipleOf} === 0)`;
if (schema.exclusiveMinimum)
yield `(${path} > ${schema.exclusiveMinimum})`;
if (schema.exclusiveMaximum)
yield `(${path} < ${schema.exclusiveMaximum})`;
if (schema.minimum)
yield `(${path} >= ${schema.minimum})`;
if (schema.maximum)
yield `(${path} <= ${schema.maximum})`;
}
function* Object(schema, path) {
yield `(typeof ${path} === 'object' && ${path} !== null && !Array.isArray(${path}))`;
if (schema.minProperties !== undefined)
yield `(Object.keys(${path}).length >= ${schema.minProperties})`;
if (schema.maxProperties !== undefined)
yield `(Object.keys(${path}).length <= ${schema.maxProperties})`;
const propertyKeys = globalThis.Object.keys(schema.properties);
if (schema.additionalProperties === false) {
// optimization: If the property key length matches the required keys length
// then we only need check that the values property key length matches that
// of the property key length. This is because exhaustive testing for values
// will occur in subsequent property tests.
if (schema.required && schema.required.length === propertyKeys.length) {
yield `(Object.keys(${path}).length === ${propertyKeys.length})`;
}
else {
const keys = `[${propertyKeys.map((key) => `'${key}'`).join(', ')}]`;
yield `(Object.keys(${path}).every(key => ${keys}.includes(key)))`;
}
}
for (const propertyKey of propertyKeys) {
const propertySchema = schema.properties[propertyKey];
if (schema.required && schema.required.includes(propertyKey)) {
yield* Visit(propertySchema, `${path}.${propertyKey}`);
}
else {
const expr = [...Visit(propertySchema, `${path}.${propertyKey}`)].map((condition) => condition).join(' && ');
yield `(${path}.${propertyKey} === undefined ? true : (${expr}))`;
}
}
}
function* Promise(schema, path) {
yield `(typeof value === 'object' && typeof ${path}.then === 'function')`;
}
function* Record(schema, path) {
yield `(typeof ${path} === 'object' && ${path} !== null && !Array.isArray(${path}))`;
const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
const local = PushLocal(`const local = new RegExp(/${keyPattern}/)`);
yield `(Object.keys(${path}).every(key => ${local}.test(key)))`;
const expr = [...Visit(valueSchema, 'value')].map((condition) => condition).join(' && ');
yield `(Object.values(${path}).every(value => ${expr}))`;
}
function* Ref(schema, path) {
// reference: referenced schemas can originate from either additional
// schemas or inline in the schema itself. Ideally the recursive
// path should align to reference path. Consider for review.
if (!functionNames.has(schema.$ref)) {
const reference = referenceMap.get(schema.$ref);
functionNames.add(schema.$ref);
const conditions = [...Visit(reference, 'value')];
const name = CreateFunctionName(schema.$ref);
const body = CreateFunction(name, conditions);
PushLocal(body);
}
const func = CreateFunctionName(schema.$ref);
yield `(${func}(${path}))`;
}
function* Self(schema, path) {
const func = CreateFunctionName(schema.$ref);
yield `(${func}(${path}))`;
}
function* String(schema, path) {
yield `(typeof ${path} === 'string')`;
if (schema.pattern !== undefined) {
const local = PushLocal(`const local = new RegExp('${schema.pattern}');`);
yield `(${local}.test(${path}))`;
}
}
function* Tuple(schema, path) {
yield `(Array.isArray(${path}))`;
if (schema.items === undefined)
return yield `(${path}.length === 0)`;
yield `(${path}.length === ${schema.maxItems})`;
for (let i = 0; i < schema.items.length; i++) {
const expr = [...Visit(schema.items[i], `${path}[${i}]`)].map((condition) => condition).join(' && ');
yield `(${expr})`;
}
}
function* Undefined(schema, path) {
yield `${path} === undefined`;
}
function* Union(schema, path) {
const exprs = schema.anyOf.map((schema) => [...Visit(schema, path)].map((condition) => condition).join(' && '));
yield `(${exprs.join(' || ')})`;
}
function* Uint8Array(schema, path) {
yield `(${path} instanceof Uint8Array)`;
if (schema.maxByteLength)
yield `(${path}.length <= ${schema.maxByteLength})`;
if (schema.minByteLength)
yield `(${path}.length >= ${schema.minByteLength})`;
}
function* Unknown(schema, path) {
yield '(true)';
}
function* Void(schema, path) {
yield `(${path} === null)`;
}
function* Visit(schema, path) {
// reference: referenced schemas can originate from either additional
// schemas or inline in the schema itself. Ideally the recursive
// path should align to reference path. Consider for review.
if (schema.$id && !functionNames.has(schema.$id)) {
functionNames.add(schema.$id);
const conditions = [...Visit(schema, 'value')];
const name = CreateFunctionName(schema.$id);
const body = CreateFunction(name, conditions);
PushLocal(body);
yield `(${name}(${path}))`;
return;
}
const anySchema = schema;
switch (anySchema[Types.Kind]) {
case 'Any':
return yield* Any(anySchema, path);
case 'Array':
return yield* Array(anySchema, path);
case 'Boolean':
return yield* Boolean(anySchema, path);
case 'Constructor':
return yield* Constructor(anySchema, path);
case 'Function':
return yield* Function(anySchema, path);
case 'Integer':
return yield* Integer(anySchema, path);
case 'Literal':
return yield* Literal(anySchema, path);
case 'Null':
return yield* Null(anySchema, path);
case 'Number':
return yield* Number(anySchema, path);
case 'Object':
return yield* Object(anySchema, path);
case 'Promise':
return yield* Promise(anySchema, path);
case 'Record':
return yield* Record(anySchema, path);
case 'Ref':
return yield* Ref(anySchema, path);
case 'Self':
return yield* Self(anySchema, path);
case 'String':
return yield* String(anySchema, path);
case 'Tuple':
return yield* Tuple(anySchema, path);
case 'Undefined':
return yield* Undefined(anySchema, path);
case 'Union':
return yield* Union(anySchema, path);
case 'Uint8Array':
return yield* Uint8Array(anySchema, path);
case 'Unknown':
return yield* Unknown(anySchema, path);
case 'Void':
return yield* Void(anySchema, path);
default:
throw Error(`Unknown schema kind '${schema[Types.Kind]}'`);
}
}
// -------------------------------------------------------------------
// Locals
// -------------------------------------------------------------------
const referenceMap = new Map();
const functionLocals = new Set();
const functionNames = new Set();
function ClearLocals() {
functionLocals.clear();
functionNames.clear();
referenceMap.clear();
}
function PushReferences(schemas = []) {
for (const schema of schemas) {
if (!schema.$id)
throw Error(`Referenced schemas must specify an $id. Failed for '${JSON.stringify(schema)}'`);
if (referenceMap.has(schema.$id))
throw Error(`Duplicate schema $id detected for '${schema.$id}'`);
referenceMap.set(schema.$id, schema);
}
}
function PushLocal(code) {
const name = `local${functionLocals.size}`;
functionLocals.add(code.replace('local', name));
return name;
}
function GetLocals() {
return [...functionLocals.values()];
}
// -------------------------------------------------------------------
// Functions
// -------------------------------------------------------------------
function CreateFunctionName($id) {
return `check_${$id.replace(/-/g, '_')}`;
}
function CreateFunction(name, conditions) {
const expression = conditions.map((condition) => ` ${condition}`).join(' &&\n');
return `function ${name}(value) {\n return (\n${expression}\n )\n}`;
}
// -------------------------------------------------------------------
// Compile
// -------------------------------------------------------------------
function Build(schema, additional = []) {
ClearLocals();
PushReferences(additional);
const conditions = [...Visit(schema, 'value')]; // locals populated during yield
const locals = GetLocals();
return `${locals.join('\n')}\nreturn ${CreateFunction('check', conditions)}`;
}
/** Compiles the given type for runtime type checking. This compiler only accepts known TypeBox types non-inclusive of unsafe types. */
function Compile(schema, additional = []) {
const code = Build(schema, additional);
const func = globalThis.Function(code);
return new TypeCheck(schema, additional, func(), code);
}
TypeCompiler.Compile = Compile;
})(TypeCompiler = exports.TypeCompiler || (exports.TypeCompiler = {}));