validata
Version:
Type safe data validation and sanitization
51 lines • 2.55 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.StringFormat = exports.REGEX_UUID = exports.REGEX_ULID = void 0;
const validator_1 = require("validator");
exports.REGEX_ULID = /^[0-9A-HJKMNP-TV-Z]{26}$/;
exports.REGEX_UUID = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
const regexCheck = (regex, expectedFormat) => (value) => regex.test(value) || { expectedFormat };
const DEFAULT_PASSWORD_REQUIREMENTS = {
minLength: 8,
numberChars: 1,
lowerCaseChars: 1,
upperCaseChars: 1,
specialChars: 1,
};
const checkEmail = (options) => (value) => {
if (!validator_1.default.isEmail(value, {
'allow_display_name': options === null || options === void 0 ? void 0 : options.allowDisplayName,
})) {
return {
expectedFormat: 'email',
};
}
return true;
};
const checkPassword = (requirements) => (value) => {
var _a, _b, _c, _d, _e, _f, _g, _h;
const reqWithDefaults = Object.assign(Object.assign({}, DEFAULT_PASSWORD_REQUIREMENTS), requirements);
const passwordCheck = {
length: value.length,
numberChars: (_b = (_a = value.match(/\d/g)) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0,
lowerCaseChars: (_d = (_c = value.match(/[a-z]/g)) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0,
upperCaseChars: (_f = (_e = value.match(/[A-Z]/g)) === null || _e === void 0 ? void 0 : _e.length) !== null && _f !== void 0 ? _f : 0,
specialChars: (_h = (_g = value.match(/[^a-zA-Z0-9]/g)) === null || _g === void 0 ? void 0 : _g.length) !== null && _h !== void 0 ? _h : 0,
};
if (passwordCheck.length < reqWithDefaults.minLength
|| passwordCheck.numberChars < reqWithDefaults.numberChars
|| passwordCheck.lowerCaseChars < reqWithDefaults.lowerCaseChars
|| passwordCheck.upperCaseChars < reqWithDefaults.upperCaseChars
|| passwordCheck.specialChars < reqWithDefaults.specialChars) {
return Object.assign({ expectedFormat: 'password' }, reqWithDefaults);
}
return true;
};
var StringFormat;
(function (StringFormat) {
StringFormat.email = checkEmail;
StringFormat.password = checkPassword;
StringFormat.ULID = () => regexCheck(exports.REGEX_ULID, 'ulid');
StringFormat.UUID = () => regexCheck(exports.REGEX_UUID, 'uuid');
})(StringFormat = exports.StringFormat || (exports.StringFormat = {}));
//# sourceMappingURL=string-format.js.map
;