dilswer
Version:
Blazingly fast data validation library with TypeScript integration.
387 lines (385 loc) • 9.46 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-float.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 StringFloatType = class extends 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) {
return visitor.visit(this);
}
get ["~standard"]() {
return getStandardSchemaProps(this);
}
["~validate"](path, value) {
if (typeof value !== "string") {
throw new 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 ValidationError(
path,
this,
value,
"value contained by the string is not a valid float"
);
}
if (dotCount > 1 || value.length === 0) {
throw new 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;
}
};
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 UnionType([
this,
new PositiveStringFloatType()
]);
}
orNegative() {
return new UnionType([
this,
new NegativeStringFloatType()
]);
}
["~validate"](path, value) {
if (typeof value !== "string") {
throw new 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) {
continue;
}
if (charCode === 46) {
dotCount++;
continue;
}
throw new ValidationError(
path,
this,
value,
"value contained by the string is not a zero"
);
}
if (dotCount > 1 || value.length === 0) {
throw new 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;
}
};
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 UnionType([
this,
new ZeroStringFloatType()
]);
}
["~validate"](path, value) {
if (typeof value !== "string") {
throw new ValidationError(path, this, value, "not a string");
}
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 ValidationError(
path,
this,
value,
"value contained by the string is not a valid float"
);
}
if (dotCount > 1) {
throw new ValidationError(
path,
this,
value,
"value contained by the string is not a valid float"
);
}
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;
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;
}
};
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 UnionType([
this,
new ZeroStringFloatType()
]);
}
["~validate"](path, value) {
if (typeof value !== "string") {
throw new ValidationError(path, this, value, "not a string");
}
if (value[0] !== "-") {
throw new 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 ValidationError(
path,
this,
value,
"value contained by the string is not a valid float"
);
}
if (dotCount > 1) {
throw new ValidationError(path, this, value, "not a valid float");
}
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;
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;
}
};
export {
NegativeStringFloatType,
PositiveStringFloatType,
StringFloatType,
ZeroStringFloatType
};