Skip to content

Commit

Permalink
extract password validation so users can access the validation within…
Browse files Browse the repository at this point in the history
… their own code for things like password reset
  • Loading branch information
export-mike authored and molomby committed Oct 16, 2017
1 parent 2ee049c commit 3551765
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions fields/types/password/PasswordType.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,37 +161,44 @@ password.prototype.compare = function (item, candidate, callback) {
* Asynchronously confirms that the provided password is valid
*/
password.prototype.validateInput = function (data, callback) {
var detail = '';
var result = true;
var min = this.options.min;
var max = this.options.max || 72;
var max = this.options.max;
var complexity = this.options.complexity;
var confirmValue = this.getValueFromData(data, '_confirm');
var passwordValue = this.getValueFromData(data);
if (confirmValue !== undefined
&& passwordValue !== confirmValue) {

var validation = validate(passwordValue, confirmValue, min, max, complexity);

utils.defer(callback, validation.result, validation.detail);
};

var validate = password.validate = function (pass, confirm, min, max, complexity) {
var detail = '';
var result = true;
max = max || 72;
if (confirm !== undefined
&& pass !== confirm) {
detail = 'passwords must match\n';
}

if (min && typeof passwordValue === 'string' && passwordValue.length < min) {
if (min && typeof pass === 'string' && pass.length < min) {
detail += 'password must be longer than ' + min + ' characters\n';
}

if (max && typeof passwordValue === 'string' && passwordValue.length > max) {
if (max && typeof pass === 'string' && pass.length > max) {
detail += 'password must not be longer than ' + max + ' characters\n';
}

for (var prop in complexity) {
if (complexity[prop] && typeof passwordValue === 'string') {
var complexityCheck = (regexChunk[prop]).test(passwordValue);
if (complexity[prop] && typeof password === 'string') {
var complexityCheck = (regexChunk[prop]).test(pass);
if (!complexityCheck) {
detail += detailMsg[prop] + '\n';
}
}
}
result = detail.length === 0;

utils.defer(callback, result, detail);
return { result, detail };
};

/**
Expand Down

0 comments on commit 3551765

Please sign in to comment.