validata
Version:
Type safe data validation and sanitization
80 lines • 3.42 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.maybeAsString = exports.asString = exports.maybeString = exports.isString = void 0;
const luxon_1 = require("luxon");
const common_1 = require("./common");
const types_1 = require("./types");
const check = (value) => {
return typeof value === 'string';
};
const convert = (value) => {
var _a, _b, _c;
if (value instanceof Date) {
return (_a = luxon_1.DateTime.fromJSDate(value).toUTC().toISO()) !== null && _a !== void 0 ? _a : undefined;
}
if (value instanceof luxon_1.DateTime) {
return (_b = value.toUTC().toISO()) !== null && _b !== void 0 ? _b : undefined;
}
if (value instanceof luxon_1.Duration) {
return (_c = value.toISO()) !== null && _c !== void 0 ? _c : undefined;
}
return String(value);
};
const coerce = (options) => (next) => (value, path) => {
if (!options)
return next(value, path);
let coerced = value;
if (options.limitLength !== undefined && coerced.length > options.limitLength) {
coerced = coerced.slice(0, options.limitLength);
}
switch (options.trim) {
case 'start':
coerced = coerced.trimStart();
break;
case 'end':
coerced = coerced.trimRight();
break;
case 'both':
coerced = coerced.trim();
break;
}
if (options.transform) {
if (Array.isArray(options.transform)) {
coerced = options.transform.reduce((acc, transform) => transform(acc), coerced);
}
else {
coerced = options.transform(coerced);
}
}
if (options.padStart && coerced.length < options.padStart.length) {
coerced = coerced.padStart(options.padStart.length, options.padStart.padWith);
}
if (options.padEnd && coerced.length < options.padEnd.length) {
coerced = coerced.padEnd(options.padEnd.length, options.padEnd.padWith);
}
return next(coerced, path);
};
const validate = (value, path, options) => {
const result = (0, common_1.basicValidation)(value, path, options);
if (options.minLength !== undefined && value.length < options.minLength) {
result.issues.push(types_1.Issue.forPath(path, value, 'min-length', { length: value.length, min: options.minLength }));
}
if (options.maxLength !== undefined && value.length > options.maxLength) {
result.issues.push(types_1.Issue.forPath(path, value, 'max-length', { length: value.length, max: options.maxLength }));
}
if (options.regex !== undefined && !options.regex.test(value)) {
result.issues.push(types_1.Issue.forPath(path, value, 'regex', { regex: options.regex.toString() }));
}
if (options.format !== undefined) {
const formatResult = options.format(value);
if (formatResult !== true) {
result.issues.push(types_1.Issue.forPath(path, value, 'incorrect-format', formatResult));
}
}
return result;
};
exports.isString = (0, common_1.createIsCheck)('string', check, coerce, validate);
exports.maybeString = (0, common_1.createMaybeCheck)('string', check, coerce, validate);
exports.asString = (0, common_1.createAsCheck)('string', check, convert, coerce, validate);
exports.maybeAsString = (0, common_1.createMaybeAsCheck)('string', check, convert, coerce, validate);
//# sourceMappingURL=string.js.map
;