.reduce(({ patches=[], replaced=[] }, patch) => { const { path, op } = patch , { authorityID, periodID, attribute } = PatchType.fromPatch(patch) const isSimpleAttributeChange = ( (op === 'add' || op === 'remove') && path.endsWith(`/${attribute}`) ) if (!attribute || isSimpleAttributeChange) { return { patches: patches.concat(patch), replaced } } const attributePointer = pointer.compile( periodID ? ['authorities', authorityID, 'periods', periodID, attribute] : ['authorities', authorityID, attribute] ) return replaced.includes(attributePointer) ? { patches, replaced } : { patches: patches.concat({ op: 'add', path: attributePointer, value: pointer.get(after, attributePointer) }), replaced: replaced.concat(attributePointer) } }, { patches: [], replaced: [] })
.then((content) => { if (isDefine) { const filename = path.basename(targetFilePath); const refPath = addDefinition(config, rootObj || obj, content, filename); const parsedClone = parsed.slice(); parsedClone.splice(-1, 1, config.referenceToken); const newPartPath = pointer.compile(parsedClone); pointer.remove(obj, partPath); pointer.set(obj, newPartPath, refPath); return; } const parsedClone = parsed.slice(); parsedClone.splice(-1, 1); const newPartPath = pointer.compile(parsedClone); pointer.set(obj, newPartPath, content); return; });
Observer.prototype.transform = function(change) { var key = String(change.name || change.index) var path = this.paths.get(change.object).concat(key) var record = { root: this.root, path: pointer.compile(path) } // the original change record ist not extensible -> copy for (var prop in change) { record[prop] = change[prop] } // unobserve deleted/replaced objects var deleted = change.oldValue && [change.oldValue] || change.removed || [] deleted.forEach(function(oldValue) { if (!oldValue || typeof oldValue !== 'object') { return } if (!comparePaths(this.paths.get(oldValue), path)) { return } this.unobserve(oldValue) }, this) // observe added/updated objects var value = change.object[key] if (typeof value === 'object') { var desc = Object.getOwnPropertyDescriptor(change.object, key) if (desc.enumerable === true) { this.observe(value, path) } else { this.unobserve(value) } } Object.preventExtensions(record) return record }
const results = Array.from({ length: data.length }, (_, index) => { const item = data[index]; let pathArray = []; if (item.property === null) { pathArray = []; } else if ( Array.isArray(item.property) && item.property.length === 1 && [null, undefined].includes(item.property[0]) ) { pathArray = []; } else { pathArray = item.property; } return { pointer: jsonPointer.compile(pathArray), message: item.message, severity: 'error' }; });
validateSchemaV4() { const result = tv4.validateMultiple(this.data, this.schema); const validationErrors = result.errors.concat(result.missing); const amandaCompatibleError = { length: validationErrors.length, errorMessages: {} }; for (let index = 0; index < validationErrors.length; index++) { const validationError = validationErrors[index]; let error; if (validationError instanceof Error) { error = validationError; } else { error = new Error('Missing schema'); error.params = { key: validationError }; error.dataPath = ''; } const pathArray = jsonPointer .parse(error.dataPath) .concat(error.params.key || []); const pointer = jsonPointer.compile(pathArray); amandaCompatibleError[index] = { property: pathArray, attributeValue: true, message: `At '${pointer}' ${error.message}`, validatorName: 'error' }; } this.errors = new ValidationErrors(amandaCompatibleError); return this.errors; }
ops = ops.map(function (op) { op.path = pointer.compile(op.path.map(function (p) { return "" + p; })); return op; });
_.each(errors, function (error) { var parentPath = jp.compile(_.dropRight(error.path)); var path = jp.compile(error.path); var parentValue = jp(swagger, parentPath); var value = jp(swagger, path); var newValue; switch(error.code) { case 'EQUIVALENT_PATH': swagger['x-hasEquivalentPaths'] = true; break; case 'MISSING_PATH_PARAMETER_DEFINITION': var field = error.message.match(/: (.+)$/)[1]; newValue = _.clone(value); newValue.parameters = value.parameters || []; newValue.parameters.push({ name: field, type: 'string', in: 'path', required: true }); break; case 'OBJECT_MISSING_REQUIRED_PROPERTY_DEFINITION': newValue = _.clone(value); newValue.required = []; _.each(value.required, function (name) { if (!_.isUndefined(value.properties[name])) newValue.required.push(name); }); if (_.isEmpty(newValue.required)) delete newValue.required; break; case 'ONE_OF_MISSING': if (value.in === 'path' && !value.required) { newValue = _.clone(value) newValue.required = true; } break; case 'UNRESOLVABLE_REFERENCE': if (typeof swagger.definitions[value] !== 'undefined') newValue = '#/definitions/' + value; break; case 'DUPLICATE_OPERATIONID': //FIXME: find better solutions than strip all 'operationId' jsonPath.apply(swagger, '$.paths[*][*].operationId', function (value) { return undefined; }); fixed = true; break; case 'OBJECT_MISSING_REQUIRED_PROPERTY': if (error.message === 'Missing required property: version') { newValue = _.clone(value); newValue.version = '1.0.0'; break; } if (value.type === 'array' && _.isUndefined(value.items)) { newValue = _.clone(value); newValue.items = {}; } break; case 'ENUM_MISMATCH': case 'INVALID_FORMAT': case 'INVALID_TYPE': if (_.last(error.path) === 'default') { var type = parentValue.type; if (_.isString(value) && !_.isUndefined(type) && type !== 'string') { try { newValue = JSON.parse(value); } catch (e) {} } delete parentValue.default; //TODO: add warning break; } var match = error.message.match(/^Expected type (\w+) but found type (\w+)$/); if (match && match[2] === 'string') { try { var tmp = JSON.parse(value); if (typeof tmp === match[1]) { newValue = tmp; fixed = true; break; } } catch (e) {} } } if (!_.isUndefined(newValue)) { jp(swagger, path, newValue); fixed = true; } });