-
Notifications
You must be signed in to change notification settings - Fork 187
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
9 changed files
with
439 additions
and
6 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
98 changes: 98 additions & 0 deletions
98
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,98 @@ | ||
'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`, | ||
'missing', | ||
'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' | ||
) | ||
); | ||
} | ||
|
||
if (actionDef.operation.bulk.groupedBy) { | ||
const requiredInputFields = []; | ||
const inputFields = _.get(actionDef, ['operation', 'inputFields'], []); | ||
inputFields.forEach((inputField) => { | ||
if (inputField.required) { | ||
requiredInputFields.push(inputField.key); | ||
} | ||
}); | ||
|
||
actionDef.operation.bulk.groupedBy.forEach((field, index) => { | ||
if (!requiredInputFields.includes(field)) { | ||
errors.push( | ||
new jsonschema.ValidationError( | ||
`cannot use optional or non-existent inputField "${field}".`, | ||
actionDef.operation.bulk, | ||
'/BulkObjectSchema', | ||
`instance.${actionType}.${actionDef.key}.operation.bulk.groupedBy[${index}]`, | ||
'invalid', | ||
'groupedBy' | ||
) | ||
); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
if (actionDef.operation && actionDef.operation.performBulk) { | ||
if (!actionDef.operation.bulk) { | ||
errors.push( | ||
new jsonschema.ValidationError( | ||
'must contain property "bulk" because property "performBulk" is present.', | ||
actionDef.operation, | ||
'/BasicCreateActionOperationSchema', | ||
`instance.${actionType}.${actionDef.key}.operation`, | ||
'missing', | ||
'bulk' | ||
) | ||
); | ||
} | ||
|
||
if (actionDef.operation.perform) { | ||
errors.push( | ||
new jsonschema.ValidationError( | ||
'must not contain property "perform" because it is mutually exclusive with property "performBulk".', | ||
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
30 changes: 30 additions & 0 deletions
30
packages/schema/lib/functional-constraints/requirePerformConditionally.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,30 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
const jsonschema = require('jsonschema'); | ||
|
||
const requirePerformConditionally = (definition) => { | ||
const errors = []; | ||
const actionType = 'creates'; | ||
|
||
if (definition[actionType]) { | ||
_.each(definition[actionType], (actionDef) => { | ||
if (actionDef.operation && !actionDef.operation.bulk && !actionDef.operation.performBulk && !actionDef.operation.perform) { | ||
errors.push( | ||
new jsonschema.ValidationError( | ||
'requires property "perform".', | ||
actionDef.operation, | ||
'/BasicCreateActionOperationSchema', | ||
`instance.${actionType}.${actionDef.key}.operation`, | ||
'required', | ||
'perform' | ||
) | ||
); | ||
} | ||
}); | ||
} | ||
|
||
return errors; | ||
}; | ||
|
||
module.exports = requirePerformConditionally; |
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. **Note** that it is capped by the platform to prevent exceeding the [AWS Lambda\'s request/response payload size quota of 6 MB](https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html#function-configuration-deployment-and-execution). Also, the execution is time-bound; we recommend reducing it upon consistent timeout.', | ||
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, | ||
}); |
Oops, something went wrong.