UNPKG

dilswer

Version:

Blazingly fast data validation library with TypeScript integration.

161 lines (159 loc) 5.1 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; 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/base-type.ts var base_type_exports = {}; __export(base_type_exports, { BaseType: () => BaseType, BasicDataTypes: () => BasicDataTypes, DataTypeSymbol: () => DataTypeSymbol, MetadataSymbol: () => MetadataSymbol, TypeMetadata: () => TypeMetadata }); module.exports = __toCommonJS(base_type_exports); var import_compile_fast_validator = require("../validation-algorithms/compile-fast-validator.js"); var DataTypeSymbol = Symbol(); var MetadataSymbol = Symbol("metadata"); var BasicDataTypes = { Unknown: "unknown", String: "string", Number: "number", Int: "integer", Boolean: "boolean", Symbol: "symbol", Function: "function", Null: "null", Undefined: "undefined", StringNumeral: "stringnumeral", StringInt: "stringinteger" }; var TypeMetadata = class { constructor(type, container) { this.type = type; this.container = container; } /** * Retrieves the metadata of a DataType, like title, description * or examples. * * Metadata must be explicitly set on the DataType, otherwise it * will be an empty object. */ get() { const copy = __spreadValues({}, this.container); if (copy.extra) { copy.extra = __spreadValues({}, copy.extra); } return copy; } /** * Sets a metadata `description` property. This property can be * later read by `getMetadata` and is also used by * `toJsonSchema` to generate a JSON Schema. */ description(description) { this.container.description = description; return this.type; } /** * Sets a metadata `title` property. This property can be later * read by `getMetadata` and is also used by `toJsonSchema` to * generate a JSON Schema. */ title(name) { this.container.title = name; return this.type; } /** * Sets a metadata `format` property. This property can be * later read by `getMetadata` and is also used by * `toJsonSchema` to generate a JSON Schema. */ format(format) { this.container.format = format; return this.type; } /** * Sets the extra metadata. The extra metadata can be anything. * This metadata is not used by Dilswer, but can be by the * Dilswer consumer. */ extra(extra) { this.container.extra = extra; return this.type; } }; var _a, _b; _b = MetadataSymbol, _a = DataTypeSymbol; var BaseType = class { constructor() { __publicField(this, _b, {}); __publicField(this, _a, true); __publicField(this, "kind"); __publicField(this, "compiledValidatorRef", {}); __publicField(this, "meta", new TypeMetadata(this, this[MetadataSymbol])); } /** @internal */ static getOriginalMetadata(dt) { return dt[MetadataSymbol]; } copy() { const proto = Object.getPrototypeOf(this); const copy = Object.create(proto); Object.assign(copy, this); copy[MetadataSymbol] = __spreadValues({}, this[MetadataSymbol]); return copy; } /** * Compiles a fast validator to be used via interfaces that support the Standard Schema * (through the `~standard` property.) * * Compiled validator is much faster than default, but provides less informations in * case of validation failure. */ compile() { const fastValidator = (0, import_compile_fast_validator.compileFastValidator)(this); this.compiledValidatorRef.fn = (value) => { if (fastValidator(value)) { return { value }; } else { return { issues: [{ message: "Value does not conform the data type structure definition." }] }; } }; return this; } };