UNPKG

dilswer

Version:

Blazingly fast data validation library with TypeScript integration.

94 lines (92 loc) 2.66 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); // src/data-types/types/recursive.ts import { BaseType } from "../base-type.mjs"; import { getStandardSchemaProps } from "../generate-standard-schema.mjs"; var validatedCircularValues = /* @__PURE__ */ new Map(); var wasCircValidated = (type, data) => { let set = validatedCircularValues.get(type); if (!set) { set = /* @__PURE__ */ new Set([data]); validatedCircularValues.set(type, set); return false; } if (set.has(data)) { return true; } set.add(data); return false; }; var RecursiveTypeReference = class extends BaseType { constructor(parent) { super(); __publicField(this, "parent", parent); __publicField(this, "kind", "circularRef"); } /** This is the type this reference points to. */ get type() { return this.parent.type; } /** @internal */ _getReferencedType() { return this.parent.type; } _acceptVisitor(visitor, depth = 1) { return visitor.visit(this, void 0, depth); } ["~validate"](path, value) { if (wasCircValidated(this._getReferencedType(), value)) { return; } const refType = this._getReferencedType(); return refType["~validate"](path, value); } ["~matches"](value) { if (wasCircValidated(this._getReferencedType(), value)) { return true; } const refType = this._getReferencedType(); return refType["~matches"](value); } toString() { return `RecursiveRefSchema[]`; } }; var RecursiveType = class extends BaseType { constructor(getDataType) { super(); __publicField(this, "kind", "circular"); __publicField(this, "type"); this.type = getDataType(new RecursiveTypeReference(this)); } /** @internal */ _acceptVisitor(visitor, depth = 1) { const c = this.type._acceptVisitor(visitor, depth); return visitor.visit(this, [c], depth); } get ["~standard"]() { return getStandardSchemaProps(this); } ["~validate"](path, value) { if (wasCircValidated(this, value)) { return; } return this.type["~validate"](path, value); } ["~matches"](value) { if (wasCircValidated(this, value)) { return true; } return this.type["~matches"](value); } toString() { return `RecursiveSchema[ ${this.type.toString()} ]`; } }; export { RecursiveType, RecursiveTypeReference, validatedCircularValues, wasCircValidated };