Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

Commit

Permalink
Merge pull request #584 from stormpath/fix/registration-field-validation
Browse files Browse the repository at this point in the history
Fix field validation
  • Loading branch information
robertjd committed Feb 3, 2017
2 parents 8d2ced8 + 3bb57c8 commit 80cf1a4
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
26 changes: 24 additions & 2 deletions lib/helpers/validate-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,34 @@ var getRequiredRegistrationFields = require('./get-required-registration-fields'
*/
module.exports = function (formData, stormpathConfig, callback) {
var accountFieldNames = Object.keys(formData);
var customFieldNames = formData.customData
? Object.keys(formData.customData)
: [];
var errors = [];

// Considers `0` and `false` valid values
function isSet(value) {
return value !== '' && value !== null && typeof value !== 'undefined';
}

function isFieldIncluded(field) {
// Is it included directly in the core object?
if (accountFieldNames.indexOf(field.name) >= 0 && isSet(formData[field.name])) {
return true;
}

// Is it included in the custom data?
if (customFieldNames.indexOf(field.name) >= 0 && isSet(formData.customData[field.name])) {
return true;
}

return false;
}

getRequiredRegistrationFields(stormpathConfig, function (requiredFields) {
async.each(requiredFields, function (field, cb) {
if (accountFieldNames.indexOf(field.name) <= -1 || (accountFieldNames.indexOf(field.name) > -1 && !formData[field.name])) {
errors.push(new Error((field.label || field.label) + ' required.'));
if (!isFieldIncluded(field)) {
errors.push(new Error((field.label || field.name) + ' required.'));
}

cb();
Expand Down
52 changes: 49 additions & 3 deletions test/helpers/test-validate-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ describe('validateAccount', function () {
confirmPassword: {
enabled: true,
required: true
},
color: {
enabled: true,
required: true,
label: 'Color'
}
}
}
Expand All @@ -42,7 +47,26 @@ describe('validateAccount', function () {
surname: 'Degges',
email: 'randall@stormpath.com',
password: 'FASRbaBjkrqJSNVlUrV2ZyUy5iUX8UEZ3TW3nejX',
confirmPassword: 'FASRbaBjkrqJSNVlUrV2ZyUy5iUX8UEZ3TW3nejX'
confirmPassword: 'FASRbaBjkrqJSNVlUrV2ZyUy5iUX8UEZ3TW3nejX',
customData: {
color: 'purple'
}
};

helpers.validateAccount(accountData, config, function (errors) {
assert.equal(errors, null);
done();
});
});

it('should support custom data properties on the root object', function (done) {
var accountData = {
givenName: 'Randall',
surname: 'Degges',
email: 'randall@stormpath.com',
password: 'FASRbaBjkrqJSNVlUrV2ZyUy5iUX8UEZ3TW3nejX',
confirmPassword: 'FASRbaBjkrqJSNVlUrV2ZyUy5iUX8UEZ3TW3nejX',
color: 'purple'
};

helpers.validateAccount(accountData, config, function (errors) {
Expand All @@ -57,7 +81,10 @@ describe('validateAccount', function () {
surname: 'Degges',
email: 'randall@stormpath.com',
password: 'woot',
confirmPassword: 'woothi'
confirmPassword: 'woothi',
customData: {
color: 'purple'
}
};

helpers.validateAccount(accountData, config, function (errors) {
Expand All @@ -70,12 +97,31 @@ describe('validateAccount', function () {
it('should return the right number of errors if errors are present', function (done) {
var accountData = {
givenName: 'Randall',
surname: 'Degges'
surname: 'Degges',
customData: {
color: 'purple'
}
};

helpers.validateAccount(accountData, config, function (errors) {
assert.equal(errors.length, 3);
done();
});
});

it('should correctly validate required fields by also looking into custom data', function (done) {
var accountData = {
givenName: 'Randall',
surname: 'Degges',
email: 'randall@stormpath.com',
password: 'FASRbaBjkrqJSNVlUrV2ZyUy5iUX8UEZ3TW3nejX',
confirmPassword: 'FASRbaBjkrqJSNVlUrV2ZyUy5iUX8UEZ3TW3nejX'
};

helpers.validateAccount(accountData, config, function (errors) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message, 'Color required.');
done();
});
});
});

0 comments on commit 80cf1a4

Please sign in to comment.