From b357c39b8718e6d47f4913699da532062b3ab0af Mon Sep 17 00:00:00 2001 From: Luka Skukan Date: Mon, 23 Jan 2017 12:50:16 +0100 Subject: [PATCH 1/5] Fix issue with adding custom required fields when registering user --- lib/helpers/validate-account.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/helpers/validate-account.js b/lib/helpers/validate-account.js index 6d2e924d..a07745e4 100644 --- a/lib/helpers/validate-account.js +++ b/lib/helpers/validate-account.js @@ -22,11 +22,33 @@ 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])) { + if (!isFieldIncluded(field)) { errors.push(new Error((field.label || field.label) + ' required.')); } From de3f0534097183fc95277a18e79e478d36062c84 Mon Sep 17 00:00:00 2001 From: Luka Skukan Date: Mon, 23 Jan 2017 12:55:00 +0100 Subject: [PATCH 2/5] Fix minor issue with validation error text --- lib/helpers/validate-account.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/helpers/validate-account.js b/lib/helpers/validate-account.js index a07745e4..ecac3ff8 100644 --- a/lib/helpers/validate-account.js +++ b/lib/helpers/validate-account.js @@ -49,7 +49,7 @@ module.exports = function (formData, stormpathConfig, callback) { getRequiredRegistrationFields(stormpathConfig, function (requiredFields) { async.each(requiredFields, function (field, cb) { if (!isFieldIncluded(field)) { - errors.push(new Error((field.label || field.label) + ' required.')); + errors.push(new Error((field.label || field.name) + ' required.')); } cb(); From 8281049aef0ee770d4568e35564d231b1e25f8be Mon Sep 17 00:00:00 2001 From: Luka Skukan Date: Mon, 23 Jan 2017 12:55:27 +0100 Subject: [PATCH 3/5] Add test for failing custom data validation --- test/helpers/test-validate-account.js | 36 ++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/test/helpers/test-validate-account.js b/test/helpers/test-validate-account.js index a3d9c02b..eed48f2f 100644 --- a/test/helpers/test-validate-account.js +++ b/test/helpers/test-validate-account.js @@ -29,6 +29,11 @@ describe('validateAccount', function () { confirmPassword: { enabled: true, required: true + }, + color: { + enabled: true, + required: true, + label: 'Color' } } } @@ -42,7 +47,10 @@ describe('validateAccount', function () { surname: 'Degges', email: 'randall@stormpath.com', password: 'FASRbaBjkrqJSNVlUrV2ZyUy5iUX8UEZ3TW3nejX', - confirmPassword: 'FASRbaBjkrqJSNVlUrV2ZyUy5iUX8UEZ3TW3nejX' + confirmPassword: 'FASRbaBjkrqJSNVlUrV2ZyUy5iUX8UEZ3TW3nejX', + customData: { + color: 'purple' + } }; helpers.validateAccount(accountData, config, function (errors) { @@ -57,7 +65,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) { @@ -70,7 +81,10 @@ 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) { @@ -78,4 +92,20 @@ describe('validateAccount', function () { 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 is required'); + done(); + }); + }); }); From 71b41cc3115fcfd545d9a4b5ad4ddaf4d0eb3543 Mon Sep 17 00:00:00 2001 From: Luka Skukan Date: Mon, 23 Jan 2017 13:06:36 +0100 Subject: [PATCH 4/5] Fix error message check in test --- test/helpers/test-validate-account.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/helpers/test-validate-account.js b/test/helpers/test-validate-account.js index eed48f2f..884e4547 100644 --- a/test/helpers/test-validate-account.js +++ b/test/helpers/test-validate-account.js @@ -104,7 +104,7 @@ describe('validateAccount', function () { helpers.validateAccount(accountData, config, function (errors) { assert.equal(errors.length, 1); - assert.equal(errors[0].message, 'Color is required'); + assert.equal(errors[0].message, 'Color required.'); done(); }); }); From 3bb57c8cbc1fc121ecbe333457ca7726d8fa556c Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 3 Feb 2017 14:39:49 -0800 Subject: [PATCH 5/5] add a test for custom data on root of object --- test/helpers/test-validate-account.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/helpers/test-validate-account.js b/test/helpers/test-validate-account.js index 884e4547..e17b37f4 100644 --- a/test/helpers/test-validate-account.js +++ b/test/helpers/test-validate-account.js @@ -59,6 +59,22 @@ describe('validateAccount', function () { }); }); + 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) { + assert.equal(errors, null); + done(); + }); + }); + it('should return errors if errors are present', function (done) { var accountData = { givenName: 'Randall',