dilswer
Version:
Blazingly fast data validation library with TypeScript integration.
143 lines (141 loc) • 4.15 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/base-type.ts
import { compileFastValidator } from "../validation-algorithms/compile-fast-validator.mjs";
var DataTypeSymbol = Symbol();
var MetadataSymbol = Symbol("metadata");
var BasicDataTypes = {
Unknown: "unknown",
String: "string",
Number: "number",
Int: "integer",
Boolean: "boolean",
Symbol: "symbol",
Function: "function",
Null: "null",
Undefined: "undefined",
StringNumeral: "stringnumeral",
StringInt: "stringinteger"
};
var TypeMetadata = class {
constructor(type, container) {
this.type = type;
this.container = container;
}
/**
* Retrieves the metadata of a DataType, like title, description
* or examples.
*
* Metadata must be explicitly set on the DataType, otherwise it
* will be an empty object.
*/
get() {
const copy = __spreadValues({}, this.container);
if (copy.extra) {
copy.extra = __spreadValues({}, copy.extra);
}
return copy;
}
/**
* Sets a metadata `description` property. This property can be
* later read by `getMetadata` and is also used by
* `toJsonSchema` to generate a JSON Schema.
*/
description(description) {
this.container.description = description;
return this.type;
}
/**
* Sets a metadata `title` property. This property can be later
* read by `getMetadata` and is also used by `toJsonSchema` to
* generate a JSON Schema.
*/
title(name) {
this.container.title = name;
return this.type;
}
/**
* Sets a metadata `format` property. This property can be
* later read by `getMetadata` and is also used by
* `toJsonSchema` to generate a JSON Schema.
*/
format(format) {
this.container.format = format;
return this.type;
}
/**
* Sets the extra metadata. The extra metadata can be anything.
* This metadata is not used by Dilswer, but can be by the
* Dilswer consumer.
*/
extra(extra) {
this.container.extra = extra;
return this.type;
}
};
var _a, _b;
_b = MetadataSymbol, _a = DataTypeSymbol;
var BaseType = class {
constructor() {
__publicField(this, _b, {});
__publicField(this, _a, true);
__publicField(this, "kind");
__publicField(this, "compiledValidatorRef", {});
__publicField(this, "meta", new TypeMetadata(this, this[MetadataSymbol]));
}
/** @internal */
static getOriginalMetadata(dt) {
return dt[MetadataSymbol];
}
copy() {
const proto = Object.getPrototypeOf(this);
const copy = Object.create(proto);
Object.assign(copy, this);
copy[MetadataSymbol] = __spreadValues({}, this[MetadataSymbol]);
return copy;
}
/**
* Compiles a fast validator to be used via interfaces that support the Standard Schema
* (through the `~standard` property.)
*
* Compiled validator is much faster than default, but provides less informations in
* case of validation failure.
*/
compile() {
const fastValidator = compileFastValidator(this);
this.compiledValidatorRef.fn = (value) => {
if (fastValidator(value)) {
return { value };
} else {
return {
issues: [{
message: "Value does not conform the data type structure definition."
}]
};
}
};
return this;
}
};
export {
BaseType,
BasicDataTypes,
DataTypeSymbol,
MetadataSymbol,
TypeMetadata
};