Skip to content

Commit d89afd5

Browse files
committed
Adds predefined validator .password()
1 parent 04ee79f commit d89afd5

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

lib/Validators.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,30 @@ validators.unique = function (msg) {
118118
};
119119
};
120120

121+
validators.password = function (checks, msg) {
122+
if (!msg) {
123+
msg = checks;
124+
checks = "luns6"; // (l)owercase, (u)ppercase, (n)umber, (s)pecial characters, (6) min length
125+
}
126+
if (!msg) {
127+
msg = "weak-password";
128+
}
129+
var m = checks.match(/([0-9]+)/);
130+
var min_len = (m ? parseInt(m[1], 10) : null);
131+
132+
return function (v, next) {
133+
if (!v) return next(msg);
134+
135+
if (checks.indexOf("l") >= 0 && !v.match(/[a-z]/)) return next(msg);
136+
if (checks.indexOf("u") >= 0 && !v.match(/[A-Z]/)) return next(msg);
137+
if (checks.indexOf("n") >= 0 && !v.match(/[0-9]/)) return next(msg);
138+
if (checks.indexOf("s") >= 0 && !v.match(/[^a-zA-Z0-9]/)) return next(msg);
139+
if (min_len !== null && min_len > v.length) return next(msg);
140+
141+
return next();
142+
};
143+
};
144+
121145
/**
122146
* Pattern validators are usually based on regular
123147
* expressions and solve more complicated validations

test/integration2/predefined-validators.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,38 @@ describe("Predefined Validators", function () {
268268
});
269269

270270

271+
describe("password()", function () {
272+
it("should pass 'Passw0r∂'", function (done) {
273+
validators.password()('Passw0r∂', checkValidation(done));
274+
});
275+
it("should not pass 'password' with 'weak-password'", function (done) {
276+
validators.password()('password', checkValidation(done, 'weak-password'));
277+
});
278+
it("should not pass 'Passw0rd' with 'weak-password'", function (done) {
279+
validators.password()('Passw0rd', checkValidation(done, 'weak-password'));
280+
});
281+
});
282+
283+
284+
describe("password('ln4', 'bad-pwd')", function () {
285+
it("should pass 'Passw0r∂'", function (done) {
286+
validators.password('ln4', 'bad-pwd')('Passw0r∂', checkValidation(done));
287+
});
288+
it("should pass 'Passw0rd'", function (done) {
289+
validators.password('ln4', 'bad-pwd')('Passw0rd', checkValidation(done));
290+
});
291+
it("should not pass 'P12345' with 'bad-pwd'", function (done) {
292+
validators.password('ln4', 'bad-pwd')('P12345', checkValidation(done, 'bad-pwd'));
293+
});
294+
it("should not pass 'password' with 'bad-pwd'", function (done) {
295+
validators.password('ln4', 'bad-pwd')('password', checkValidation(done, 'bad-pwd'));
296+
});
297+
it("should not pass 'p12' with 'bad-pwd'", function (done) {
298+
validators.password('ln4', 'bad-pwd')('p12', checkValidation(done, 'bad-pwd'));
299+
});
300+
});
301+
302+
271303
describe("patterns.hexString()", function () {
272304
it("should pass 'ABCDEF0123456789'", function (done) {
273305
validators.patterns.hexString()('ABCDEF0123456789', checkValidation(done));

0 commit comments

Comments
 (0)