dilswer
Version:
Blazingly fast data validation library with TypeScript integration.
348 lines (346 loc) • 9.79 kB
JavaScript
"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-integer.ts
var string_integer_exports = {};
__export(string_integer_exports, {
NegativeStringIntegerType: () => NegativeStringIntegerType,
PositiveStringIntegerType: () => PositiveStringIntegerType,
StringIntegerType: () => StringIntegerType,
ZeroStringIntegerType: () => ZeroStringIntegerType
});
module.exports = __toCommonJS(string_integer_exports);
var import_base_type = require("../base-type.cjs");
var import_generate_standard_schema = require("../generate-standard-schema.cjs");
var import_union = require("./union.cjs");
var import_validation_error = require("../../validation-algorithms/validation-error/validation-error.cjs");
var StringIntegerType = class extends import_base_type.BaseType {
constructor() {
super();
__publicField(this, "kind", "simple");
__publicField(this, "simpleType", "stringinteger");
__publicField(this, "_options", {
positive: true,
negative: true,
zero: true
});
Object.freeze(this);
}
get options() {
return __spreadValues({}, this._options);
}
positive() {
return new PositiveStringIntegerType();
}
negative() {
return new NegativeStringIntegerType();
}
zero() {
return new ZeroStringIntegerType();
}
/** @internal */
_acceptVisitor(visitor) {
return visitor.visit(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 (value.length === 0) {
throw new import_validation_error.ValidationError(path, this, value, "empty string");
}
let i = 0;
if (value[0] === "-") {
i++;
}
for (; i < value.length; i++) {
const charCode = value.charCodeAt(i);
if (charCode >= 48 && charCode <= 57) {
continue;
}
throw new import_validation_error.ValidationError(
path,
this,
value,
"value contained by the string is not a valid integer"
);
}
}
["~matches"](value) {
if (typeof value !== "string") {
return false;
}
let i = 0;
if (value[0] === "-") {
i++;
}
for (; i < value.length; i++) {
const charCode = value.charCodeAt(i);
if (charCode >= 48 && charCode <= 57) {
continue;
}
return false;
}
return value.length > 0;
}
};
var ZeroStringIntegerType = class extends StringIntegerType {
constructor() {
super();
this._options.negative = false;
this._options.positive = false;
}
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 PositiveStringIntegerType()
]);
}
orNegative() {
return new import_union.UnionType([
this,
new NegativeStringIntegerType()
]);
}
["~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++;
}
for (; i < value.length; i++) {
const charCode = value.charCodeAt(i);
if (charCode === 48) {
continue;
}
throw new import_validation_error.ValidationError(
path,
this,
value,
"value contained by the string is not equalt to zero"
);
}
}
["~matches"](value) {
if (typeof value !== "string") {
return false;
}
let i = 0;
if (value[0] === "-") {
i++;
}
for (; i < value.length; i++) {
const charCode = value.charCodeAt(i);
if (charCode === 48) {
continue;
}
return false;
}
return value.length > 0;
}
};
var PositiveStringIntegerType = class extends StringIntegerType {
constructor() {
super();
this._options.negative = false;
this._options.zero = false;
}
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 ZeroStringIntegerType()
]);
}
["~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 hasNonZeroDigit = false;
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;
}
throw new import_validation_error.ValidationError(
path,
this,
value,
"value contained by the string is not a valid integer"
);
}
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;
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;
}
return false;
}
return hasNonZeroDigit;
}
};
var NegativeStringIntegerType = class extends StringIntegerType {
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 ZeroStringIntegerType()
]);
}
["~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;
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;
}
throw new import_validation_error.ValidationError(
path,
this,
value,
"value contained by the string is not a valid integer"
);
}
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;
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;
}
return false;
}
return hasNonZeroDigit;
}
};