dilswer
Version:
Blazingly fast data validation library with TypeScript integration.
88 lines (86 loc) • 3.14 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
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/tuple.ts
var tuple_exports = {};
__export(tuple_exports, {
TupleType: () => TupleType
});
module.exports = __toCommonJS(tuple_exports);
var import_base_type = require("../base-type.js");
var import_generate_standard_schema = require("../generate-standard-schema.js");
var import_validation_error = require("../../validation-algorithms/validation-error/validation-error.js");
var TupleType = class extends import_base_type.BaseType {
constructor(tuple) {
super();
__publicField(this, "tuple", tuple);
__publicField(this, "kind", "tuple");
Object.freeze(this.tuple);
Object.freeze(this);
}
/** @internal */
_acceptVisitor(visitor, depth = 1) {
const children = [];
for (let i = 0; i < this.tuple.length; i++) {
children.push(this.tuple[i]._acceptVisitor(visitor, depth + 1));
}
return visitor.visit(this, children, depth);
}
get ["~standard"]() {
return (0, import_generate_standard_schema.getStandardSchemaProps)(this);
}
["~validate"](path, value) {
if (!Array.isArray(value)) {
throw new import_validation_error.ValidationError(path, this, value, "not an array");
}
if (value.length !== this.tuple.length) {
throw new import_validation_error.ValidationError(
path,
this,
value,
"too many or too few elements in array"
);
}
for (let index = 0; index < this.tuple.length; index++) {
const elem = value[index];
const elemtType = this.tuple[index];
elemtType["~validate"](
path.concat(index),
elem
);
}
}
["~matches"](value) {
if (!Array.isArray(value) || value.length !== this.tuple.length) {
return false;
}
for (let index = 0; index < this.tuple.length; index++) {
const elem = value[index];
const elemtType = this.tuple[index];
if (!elemtType["~matches"](elem)) {
return false;
}
}
return true;
}
toString() {
return `TupleSchema[ ${this.tuple.map((t) => t.toString()).join(", ")} ]`;
}
};