validata
Version:
Type safe data validation and sanitization
29 lines • 1.08 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkAsync = exports.check = exports.ValidationError = void 0;
const types_1 = require("./types");
class ValidationError extends Error {
constructor(issues) {
super('Validation failed');
this.issues = issues;
this.name = 'ValidationError';
Object.setPrototypeOf(this, new.target.prototype);
}
}
exports.ValidationError = ValidationError;
const getValue = (result) => {
if ((0, types_1.isIssue)(result))
throw new ValidationError(result.issues);
return result.value;
};
const check = (valueProcessor, value, path = []) => {
const result = valueProcessor.process(value(), Array.isArray(path) ? path : [path]);
return getValue(result);
};
exports.check = check;
const checkAsync = async (asyncValueProcessor, value, path = []) => {
const result = await asyncValueProcessor.process(await value(), Array.isArray(path) ? path : [path]);
return getValue(result);
};
exports.checkAsync = checkAsync;
//# sourceMappingURL=check.js.map
;