dilswer
Version:
Blazingly fast data validation library with TypeScript integration.
59 lines (57 loc) • 1.96 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/set.ts
import { BaseType } from "../base-type.mjs";
import { getStandardSchemaProps } from "../generate-standard-schema.mjs";
import { UnionType } from "./union.mjs";
import { ValidationError } from "../../validation-algorithms/validation-error/validation-error.mjs";
var SetType = class extends BaseType {
constructor(setOf) {
super();
__publicField(this, "setOf", setOf);
__publicField(this, "kind", "set");
__publicField(this, "union");
this.union = new UnionType(setOf);
Object.freeze(this);
}
/** @internal */
_acceptVisitor(visitor, depth = 1) {
const children = [];
for (let i = 0; i < this.setOf.length; i++) {
children.push(this.setOf[i]._acceptVisitor(visitor, depth + 1));
}
return visitor.visit(this, children, depth);
}
get ["~standard"]() {
return getStandardSchemaProps(this);
}
["~validate"](path, value) {
if (typeof value !== "object" || value === null) {
throw new ValidationError(path, this, value, "not an object");
}
if (value[Symbol.toStringTag] !== "Set") {
throw new ValidationError(path, this, value, "not a Set instance");
}
for (const elem of value) {
this.union["~validate"](path.concat("SET_ELEMENT"), elem);
}
}
["~matches"](value) {
if (typeof value !== "object" || value === null || value[Symbol.toStringTag] !== "Set") {
return false;
}
for (const elem of value) {
if (!this.union["~matches"](elem)) {
return false;
}
}
return true;
}
toString() {
return `SetSchema[ ${this.union.toString()} ]`;
}
};
export {
SetType
};