UNPKG

dilswer

Version:

Blazingly fast data validation library with TypeScript integration.

75 lines (73 loc) 2.84 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/set.ts var set_exports = {}; __export(set_exports, { SetType: () => SetType }); module.exports = __toCommonJS(set_exports); var import_base_type = require("../base-type.js"); var import_generate_standard_schema = require("../generate-standard-schema.js"); var import_union = require("./union.js"); var import_validation_error = require("../../validation-algorithms/validation-error/validation-error.js"); var SetType = class extends import_base_type.BaseType { constructor(setOf) { super(); this.setOf = setOf; __publicField(this, "kind", "set"); __publicField(this, "union"); this.union = new import_union.UnionType(setOf); Object.freeze(this); } /** @internal */ _acceptVisitor(visitor) { const children = []; for (let i = 0; i < this.setOf.length; i++) { children.push(this.setOf[i]._acceptVisitor(visitor)); } return visitor.visit(this, children); } get ["~standard"]() { return (0, import_generate_standard_schema.getStandardSchemaProps)(this); } ["~validate"](path, value) { if (typeof value !== "object" || value === null) { throw new import_validation_error.ValidationError(path, this, value, "not an object"); } if (value[Symbol.toStringTag] !== "Set") { throw new import_validation_error.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; } };