UNPKG

dilswer

Version:

Blazingly fast data validation library with TypeScript integration.

57 lines (55 loc) 1.96 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/dict.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 DictType = class extends BaseType { constructor(dict) { super(); this.dict = dict; __publicField(this, "kind", "dictionary"); __publicField(this, "union"); this.union = new UnionType(this.dict); Object.freeze(this.dict); Object.freeze(this); } /** @internal */ _acceptVisitor(visitor) { const children = []; for (let i = 0; i < this.dict.length; i++) { children.push(this.dict[i]._acceptVisitor(visitor)); } return visitor.visit(this, children); } get ["~standard"]() { return getStandardSchemaProps(this); } ["~validate"](path, value) { if (typeof value !== "object" || value === null || Array.isArray(value)) { throw new ValidationError(path, this, value, "not an object"); } const keys = Object.keys(value); for (let index = 0; index < keys.length; index++) { const key = keys[index]; this.union["~validate"](path.concat(key), value[key]); } } ["~matches"](value) { if (typeof value !== "object" || value === null || Array.isArray(value)) { return false; } const keys = Object.keys(value); for (let index = 0; index < keys.length; index++) { const key = keys[index]; const match = this.union["~matches"](value[key]); if (!match) return false; } return true; } }; export { DictType };