dilswer
Version:
Blazingly fast data validation library with TypeScript integration.
38 lines (37 loc) • 946 B
JavaScript
// src/validation-algorithms/create-validator.ts
import { validatedCircularValues } from "../data-types/types/recursive.mjs";
import { Path } from "./path.mjs";
import { ValidationError } from "./validation-error/validation-error.mjs";
var DEFAULT_ROOT = 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 (!ValidationError.isValidationError(e)) throw e;
return {
success: false,
error: e
};
} finally {
validatedCircularValues.clear();
}
};
} else {
return (value) => {
try {
return dataType["~matches"](value);
} finally {
validatedCircularValues.clear();
}
};
}
}
export {
validator
};