From fbac4a04a3b7b500cc155bd6b8d015c511cfe75e Mon Sep 17 00:00:00 2001 From: Bruno Bernardino Date: Fri, 1 Sep 2017 13:13:54 +0100 Subject: [PATCH] Adding more mutually-exclusive fields to the checks Applying suggestions from code review. Related to #25 --- .../mutuallyExclusiveFields.js | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/functional-constraints/mutuallyExclusiveFields.js b/lib/functional-constraints/mutuallyExclusiveFields.js index d3f7748..727cd4b 100644 --- a/lib/functional-constraints/mutuallyExclusiveFields.js +++ b/lib/functional-constraints/mutuallyExclusiveFields.js @@ -8,20 +8,25 @@ const jsonschema = require('jsonschema'); // it was harder to read and understand. const incompatibleFields = [ - ['children', 'list'], - ['dict', 'list'], - ['dynamic', 'dict'], - ['dynamic', 'choices'], + ['children', 'list'], // This is actually a Feature Request (https://github.com/zapier/zapier-platform-cli/issues/115) + ['children', 'dict'], // dict is ignored + ['children', 'type'], // type is ignored + ['children', 'placeholder'], // placeholder is ignored + ['children', 'helpText'], // helpText is ignored + ['children', 'default'], // default is ignored + ['dict', 'list'], // Use only one or the other + ['dynamic', 'dict'], // dict is ignored + ['dynamic', 'choices'], // choices are ignored ]; -const collectErrors = (inputFields, path) => { +const verifyIncompatibilities = (inputFields, path) => { const errors = []; _.each(inputFields, (inputField, index) => { - _.each(incompatibleFields, (fieldGroup) => { - if (inputField.hasOwnProperty(fieldGroup[0]) && inputField.hasOwnProperty(fieldGroup[1])) { + _.each(incompatibleFields, ([firstField, secondField]) => { + if (_.has(inputField, firstField) && _.has(inputField, secondField)) { errors.push(new jsonschema.ValidationError( - `must not contain ${fieldGroup[0]} and ${fieldGroup[1]}, as they're mutually exclusive.`, + `must not contain ${firstField} and ${secondField}, as they're mutually exclusive.`, inputField, '/FieldSchema', `instance.${path}.inputFields[${index}]`, @@ -42,7 +47,7 @@ const mutuallyExclusiveFields = (definition) => { if (definition[typeOf]) { _.each(definition[typeOf], (actionDef) => { if (actionDef.operation && actionDef.operation.inputFields) { - errors = errors.concat(collectErrors(actionDef.operation.inputFields, `${typeOf}.${actionDef.key}`)); + errors = [...errors, ...verifyIncompatibilities(actionDef.operation.inputFields, `${typeOf}.${actionDef.key}`)]; } }); }