Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Commit

Permalink
Adding more mutually-exclusive fields to the checks
Browse files Browse the repository at this point in the history
Applying suggestions from code review.

Related to #25
  • Loading branch information
Bruno Bernardino committed Sep 1, 2017
1 parent f00a778 commit fbac4a0
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions lib/functional-constraints/mutuallyExclusiveFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}]`,
Expand All @@ -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}`)];
}
});
}
Expand Down

0 comments on commit fbac4a0

Please sign in to comment.