This repository has been archived by the owner on May 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Adding mutually-exclusive fields functional validation #25
Merged
BrunoBernardino
merged 2 commits into
master
from
feature-warn-on-mutually-exclusive-fields
Sep 4, 2017
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
const jsonschema = require('jsonschema'); | ||
|
||
// NOTE: While it would be possible to accomplish this with a solution like | ||
// https://stackoverflow.com/questions/28162509/mutually-exclusive-property-groups#28172831 | ||
// it was harder to read and understand. | ||
|
||
const incompatibleFields = [ | ||
['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 verifyIncompatibilities = (inputFields, path) => { | ||
const errors = []; | ||
|
||
_.each(inputFields, (inputField, index) => { | ||
_.each(incompatibleFields, ([firstField, secondField]) => { | ||
if (_.has(inputField, firstField) && _.has(inputField, secondField)) { | ||
errors.push(new jsonschema.ValidationError( | ||
`must not contain ${firstField} and ${secondField}, as they're mutually exclusive.`, | ||
inputField, | ||
'/FieldSchema', | ||
`instance.${path}.inputFields[${index}]`, | ||
'invalid', | ||
'inputFields' | ||
)); | ||
} | ||
}); | ||
}); | ||
|
||
return errors; | ||
}; | ||
|
||
const mutuallyExclusiveFields = (definition) => { | ||
let errors = []; | ||
|
||
_.each(['triggers', 'searches', 'creates'], (typeOf) => { | ||
if (definition[typeOf]) { | ||
_.each(definition[typeOf], (actionDef) => { | ||
if (actionDef.operation && actionDef.operation.inputFields) { | ||
errors = [...errors, ...verifyIncompatibilities(actionDef.operation.inputFields, `${typeOf}.${actionDef.key}`)]; | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
return errors; | ||
}; | ||
|
||
module.exports = mutuallyExclusiveFields; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
'use strict'; | ||
|
||
require('should'); | ||
const schema = require('../../schema'); | ||
|
||
describe('mutuallyExclusiveFields', () => { | ||
|
||
it('should not error on fields not mutually exclusive', () => { | ||
const definition = { | ||
version: '1.0.0', | ||
platformVersion: '1.0.0', | ||
creates: { | ||
foo: { | ||
key: 'foo', | ||
noun: 'Foo', | ||
display: { | ||
label: 'Create Foo', | ||
description: 'Creates a...', | ||
}, | ||
operation: { | ||
perform: '$func$2$f$', | ||
inputFields: [ | ||
{key: 'orderId', type: 'number'}, | ||
{ | ||
key: 'line_items', | ||
children: [ | ||
{ | ||
key: 'product', type: 'string', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const results = schema.validateAppDefinition(definition); | ||
results.errors.should.have.length(0); | ||
}); | ||
|
||
it('should error on fields that have children and list', () => { | ||
const definition = { | ||
version: '1.0.0', | ||
platformVersion: '1.0.0', | ||
creates: { | ||
foo: { | ||
key: 'foo', | ||
noun: 'Foo', | ||
display: { | ||
label: 'Create Foo', | ||
description: 'Creates a...', | ||
}, | ||
operation: { | ||
perform: '$func$2$f$', | ||
inputFields: [ | ||
{key: 'orderId', type: 'number'}, | ||
{ | ||
key: 'line_items', | ||
children: [ | ||
{ | ||
key: 'product', | ||
}, | ||
], | ||
list: true, | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const results = schema.validateAppDefinition(definition); | ||
results.errors.should.have.length(1); | ||
results.errors[0].stack.should.eql('instance.creates.foo.inputFields[1] must not contain children and list, as they\'re mutually exclusive.'); | ||
}); | ||
|
||
it('should error on fields that have list and dict', () => { | ||
const definition = { | ||
version: '1.0.0', | ||
platformVersion: '1.0.0', | ||
creates: { | ||
foo: { | ||
key: 'foo', | ||
noun: 'Foo', | ||
display: { | ||
label: 'Create Foo', | ||
description: 'Creates a...', | ||
}, | ||
operation: { | ||
perform: '$func$2$f$', | ||
inputFields: [ | ||
{key: 'orderId', type: 'number'}, | ||
{ | ||
key: 'line_items', | ||
dict: true, | ||
list: true, | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const results = schema.validateAppDefinition(definition); | ||
results.errors.should.have.length(1); | ||
results.errors[0].stack.should.eql('instance.creates.foo.inputFields[1] must not contain dict and list, as they\'re mutually exclusive.'); | ||
}); | ||
|
||
it('should error on fields that have dynamic and dict', () => { | ||
const definition = { | ||
version: '1.0.0', | ||
platformVersion: '1.0.0', | ||
creates: { | ||
foo: { | ||
key: 'foo', | ||
noun: 'Foo', | ||
display: { | ||
label: 'Create Foo', | ||
description: 'Creates a...', | ||
}, | ||
operation: { | ||
perform: '$func$2$f$', | ||
inputFields: [ | ||
{ | ||
key: 'orderId', | ||
type: 'number', | ||
dynamic: 'foo.id.number', | ||
dict: true, | ||
}, | ||
{ | ||
key: 'line_items', | ||
list: true, | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const results = schema.validateAppDefinition(definition); | ||
results.errors.should.have.length(1); | ||
results.errors[0].stack.should.eql('instance.creates.foo.inputFields[0] must not contain dynamic and dict, as they\'re mutually exclusive.'); | ||
}); | ||
|
||
it('should error on fields that have dynamic and choices', () => { | ||
const definition = { | ||
version: '1.0.0', | ||
platformVersion: '1.0.0', | ||
creates: { | ||
foo: { | ||
key: 'foo', | ||
noun: 'Foo', | ||
display: { | ||
label: 'Create Foo', | ||
description: 'Creates a...', | ||
}, | ||
operation: { | ||
perform: '$func$2$f$', | ||
inputFields: [ | ||
{ | ||
key: 'orderId', | ||
type: 'number', | ||
dynamic: 'foo.id.number', | ||
choices: { | ||
uno: 1, | ||
dos: 2 | ||
}, | ||
}, | ||
{ | ||
key: 'line_items', | ||
list: true, | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const results = schema.validateAppDefinition(definition); | ||
results.errors.should.have.length(1); | ||
results.errors[0].stack.should.eql('instance.creates.foo.inputFields[0] must not contain dynamic and choices, as they\'re mutually exclusive.'); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is simpler indeed than the stackoverflow :).
Would it be helpful if we added comments on why the combinations are not available (comments would suffice)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point, but screen estate is limited for the errors, there. Do you have any suggestions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I meant inside the code.