UNPKG

dilswer

Version:

Blazingly fast data validation library with TypeScript integration.

434 lines (432 loc) 11.8 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/types/string-float.ts var string_float_exports = {}; __export(string_float_exports, { NegativeStringFloatType: () => NegativeStringFloatType, PositiveStringFloatType: () => PositiveStringFloatType, StringFloatType: () => StringFloatType, ZeroStringFloatType: () => ZeroStringFloatType }); module.exports = __toCommonJS(string_float_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 StringFloatType = class extends import_base_type.BaseType { constructor() { super(); __publicField(this, "kind", "simple"); __publicField(this, "simpleType", "stringnumeral"); __publicField(this, "_options", { positive: true, negative: true, zero: true }); Object.freeze(this); } get options() { return __spreadValues({}, this._options); } positive() { return new PositiveStringFloatType(); } negative() { return new NegativeStringFloatType(); } zero() { return new ZeroStringFloatType(); } /** @internal */ _acceptVisitor(visitor, depth = 1) { return visitor.visit(this, void 0, depth); } 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"); } let i = 0; if (value[0] === "-") { i++; } let dotCount = 0; for (; i < value.length; i++) { const charCode = value.charCodeAt(i); if (charCode >= 48 && charCode <= 57) { continue; } if (charCode === 46) { dotCount++; continue; } throw new import_validation_error.ValidationError( path, this, value, "value contained by the string is not a valid float" ); } if (dotCount > 1 || value.length === 0) { throw new import_validation_error.ValidationError( path, this, value, "value contained by the string is not a valid float" ); } } ["~matches"](value) { if (typeof value !== "string") { return false; } let i = 0; if (value[0] === "-") { i++; } let dotCount = 0; for (; i < value.length; i++) { const charCode = value.charCodeAt(i); if (charCode >= 48 && charCode <= 57) { continue; } if (charCode === 46) { dotCount++; continue; } return false; } return dotCount <= 1 && value.length > 0; } toString() { return `PrimitiveSchema[ string (float) ]`; } }; var ZeroStringFloatType = class extends StringFloatType { constructor() { super(); this._options.negative = false; this._options.positive = false; Object.freeze(this); } positive() { throw new Error("This type already has a zero constraint"); } negative() { throw new Error("This type already has a zero constraint"); } zero() { throw new Error("This type already has a zero constraint"); } orPositive() { return new import_union.UnionType([ this, new PositiveStringFloatType() ]); } orNegative() { return new import_union.UnionType([ this, new NegativeStringFloatType() ]); } ["~validate"](path, value) { if (typeof value !== "string") { throw new import_validation_error.ValidationError(path, this, value, "not a string"); } if (value.length === 0) { throw new import_validation_error.ValidationError(path, this, value, "empty string"); } let i = 0; if (value[0] === "-") { i++; } let dotCount = 0; for (; i < value.length; i++) { const charCode = value.charCodeAt(i); if (charCode == 48) { continue; } if (charCode === 46) { dotCount++; continue; } throw new import_validation_error.ValidationError( path, this, value, "value contained by the string is not a zero" ); } if (dotCount > 1 || value.length === 0) { throw new import_validation_error.ValidationError( path, this, value, "value contained by the string is not a valid float" ); } } ["~matches"](value) { if (typeof value !== "string") { return false; } let i = 0; if (value[0] === "-") { i++; } let dotCount = 0; for (; i < value.length; i++) { const charCode = value.charCodeAt(i); if (charCode == 48) { continue; } if (charCode === 46) { dotCount++; continue; } return false; } return dotCount <= 1 && value.length > 0; } toString() { return `PrimitiveSchema[ string (float) ${JSON.stringify(this._options)} ]`; } }; var PositiveStringFloatType = class extends StringFloatType { constructor() { super(); this._options.negative = false; this._options.zero = false; Object.freeze(this); } positive() { throw new Error("This type already has a positive constraint"); } negative() { throw new Error("This type already has a positive constraint"); } zero() { throw new Error("This type already has a positive constraint"); } orZero() { return new import_union.UnionType([ this, new ZeroStringFloatType() ]); } ["~validate"](path, value) { if (typeof value !== "string") { throw new import_validation_error.ValidationError(path, this, value, "not a string"); } if (value.length === 0) { throw new import_validation_error.ValidationError(path, this, value, "empty string"); } if (value[0] === "-") { throw new import_validation_error.ValidationError( path, this, value, "value contained by the string is not positive" ); } let hasNonZeroDigit = false; let dotCount = 0; for (let i = 0; i < value.length; i++) { const charCode = value.charCodeAt(i); if (charCode >= 49 && charCode <= 57) { hasNonZeroDigit = true; continue; } if (charCode === 48) { continue; } if (charCode === 46) { dotCount++; continue; } throw new import_validation_error.ValidationError( path, this, value, "value contained by the string is not a valid float" ); } if (dotCount > 1) { throw new import_validation_error.ValidationError( path, this, value, "value contained by the string is not a valid float" ); } if (!hasNonZeroDigit) { throw new import_validation_error.ValidationError( path, this, value, "value contained by the string is not positive" ); } } ["~matches"](value) { if (typeof value !== "string") { return false; } let hasNonZeroDigit = false; let dotCount = 0; for (let i = 0; i < value.length; i++) { const charCode = value.charCodeAt(i); if (charCode >= 49 && charCode <= 57) { hasNonZeroDigit = true; continue; } if (charCode === 48) { continue; } if (charCode === 46) { dotCount++; continue; } return false; } return dotCount <= 1 && hasNonZeroDigit; } toString() { return `PrimitiveSchema[ string (float) ${JSON.stringify(this._options)} ]`; } }; var NegativeStringFloatType = class extends StringFloatType { constructor() { super(); this._options.positive = false; this._options.zero = false; Object.freeze(this); } positive() { throw new Error("This type already has a negative constraint"); } negative() { throw new Error("This type already has a negative constraint"); } zero() { throw new Error("This type already has a negative constraint"); } orZero() { return new import_union.UnionType([ this, new ZeroStringFloatType() ]); } ["~validate"](path, value) { if (typeof value !== "string") { throw new import_validation_error.ValidationError(path, this, value, "not a string"); } if (value.length === 0) { throw new import_validation_error.ValidationError(path, this, value, "empty string"); } if (value[0] !== "-") { throw new import_validation_error.ValidationError( path, this, value, "value contained by the string is not negative" ); } let hasNonZeroDigit = false; let dotCount = 0; for (let i = 1; i < value.length; i++) { const charCode = value.charCodeAt(i); if (charCode >= 49 && charCode <= 57) { hasNonZeroDigit = true; continue; } if (charCode === 48) { continue; } if (charCode === 46) { dotCount++; continue; } throw new import_validation_error.ValidationError( path, this, value, "value contained by the string is not a valid float" ); } if (dotCount > 1) { throw new import_validation_error.ValidationError(path, this, value, "not a valid float"); } if (!hasNonZeroDigit) { throw new import_validation_error.ValidationError( path, this, value, "value contained by the string is not negative" ); } } ["~matches"](value) { if (typeof value !== "string") { return false; } if (value[0] !== "-") { return false; } let hasNonZeroDigit = false; let dotCount = 0; for (let i = 1; i < value.length; i++) { const charCode = value.charCodeAt(i); if (charCode >= 49 && charCode <= 57) { hasNonZeroDigit = true; continue; } if (charCode === 48) { continue; } if (charCode === 46) { dotCount++; continue; } return false; } return dotCount <= 1 && hasNonZeroDigit; } toString() { return `PrimitiveSchema[ string (float) ${JSON.stringify(this._options)} ]`; } };