-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PDE-4989 feat(schema): Add support for bulk writes
- Loading branch information
Showing
8 changed files
with
270 additions
and
2 deletions.
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
45 changes: 45 additions & 0 deletions
45
packages/schema/lib/functional-constraints/bulkWriteConstraints.js
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,45 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
const jsonschema = require('jsonschema'); | ||
|
||
const bulkWriteConstraints = (definition) => { | ||
const errors = []; | ||
const actionType = 'creates'; | ||
|
||
if (definition[actionType]) { | ||
_.each(definition[actionType], (actionDef) => { | ||
if (actionDef.operation && actionDef.operation.bulk) { | ||
if (!actionDef.operation.performBulk) { | ||
errors.push( | ||
new jsonschema.ValidationError( | ||
'must contain property "performBulk" because property "bulk" is present.', | ||
actionDef.operation, | ||
'/BasicCreateActionOperationSchema', | ||
`instance.${actionType}.${actionDef.key}.operation`, | ||
'invalid', | ||
'performBulk' | ||
) | ||
); | ||
} | ||
|
||
if (actionDef.operation.perform) { | ||
errors.push( | ||
new jsonschema.ValidationError( | ||
'must not contain property "perform" because it is mutually exclusive with property "bulk".', | ||
actionDef.operation, | ||
'/BasicCreateActionOperationSchema', | ||
`instance.${actionType}.${actionDef.key}.operation`, | ||
'invalid', | ||
'perform' | ||
) | ||
); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
return errors; | ||
}; | ||
|
||
module.exports = bulkWriteConstraints; |
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
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,48 @@ | ||
'use strict'; | ||
|
||
const makeSchema = require('../utils/makeSchema'); | ||
|
||
module.exports = makeSchema({ | ||
id: '/BulkObjectSchema', | ||
description: | ||
'Zapier uses this configuration for writing in bulk.', | ||
type: 'object', | ||
required: ['groupedBy', 'limit'], | ||
properties: { | ||
groupedBy: { | ||
description: | ||
'The list of keys of input fields to group bulk-write with. The actual user data provided for the fields will be used during execution. Note that a required input field should be referenced to get user data always.', | ||
type: 'array', | ||
minItems: 1, | ||
}, | ||
limit: { | ||
description: | ||
'The maximum number of items to call performBulk with.', | ||
type: 'integer', | ||
}, | ||
}, | ||
examples: [ | ||
{ | ||
groupedBy: ['workspace', 'sheet'], | ||
limit: 100, | ||
}, | ||
], | ||
antiExamples: [ | ||
{ | ||
example: { | ||
groupedBy: [], | ||
limit: 100, | ||
}, | ||
reason: 'Empty groupedBy list provided: `[]`.', | ||
}, | ||
{ | ||
example: {groupedBy: ['workspace']}, | ||
reason: 'Missing required key: `limit`.', | ||
}, | ||
{ | ||
example: {limit: 1}, | ||
reason: 'Missing required key: `groupedBy`.', | ||
}, | ||
], | ||
additionalProperties: false, | ||
}); |
105 changes: 105 additions & 0 deletions
105
packages/schema/test/functional-constraints/bulkWriteConstraints.js
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,105 @@ | ||
'use strict'; | ||
|
||
require('should'); | ||
const schema = require('../../schema'); | ||
|
||
describe('bulkWriteConstraints', () => { | ||
it('should error on creates with both bulk and perform defined', () => { | ||
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$', | ||
performBulk: '$func$2$f$', | ||
bulk: { | ||
groupedBy: ['orderId'], | ||
limit: 10, | ||
}, | ||
sample: { id: 1 }, | ||
inputFields: [ | ||
{ key: 'orderId', type: 'number' }, | ||
], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const results = schema.validateAppDefinition(definition); | ||
results.errors.should.have.length(1); | ||
results.errors[0].stack.should.eql( | ||
'instance.creates.foo.operation must not contain property "perform" because it is mutually exclusive with property "bulk".' | ||
); | ||
}); | ||
|
||
it('should error on creates with bulk but no performBulk defined', () => { | ||
const definition = { | ||
version: '1.0.0', | ||
platformVersion: '1.0.0', | ||
creates: { | ||
foo: { | ||
key: 'foo', | ||
noun: 'Foo', | ||
display: { | ||
label: 'Create Foo', | ||
description: 'Creates a...', | ||
}, | ||
operation: { | ||
bulk: { | ||
groupedBy: ['orderId'], | ||
limit: 10, | ||
}, | ||
sample: { id: 1 }, | ||
inputFields: [ | ||
{ key: 'orderId', type: 'number' }, | ||
], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const results = schema.validateAppDefinition(definition); | ||
results.errors.should.have.length(1); | ||
results.errors[0].stack.should.eql( | ||
'instance.creates.foo.operation must contain property "performBulk" because property "bulk" is present.' | ||
); | ||
}); | ||
|
||
it('should not error on creates with both bulk and performBulk but no perform defined', () => { | ||
const definition = { | ||
version: '1.0.0', | ||
platformVersion: '1.0.0', | ||
creates: { | ||
foo: { | ||
key: 'foo', | ||
noun: 'Foo', | ||
display: { | ||
label: 'Create Foo', | ||
description: 'Creates a...', | ||
}, | ||
operation: { | ||
performBulk: '$func$2$f$', | ||
bulk: { | ||
groupedBy: ['orderId'], | ||
limit: 10, | ||
}, | ||
sample: { id: 1 }, | ||
inputFields: [ | ||
{ key: 'orderId', type: 'number' }, | ||
], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const results = schema.validateAppDefinition(definition); | ||
results.errors.should.have.length(0); | ||
}); | ||
}); |
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