dilswer
Version:
Blazingly fast data validation library with TypeScript integration.
59 lines (57 loc) • 1.96 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
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);
// src/validation-algorithms/create-validator.ts
var create_validator_exports = {};
__export(create_validator_exports, {
validator: () => validator
});
module.exports = __toCommonJS(create_validator_exports);
var import_recursive = require("../data-types/types/recursive.cjs");
var import_path = require("./path.cjs");
var import_validation_error = require("./validation-error/validation-error.cjs");
var DEFAULT_ROOT = import_path.Path.init("$");
function validator(dataType, options) {
if (options == null ? void 0 : options.details) {
return (data) => {
try {
dataType["~validate"](DEFAULT_ROOT, data);
return {
success: true,
value: data
};
} catch (e) {
if (!import_validation_error.ValidationError.isValidationError(e)) throw e;
return {
success: false,
error: e
};
} finally {
import_recursive.validatedCircularValues.clear();
}
};
} else {
return (value) => {
try {
return dataType["~matches"](value);
} finally {
import_recursive.validatedCircularValues.clear();
}
};
}
}