dilswer
Version:
Blazingly fast data validation library with TypeScript integration.
123 lines (121 loc) • 4.38 kB
JavaScript
;
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.ts
var string_exports = {};
__export(string_exports, {
ComplesStringType: () => ComplesStringType,
StringType: () => StringType
});
module.exports = __toCommonJS(string_exports);
var import_base_type = require("../base-type.cjs");
var import_generate_standard_schema = require("../generate-standard-schema.cjs");
var import_string_float = require("./string-float.cjs");
var import_string_integer = require("./string-integer.cjs");
var import_string_matching = require("./string-matching.cjs");
var import_validation_error = require("../../validation-algorithms/validation-error/validation-error.cjs");
var StringType = class extends import_base_type.BaseType {
constructor() {
super();
__publicField(this, "kind", "simple");
__publicField(this, "simpleType", "string");
__publicField(this, "_options", {
min: null,
max: null
});
Object.freeze(this);
}
get options() {
return __spreadValues({}, this._options);
}
get Int() {
return new import_string_integer.StringIntegerType();
}
get Float() {
return new import_string_float.StringFloatType();
}
matching(pattern) {
return new import_string_matching.StringMatchingType(pattern);
}
len(constraints) {
const t = new ComplesStringType();
Object.assign(t._options, constraints);
return t;
}
/** @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");
}
}
["~matches"](value) {
return typeof value === "string";
}
};
var ComplesStringType = class _ComplesStringType extends StringType {
len(constraints) {
const t = new _ComplesStringType();
Object.assign(t._options, this._options);
Object.assign(t._options, constraints);
return t;
}
["~validate"](path, value) {
if (typeof value !== "string") {
throw new import_validation_error.ValidationError(path, this, value, "not a string");
}
if (this._options.min !== null && value.length < this._options.min) {
throw new import_validation_error.ValidationError(path, this, value, "string shorter than min");
}
if (this._options.max !== null && value.length > this._options.max) {
throw new import_validation_error.ValidationError(path, this, value, "string longer than max");
}
}
["~matches"](value) {
if (typeof value !== "string") {
return false;
}
if (this._options.min !== null && value.length < this._options.min) {
return false;
}
if (this._options.max !== null && value.length > this._options.max) {
return false;
}
return true;
}
};