dilswer
Version:
Blazingly fast data validation library with TypeScript integration.
54 lines (52 loc) • 1.86 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
// src/data-types/types/array.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 ArrayType = class extends BaseType {
constructor(arrayOf) {
super();
__publicField(this, "arrayOf", arrayOf);
__publicField(this, "kind", "array");
__publicField(this, "union");
this.union = new UnionType(this.arrayOf);
Object.freeze(this.arrayOf);
Object.freeze(this);
}
/** @internal */
_acceptVisitor(visitor, depth = 1) {
const children = [];
for (let i = 0; i < this.arrayOf.length; i++) {
children.push(this.arrayOf[i]._acceptVisitor(visitor, depth + 1));
}
return visitor.visit(this, children, depth);
}
get ["~standard"]() {
return getStandardSchemaProps(this);
}
["~validate"](path, value) {
if (!Array.isArray(value)) {
throw new ValidationError(path, this, value, "not an array");
}
for (let index = 0; index < value.length; index++) {
this.union["~validate"](path.concat(index), value[index]);
}
}
["~matches"](value) {
if (!Array.isArray(value)) return false;
for (let index = 0; index < value.length; index++) {
const match = this.union["~matches"](value[index]);
if (!match) return false;
}
return true;
}
toString() {
return `ArraySchema[ ${this.union.toString()} ]`;
}
};
export {
ArrayType
};