From feb6dc0df3e0e1490a1a4da28d7e2e9a043aa7d4 Mon Sep 17 00:00:00 2001 From: Sam Richard Date: Tue, 9 Aug 2016 17:17:59 -0400 Subject: [PATCH 1/3] :new: Validate against Required Save/Publish Resolves https://github.com/punchcard-cms/content-types/issues/84 --- lib/form/validate.js | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/form/validate.js b/lib/form/validate.js index 1c17954..5a79940 100644 --- a/lib/form/validate.js +++ b/lib/form/validate.js @@ -203,9 +203,18 @@ const buildValues = working => { * * @returns {true|FormInputValues} - Returns `true` if there are no validation or required issues, {FormInputValues} with error messages as values if there are */ -const valueCheck = (input, settings, validation) => { - if (input.target.hasOwnProperty('required') && (input.target.required === 'save' || input.target.required === 'publish') && input.target.value === '') { - return 'Field cannot be left blank!'; +const valueCheck = (input, settings, validation, required) => { + // Check for Required + if (input.target.hasOwnProperty('required') && input.target.value === '') { + // Only require a publishable field if we're checking against publish + if (required === 'publish' && input.target.required === 'publish') { + return 'Field is required to be published!'; + } + + // Only return require save fields + if (input.target.required === 'save') { + return 'Field is required to be saved!'; + } } if (settings.repeatable.hasOwnProperty('min') && ((!Array.isArray(input.all) && settings.repeatable.min > 1) || (Array.isArray(input.all) && input.all.length < settings.repeatable.min))) { @@ -220,10 +229,21 @@ const valueCheck = (input, settings, validation) => { * * @param {FormInputValues} raw - Raw form input * @param {InputType} type - Individual input type + * @param {string} check - Either `save`, `publish` to validate required correctly * * @returns {true|FormInputValues} - Returns `true` if there are no validation issues, {FormInputValues} with error messages as values if there are */ -const validate = (raw, type) => { +const validate = (raw, type, check) => { + let required = check; + + if (typeof required === 'undefined') { + required = 'publish'; + } + + if (typeof required !== 'string' || ['save', 'publish'].indexOf(required) < 0) { + throw new Error('Parameter `check` must either be `save` or `publish`'); + } + let working = split(raw); const allSettings = buildSettings(type); const allRepeatables = buildRepeatables(type); @@ -257,7 +277,7 @@ const validate = (raw, type) => { } // Run the validation function - result.validation = valueCheck(input, settings, plugin.validation[plugin.inputs[0][result.input].validation.function]); + result.validation = valueCheck(input, settings, plugin.validation[plugin.inputs[0][result.input].validation.function], required); } // check if input is required, supersede main plugin @@ -267,7 +287,7 @@ const validate = (raw, type) => { } // Run the validation function - result.validation = valueCheck(input, settings, plugin.validation[plugin.inputs[result.input].validation.function]); + result.validation = valueCheck(input, settings, plugin.validation[plugin.inputs[result.input].validation.function], required); } return result; From 761f35e85b0d2ae705b4635895c97d0b1860cbd4 Mon Sep 17 00:00:00 2001 From: Sam Richard Date: Tue, 9 Aug 2016 17:18:19 -0400 Subject: [PATCH 2/3] :white_check_mark: Add tests for Required validation --- tests/fixtures/content-types/baz.yml | 14 ++++---- tests/validate.js | 53 ++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/tests/fixtures/content-types/baz.yml b/tests/fixtures/content-types/baz.yml index b3fb4f7..d7c112f 100644 --- a/tests/fixtures/content-types/baz.yml +++ b/tests/fixtures/content-types/baz.yml @@ -11,7 +11,7 @@ attributes: inputs: text: settings: - empty: false + empty: true - type: text name: Input required save id: input-required-save @@ -20,7 +20,7 @@ attributes: text: required: 'save' settings: - empty: false + empty: true - type: text name: Plugin required publish id: plugin-required-publish @@ -29,7 +29,7 @@ attributes: inputs: text: settings: - empty: false + empty: true - type: text name: Input required publish id: input-required-publish @@ -38,7 +38,7 @@ attributes: text: required: 'publish' settings: - empty: false + empty: true - type: text name: Plugin required bad level id: plugin-required-bad-level @@ -47,7 +47,7 @@ attributes: inputs: text: settings: - empty: false + empty: true - type: text name: Input required bad level id: input-required-bad-level @@ -56,7 +56,7 @@ attributes: text: required: 'smublish' settings: - empty: false + empty: true - type: quote name: MULTI Plugin required save id: multi-plugin-required-save @@ -70,7 +70,7 @@ attributes: author: required: 'publish' settings: - empty: false + empty: true - type: selects-related name: Two related select elements id: input-related-selects diff --git a/tests/validate.js b/tests/validate.js index 4f75bd7..bbddd3e 100644 --- a/tests/validate.js +++ b/tests/validate.js @@ -259,7 +259,7 @@ test('Required - Pass', t => { }); }); -test('Required - Fail', t => { +test('Required Publish with Empty Save', t => { return types.only('baz').then(ct => { const input = { 'plugin-required-save--text': '', @@ -267,8 +267,8 @@ test('Required - Fail', t => { }; const expected = { - 'plugin-required-save--text': 'Field cannot be left blank!', - 'input-required-save--text': 'Field cannot be left blank!', + 'plugin-required-save--text': 'Field is required to be saved!', + 'input-required-save--text': 'Field is required to be saved!', }; const result = validation(input, ct); @@ -276,3 +276,50 @@ test('Required - Fail', t => { t.deepEqual(result, expected, 'Returns an object of inputs that have failed'); }); }); + +test('Required Publish with Empty Publish and Save', t => { + return types.only('baz').then(ct => { + const input = { + 'plugin-required-save--text': '', + 'input-required-publish--text': '', + }; + + const expected = { + 'plugin-required-save--text': 'Field is required to be saved!', + 'input-required-publish--text': 'Field is required to be published!', + }; + + const result = validation(input, ct); + + t.deepEqual(result, expected, 'Returns an object of inputs that have failed'); + }); +}); + + +test('Required Save with Empty Publish', t => { + return types.only('baz').then(ct => { + const input = { + 'plugin-required-publish--text': '', + 'input-required-publish--text': '', + }; + + const result = validation(input, ct, 'save'); + + t.true(result, 'No errors for just publish on save'); + }); +}); + +test('Validation fails if required check is wrong', t => { + return types.only('baz').then(ct => { + const input = { + 'plugin-required-publish--text': '', + 'input-required-publish--text': '', + }; + + validation(input, ct, 'foo'); + + t.fail(); + }).catch(e => { + t.is(e.message, 'Parameter `check` must either be `save` or `publish`', 'Errors out as expected'); + }); +}); From e989d96682c4f3debaf998584dda7749b6b3ec8b Mon Sep 17 00:00:00 2001 From: Sam Richard Date: Tue, 9 Aug 2016 17:25:20 -0400 Subject: [PATCH 3/3] :memo: Update JSDoc for valueCheck --- lib/form/validate.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/form/validate.js b/lib/form/validate.js index 5a79940..722108d 100644 --- a/lib/form/validate.js +++ b/lib/form/validate.js @@ -200,6 +200,8 @@ const buildValues = working => { * * @param {object} input - Input Type being tested * @param {ValidationSettings} settings - All validation settings + * @param {function} validation - The validation function to run + * @param {string} required - The required check to run (save/publish) * * @returns {true|FormInputValues} - Returns `true` if there are no validation or required issues, {FormInputValues} with error messages as values if there are */