UNPKG

dilswer

Version:

Blazingly fast data validation library with TypeScript integration.

94 lines (92 loc) 3.41 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/string-matching.ts var string_matching_exports = {}; __export(string_matching_exports, { StringMatchingType: () => StringMatchingType, StringMetadata: () => StringMetadata }); module.exports = __toCommonJS(string_matching_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 StringMetadata = class extends import_base_type.TypeMetadata { /** * Sets the metadata for the TypeScript pattern. This is used * for generating appropriate TypeScript declarations (via * `toTsType()`). * * This value must use the same syntax as the type literal * types in TypeScript. * * @example * const type = * DataType.StringMatching<`${string}.${string}`>( * /^.+\..+$/ * ).setTsPattern("${string}.${string}"); */ tsPattern(name) { this.container.tsPattern = name; return this.type; } }; var _a, _b; var StringMatchingType = class extends (_b = import_base_type.BaseType, _a = import_base_type.MetadataSymbol, _b) { constructor(pattern) { super(); this.pattern = pattern; __publicField(this, _a, {}); __publicField(this, "meta", new StringMetadata(this, this[import_base_type.MetadataSymbol])); __publicField(this, "kind", "stringMatching"); Object.freeze(this); } /** @internal */ static getOriginalMetadata(dt) { return dt[import_base_type.MetadataSymbol]; } /** @internal */ _acceptVisitor(visitor) { return visitor.visit(this); } setTsPattern(tsPattern) { this[import_base_type.MetadataSymbol].tsPattern = tsPattern; return this; } get ["~standard"]() { return (0, import_generate_standard_schema.getStandardSchemaProps)(this); } ["~validate"](path, value) { if (typeof value !== "string") { throw new import_validation_error.ValidationError(path, this, value, "not a string"); } if (!this.pattern.test(value)) { throw new import_validation_error.ValidationError( path, this, value, "does not match the pattern" ); } } ["~matches"](value) { return typeof value === "string" && this.pattern.test(value); } };