nist-password-validator
Version:
A lightweight, zero-dependencies open-source password validator according to NIST guidelines.
79 lines (77 loc) • 3.07 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/validators/inputValidator.ts
var inputValidator_exports = {};
__export(inputValidator_exports, {
validateInput: () => validateInput
});
module.exports = __toCommonJS(inputValidator_exports);
function validateInput(password, options) {
const errors = [];
if (typeof password !== "string") {
errors.push("Password must be a string.");
return errors;
}
if (options.trimWhitespace !== false) {
password = password.trim();
}
if (!password) {
errors.push("Password cannot be empty.");
return errors;
}
if (options.minLength && (typeof options.minLength !== "number" || options.minLength < 1)) {
errors.push("Minimum length must be a positive number.");
}
if (options.maxLength && (typeof options.maxLength !== "number" || options.maxLength < 1)) {
errors.push("Maximum length must be a positive number.");
}
if (options.minLength && options.maxLength && options.minLength > options.maxLength) {
errors.push("Minimum length cannot be greater than maximum length.");
}
if (options.blocklist) {
if (!Array.isArray(options.blocklist)) {
errors.push("Blocklist must be an array.");
} else if (options.trimWhitespace !== false) {
options.blocklist = options.blocklist.map((term) => term.trim());
}
}
if (options.matchingSensitivity && typeof options.matchingSensitivity !== "number") {
errors.push("Matching sensitivity must be a number.");
}
if (options.matchingSensitivity && (options.matchingSensitivity < 0 || options.matchingSensitivity > 1)) {
errors.push("Matching sensitivity must be between 0 and 1.");
}
if (options.maxEditDistance && typeof options.maxEditDistance !== "number") {
errors.push("Max tolerance must be a number.");
}
if (options.maxEditDistance && options.maxEditDistance < 0) {
errors.push("Max tolerance must be greater than or equal to 0.");
}
if (options.errorLimit && typeof options.errorLimit !== "number") {
errors.push("Error limit must be a number.");
}
if (options.errorLimit !== void 0 && options.errorLimit < 1) {
errors.push("Error limit must be greater than or equal to 1.");
}
return errors;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
validateInput
});