dilswer
Version:
Blazingly fast data validation library with TypeScript integration.
350 lines (348 loc) • 8.92 kB
JavaScript
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/string-integer.ts
import { BaseType } from "../base-type.mjs";
import { getStandardSchemaProps } from "../generate-standard-schema.mjs";
import { UnionType } from "./union.mjs";
import { ValidationError } from "../../validation-algorithms/validation-error/validation-error.mjs";
var StringIntegerType = class extends 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, depth = 1) {
return visitor.visit(this, void 0, depth);
}
get ["~standard"]() {
return getStandardSchemaProps(this);
}
["~validate"](path, value) {
if (typeof value !== "string") {
throw new ValidationError(path, this, value, "not a string");
}
if (value.length === 0) {
throw new 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 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;
}
toString() {
return `PrimitiveSchema[ string (int) ]`;
}
};
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 UnionType([
this,
new PositiveStringIntegerType()
]);
}
orNegative() {
return new UnionType([
this,
new NegativeStringIntegerType()
]);
}
["~validate"](path, value) {
if (typeof value !== "string") {
throw new ValidationError(path, this, value, "not a string");
}
if (value.length === 0) {
throw new 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 ValidationError(
path,
this,
value,
"value contained by the string is not equal 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;
}
toString() {
return `PrimitiveSchema[ string (int) ${JSON.stringify(this._options)} ]`;
}
};
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 UnionType([
this,
new ZeroStringIntegerType()
]);
}
["~validate"](path, value) {
if (typeof value !== "string") {
throw new ValidationError(path, this, value, "not a string");
}
if (value.length === 0) {
throw new ValidationError(path, this, value, "empty string");
}
if (value[0] === "-") {
throw new ValidationError(
path,
this,
value,
"value contained by the string is not positive"
);
}
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 ValidationError(
path,
this,
value,
"value contained by the string is not a valid integer"
);
}
if (!hasNonZeroDigit) {
throw new 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;
}
toString() {
return `PrimitiveSchema[ string (int) ${JSON.stringify(this._options)} ]`;
}
};
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 UnionType([
this,
new ZeroStringIntegerType()
]);
}
["~validate"](path, value) {
if (typeof value !== "string") {
throw new ValidationError(path, this, value, "not a string");
}
if (value.length === 0) {
throw new ValidationError(path, this, value, "empty string");
}
if (value[0] !== "-") {
throw new 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 ValidationError(
path,
this,
value,
"value contained by the string is not a valid integer"
);
}
if (!hasNonZeroDigit) {
throw new 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;
}
toString() {
return `PrimitiveSchema[ string (int) ${JSON.stringify(this._options)} ]`;
}
};
export {
NegativeStringIntegerType,
PositiveStringIntegerType,
StringIntegerType,
ZeroStringIntegerType
};