UNPKG

@unito/integration-debugger

Version:

The Unito Integration Debugger

108 lines (107 loc) 4.01 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const integration_api_1 = require("@unito/integration-api"); const CrawlerDriver = __importStar(require("../crawlerDriver")); /** * Check: That subfields of a readOnly field of type `OBJECT` are also readOnly * * @example * The item "foo" cannot have a readOnly field "bar" with subfields that are not readOnly: * { * name: 'bar', * label: 'BAR', * type: FieldValueType.OBJECT, * readOnly: true, * fields: [ * { * name: 'id', * label: 'ID', * type: FieldValueType.NUMBER, * readOnly: true, * }, * { * name: 'name', * label: 'Name', * type: FieldValueType.STRING, * readOnly: false, // <-- This field cannot be updated since the parent field is readOnly * }, * ], * }, */ const check = { label: 'Schema - Read-only object', prepareOnPreparedSteps: false, validateOnError: false, activatedByDefault: true, prepare: async (_stepResult, _crawlerDriver) => { return []; }, validate: (step, _crawlerDriver) => { if (step.operation !== CrawlerDriver.Operation.GetItem) { return; } for (const [relationIndex, relation] of step.payloadOut.relations.entries()) { validateFields(step, `/relations/${relationIndex}/schema/fields`, relation.schema.fields); } }, }; function validateFields(step, basePath, fields, parent) { for (const [fieldIndex, field] of fields.entries()) { const instancePath = [basePath, fieldIndex].join('/'); if (parent?.readOnly && field.readOnly === false) { step.errors.push({ keyword: 'unito', message: 'A readOnly Object field has subfields that are not readOnly', detailedMessage: `The field '${parent.name}' is readOnly, but its subfield '${field.name}' is not`, instancePath, schemaPath: step.schemaPath ?? '', params: { code: 'READ_ONLY_OBJECT_HAS_NON_READ_ONLY_SUBFIELD', }, }); } if (isObjectField(field)) { // If parent readOnly is true, than children default to true const fieldCopy = structuredClone(field); fieldCopy.readOnly = field.readOnly ?? parent?.readOnly; validateFields(step, instancePath, field.fields, fieldCopy); } } } function isObjectField(field) { return field.type === integration_api_1.FieldValueTypes.OBJECT; } exports.default = check;