UNPKG

dilswer

Version:

Blazingly fast data validation library with TypeScript integration.

116 lines (114 loc) 3.66 kB
var __defProp = Object.defineProperty; 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 __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); // src/data-types/types/integer.ts import { BaseType } from "../base-type.mjs"; import { getStandardSchemaProps } from "../generate-standard-schema.mjs"; import { ValidationError } from "../../validation-algorithms/validation-error/validation-error.mjs"; var IntegerType = class extends BaseType { constructor() { super(); __publicField(this, "kind", "simple"); __publicField(this, "simpleType", "integer"); __publicField(this, "_options", { min: null, max: null }); Object.freeze(this); } get options() { return __spreadValues({}, this._options); } min(min) { const t = new ComplexIntegerType(); t._options.min = min; return t; } max(max) { const t = new ComplexIntegerType(); t._options.max = max; return t; } /** @internal */ _acceptVisitor(visitor, depth = 1) { return visitor.visit(this, void 0, depth); } get ["~standard"]() { return getStandardSchemaProps(this); } ["~validate"](path, value) { if (typeof value !== "number" || Number.isNaN(value)) { throw new ValidationError(path, this, value, "not a number"); } if (!Number.isInteger(value)) { throw new ValidationError(path, this, value, "not an integer"); } } ["~matches"](value) { return typeof value === "number" && !Number.isNaN(value) && Number.isInteger(value); } toString() { return `PrimitiveSchema[ int ]`; } }; var ComplexIntegerType = class _ComplexIntegerType extends IntegerType { min(min) { const t = new _ComplexIntegerType(); Object.assign(t._options, this._options); t._options.min = min; return t; } max(max) { const t = new _ComplexIntegerType(); Object.assign(t._options, this._options); t._options.max = max; return t; } ["~validate"](path, value) { if (typeof value !== "number" || Number.isNaN(value)) { throw new ValidationError(path, this, value, "not a number"); } if (!Number.isInteger(value)) { throw new ValidationError(path, this, value, "not an integer"); } if (this._options.min !== null && value < this._options.min) { throw new ValidationError(path, this, value, "integer less than min"); } if (this._options.max !== null && value > this._options.max) { throw new ValidationError(path, this, value, "integer greater than max"); } } ["~matches"](value) { if (typeof value !== "number" || Number.isNaN(value) || !Number.isInteger(value)) { return false; } if (this._options.min !== null && value < this._options.min) { return false; } if (this._options.max !== null && value > this._options.max) { return false; } return true; } toString() { return `PrimitiveSchema[ int ${JSON.stringify(this._options)} ]`; } }; export { ComplexIntegerType, IntegerType };