UNPKG

dilswer

Version:

Blazingly fast data validation library with TypeScript integration.

63 lines (61 loc) 1.89 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/union.ts import { BaseType } from "../base-type.mjs"; import { getStandardSchemaProps } from "../generate-standard-schema.mjs"; import { ValidationError } from "../../validation-algorithms/validation-error/validation-error.mjs"; var UnionType = class extends BaseType { constructor(oneOf) { super(); this.oneOf = oneOf; __publicField(this, "kind", "union"); Object.freeze(this.oneOf); Object.freeze(this); } /** @internal */ _acceptVisitor(visitor) { const children = []; for (let i = 0; i < this.oneOf.length; i++) { children.push(this.oneOf[i]._acceptVisitor(visitor)); } return visitor.visit(this, children); } get ["~standard"]() { return getStandardSchemaProps(this); } ["~validate"](path, value) { if (this.oneOf.length === 1) { const oneOfType = this.oneOf[0]; return oneOfType["~validate"](path, value); } for (let i = 0; i < this.oneOf.length; i++) { const oneOfType = this.oneOf[i]; if (oneOfType["~matches"](value)) { return; } } throw new ValidationError( path, this, value, "does not match any of the types in the union" ); } ["~matches"](value) { if (this.oneOf.length === 1) { const oneOfType = this.oneOf[0]; return oneOfType["~matches"](value); } for (let i = 0; i < this.oneOf.length; i++) { const oneOfType = this.oneOf[i]; if (oneOfType["~matches"](value)) { return true; } } return false; } }; export { UnionType };