UNPKG

dilswer

Version:

Blazingly fast data validation library with TypeScript integration.

93 lines (91 loc) 3.26 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); // src/data-types/types/union.ts var union_exports = {}; __export(union_exports, { UnionType: () => UnionType }); module.exports = __toCommonJS(union_exports); var import_base_type = require("../base-type.cjs"); var import_generate_standard_schema = require("../generate-standard-schema.cjs"); var import_validation_error = require("../../validation-algorithms/validation-error/validation-error.cjs"); var UnionType = class extends import_base_type.BaseType { constructor(oneOf) { super(); __publicField(this, "oneOf", oneOf); __publicField(this, "kind", "union"); Object.freeze(this.oneOf); Object.freeze(this); } /** @internal */ _acceptVisitor(visitor, depth = 1) { const children = []; for (let i = 0; i < this.oneOf.length; i++) { children.push(this.oneOf[i]._acceptVisitor(visitor, depth)); } return visitor.visit(this, children, depth); } get ["~standard"]() { return (0, import_generate_standard_schema.getStandardSchemaProps)(this); } ["~validate"](path, value) { if (this.oneOf.length === 1) { const oneOfType = this.oneOf[0]; return oneOfType["~validate"](path, value); } const validationErrors = []; for (let i = 0; i < this.oneOf.length; i++) { const oneOfType = this.oneOf[i]; try { oneOfType["~validate"](path, value); return; } catch (err) { const typedErr = err; typedErr.originType = oneOfType; validationErrors.push(typedErr); } } throw new import_validation_error.AggregateValidationError( path, validationErrors, "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; } toString() { if (this.oneOf.length === 1) { return this.oneOf[0].toString(); } return `UnionSchema[ ${this.oneOf.map((t) => t.toString()).join(" | ")} ]`; } };