dilswer
Version:
Blazingly fast data validation library with TypeScript integration.
88 lines (86 loc) • 2.46 kB
JavaScript
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();
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) {
return visitor.visit(this);
}
["~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);
}
};
var RecursiveType = class extends BaseType {
constructor(getDataType) {
super();
__publicField(this, "kind", "circular");
__publicField(this, "type");
this.type = getDataType(new RecursiveTypeReference(this));
}
/** @internal */
_acceptVisitor(visitor) {
const c = this.type._acceptVisitor(visitor);
return visitor.visit(this, [c]);
}
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);
}
};
export {
RecursiveType,
RecursiveTypeReference,
validatedCircularValues,
wasCircValidated
};