dilswer
Version:
Blazingly fast data validation library with TypeScript integration.
42 lines (40 loc) • 1.3 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/literal.ts
import { BaseType } from "../base-type.mjs";
import { getStandardSchemaProps } from "../generate-standard-schema.mjs";
import { ValidationError } from "../../validation-algorithms/validation-error/validation-error.mjs";
var LiteralType = class extends BaseType {
constructor(literal) {
super();
__publicField(this, "literal", literal);
__publicField(this, "kind", "literal");
Object.freeze(this);
}
/** @internal */
_acceptVisitor(visitor, depth = 1) {
return visitor.visit(this, void 0, depth);
}
get ["~standard"]() {
return getStandardSchemaProps(this);
}
["~validate"](path, value) {
if (this.literal === value) return;
throw new ValidationError(
path,
this,
value,
`not equal to the expected literal value`
);
}
["~matches"](value) {
return this.literal === value;
}
toString() {
return `LiteralSchema[ ${this.literal} ]`;
}
};
export {
LiteralType
};