@unito/integration-debugger
Version:
The Unito Integration Debugger
107 lines (106 loc) • 3.7 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const crawler_1 = require("../crawler");
/**
* Check: Update a valid item
*
* When canUpdateItem is set to true, it is assumed one can PATCH a record with the item's path.
* It accepts a Fields object, and must return the updated version of an Item on success with a 200 status code,
* or an Error on failure with a 422 status code.
*
* This check seek to generate a **VALID** payload for a given item and exercise the integration to validate that it can
* update the corresponding Item successfully.
*
*
* @example
* For the following item...
*
* ```json
* {
* "fields": {},
* "relations": [{
* "name": "foos",
* "label": "Foos",
* "path": "/foos",
* "schema": {
* "canCreateItem": false,
* "canCreateItem": true,
* "fields": [
* {
* "name": "id",
* "type": "string",
* "readOnly": true
* },
* {
* "name": "value",
* "type": "string",
* "readOnly": false
* }]
* }
* }]
* }
* ```
*
* ... we try a `PATCH /foos/path` where path points to the first foo Item of the Collection with the following payload,
* where only the field that are not readOnly are present:
*
* ```json
* {
* "value": "<random string>"
* }
*
*/
const check = {
label: 'UpdateItem - Valid Item',
prepareOnPreparedSteps: false,
validateOnError: true,
activatedByDefault: false,
prepare: async (stepResult, crawlerDriver) => {
const step = stepResult.step;
if (step.operation !== crawler_1.Operation.GetCollection) {
return [];
}
const schema = crawlerDriver.getRelationSchema(step.schemaPath ?? '');
const itemSummary = step.payloadOut?.data?.at(0);
if (!schema?.canUpdateItem || !itemSummary) {
return [];
}
// This check only updates the first item of a collection (well, one per page)
return [
{
path: itemSummary.path,
requestSchema: undefined,
schemaPath: step.schemaPath,
parentOperation: step.operation,
parentPath: step.path,
operation: crawler_1.Operation.UpdateItem,
payloadIn: await crawlerDriver.generator.generate(schema.fields, { excludeReadOnly: true }),
headersIn: step.headersIn,
warnings: [],
errors: [],
},
];
},
validate: (step, crawlerDriver) => {
if (step.operation !== crawler_1.Operation.UpdateItem) {
return;
}
const updatedItem = step.payloadIn;
// If the generated Item to update was empty, it means the relationSchema has canUpdateItem = true, but every fields
// are marked readOnly. It is considered an error for now (may be changed to warning if you really have a use case)
if (updatedItem && Object.keys(updatedItem).length === 0) {
const schema = crawlerDriver.getRelationSchema(step.schemaPath ?? '');
step.errors.push({
keyword: 'unito',
message: `A Relation Schema (${schema?.label}) has canUpdateItem = true, but all its fields are readOnly.`,
detailedMessage: `The relation schema ${schema?.label} should have at least one non readOnly field to be updatable.`,
instancePath: step.path,
schemaPath: step.schemaPath ?? '',
params: {
code: 'NO_UPDATABLE_FIELD_ON_UPDATABLE_ITEM',
},
});
}
},
};
exports.default = check;
;