validata
Version:
Type safe data validation and sanitization
125 lines • 6.49 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.basicValidation = exports.createMaybeAsCheck = exports.createAsCheck = exports.createMaybeCheck = exports.createIsCheck = exports.asNullable = exports.isNullable = exports.as = exports.is = exports.maybe = exports.nullOrUndefined = exports.definitely = exports.withDefault = void 0;
const types_1 = require("./types");
const withDefault = (options) => () => {
if ((options === null || options === void 0 ? void 0 : options.default) === undefined)
return undefined;
return { value: (options === null || options === void 0 ? void 0 : options.default) instanceof Function ? options.default() : options.default };
};
exports.withDefault = withDefault;
const definitely = (undefinedHandler) => (next) => (value, path = []) => {
var _a;
if (value === null || value === undefined) {
return (_a = undefinedHandler === null || undefinedHandler === void 0 ? void 0 : undefinedHandler()) !== null && _a !== void 0 ? _a : { issues: [types_1.Issue.forPath(path, value, 'not-defined')] };
}
return next(value, path);
};
exports.definitely = definitely;
const nullOrUndefined = (value) => {
return value === null || value === undefined;
};
exports.nullOrUndefined = nullOrUndefined;
const maybe = (empty, check, options, undefinedHandler) => (next) => (value, path = []) => {
var _a;
if (empty(value)) {
return (_a = undefinedHandler === null || undefinedHandler === void 0 ? void 0 : undefinedHandler()) !== null && _a !== void 0 ? _a : { value: undefined };
}
if (options === null || options === void 0 ? void 0 : options.incorrectTypeToUndefined) {
if (!check(value)) {
return { value: undefined };
}
}
const result = next(value, path);
if ((0, types_1.isIssue)(result) && result.issues.length === 1 && result.issues[0].reason === 'no-conversion') {
if (options === null || options === void 0 ? void 0 : options.strictParsing)
return result;
return { value: undefined };
}
return result;
};
exports.maybe = maybe;
const is = (check, typeName) => (next) => (value, path = []) => {
if (!check(value)) {
return { issues: [types_1.Issue.forPath(path, value, 'incorrect-type', { expectedType: typeName })] };
}
return next(value, path);
};
exports.is = is;
const as = (check, convert, typeName, undefinedHandler, options) => (next) => (value, path = []) => {
var _a, _b, _c;
if (check(value))
return next(value, path);
const converted = (_b = (_a = options === null || options === void 0 ? void 0 : options.converter) === null || _a === void 0 ? void 0 : _a.call(options, value, options.convertOptions)) !== null && _b !== void 0 ? _b : convert(value, options);
if (converted === undefined || converted === null) {
return (_c = undefinedHandler === null || undefinedHandler === void 0 ? void 0 : undefinedHandler()) !== null && _c !== void 0 ? _c : { issues: [types_1.Issue.forPath(path, value, 'no-conversion', { toType: typeName })] };
}
return next(converted, path);
};
exports.as = as;
const getResultOrValidationIssues = (validate, value, path, options) => {
if (!options)
return { value };
const validationResult = validate(value, path, options);
return validationResult.issues.length ? validationResult : { value };
};
const isNullable = (processor) => ({
process: (value, path) => {
if (value === null)
return { value: null };
return processor.process(value, path);
},
});
exports.isNullable = isNullable;
const asNullable = (processor, options) => ({
process: (value, path) => {
if (value === null)
return { value: null };
const result = processor.process(value, path);
if (!(0, types_1.isIssue)(result) && result.value === undefined) {
const defaultValue = (options === null || options === void 0 ? void 0 : options.default) === undefined
? null
: (options === null || options === void 0 ? void 0 : options.default) instanceof Function
? options.default()
: options.default;
return { value: defaultValue };
}
return result;
},
});
exports.asNullable = asNullable;
const createIsCheck = (typeName, check, coerce, validate) => (options) => {
return {
process: (0, exports.definitely)()((0, exports.is)(check, typeName)(coerce(options)((value, path) => getResultOrValidationIssues(validate, value, path, options)))),
};
};
exports.createIsCheck = createIsCheck;
const createMaybeCheck = (typeName, check, coerce, validate, empty = exports.nullOrUndefined) => (options) => {
return {
process: (0, exports.maybe)(empty, check, options)((0, exports.is)(check, typeName)(coerce(options)((value, path) => getResultOrValidationIssues(validate, value, path, options)))),
};
};
exports.createMaybeCheck = createMaybeCheck;
const createAsCheck = (typeName, check, convert, coerce, validate) => (options) => {
return {
process: (0, exports.definitely)((0, exports.withDefault)(options))((0, exports.as)(check, convert, typeName, (0, exports.withDefault)(options), options)(coerce(options)((value, path) => getResultOrValidationIssues(validate, value, path, options)))),
};
};
exports.createAsCheck = createAsCheck;
const createMaybeAsCheck = (typeName, check, convert, coerce, validate, empty = exports.nullOrUndefined) => (options) => {
return {
process: (0, exports.maybe)(empty, check, options, (0, exports.withDefault)(options))((0, exports.as)(check, convert, typeName, (0, exports.withDefault)(options), options)(coerce(options)((value, path) => getResultOrValidationIssues(validate, value, path, options)))),
};
};
exports.createMaybeAsCheck = createMaybeAsCheck;
const basicValidation = (value, path, options) => {
var _a;
const customValidatorResult = (_a = options.validator) === null || _a === void 0 ? void 0 : _a.call(options, value, options.validatorOptions, path);
if (customValidatorResult === undefined || customValidatorResult === true)
return { issues: [] };
if (customValidatorResult === false)
return { issues: [types_1.Issue.forPath(path, value, 'validator')] };
return { issues: customValidatorResult };
};
exports.basicValidation = basicValidation;
//# sourceMappingURL=common.js.map
;