ajv-validator / ajv-merge-patch

$merge and $patch keywords for Ajv JSON-Schema validator to extend schemas

Home Page:https://ajv.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nested merge and patch

flipmokid opened this issue · comments

Hi,

I'm not sure if this is an issue with my understand of the spec or not. I understand merge a lot better than patch and so I'd like to use it where possible and then patch things I can't fix with merge. However, I'm having difficulty applying a patch to a schema who's source I apply a merge:

deliver.json

{
  "type": "object",
  "definitions": {
      "settings": {
          "type": "object",
          "properties": {
              "country": { 
                "type": "array", 
                "items": { "type": "string" } 
              },
              "client": { 
                "type": "array", 
                "items": { 
                  "type": "string", 
                  "enum": ["mobile", "desktop"] 
                }
              }
          },
          "additionalProperties": false
      }
  },
  "properties": {
    "include": { "$ref": "#/definitions/settings" },
    "exclude": { "$ref": "#/definitions/settings" },
    "templateParameters": { 
      "type": "object", 
      "required": [], 
      "patternProperties": { 
        ".*": { "type": "string" } 
      } 
    },
    "expire": { "type": "string", "format": "date" }
  },
  "required": [],
  "additionalProperties": false
}

and my new schema:

x.json

{
  "$patch": {
    "source" : {
      "$merge": {
        "source": { "$ref": "deliver.json" },
        "with": {
          "properties": {
            "templateParameters": {
              "properties": {
                "link": { "type": "string" }
              },
              "required": ["link"]
            }
          },
          "required": ["templateParameters"]
        }
      }
    },
    "with": [{
      "op": "replace",
      "path": "/properties/include/properties/client/items",
      "value": { "type": "string", "enum": [ "alpha", "bravo" ] }
    }]
  }
}

I get the error message:

{ [OPERATION_PATH_UNRESOLVABLE: Cannot perform the operation at a path that does not exist]
  message: 'Cannot perform the operation at a path that does not exist',
  name: 'OPERATION_PATH_UNRESOLVABLE',
  index: 0,
  operation:
   { op: 'replace',
     path: '/properties/include/properties/client/items',
     value: { type: 'string', enum: [Array] } },
  tree: { '$merge': { source: [Object], with: [Object] } } }

Is this expected? I'm not sure if this kind of thing is allowed under the specification but if it were then I think the order the transformations are applied would need to be altered.

Thanks

"/properties/include" is the object "{ "$ref": "#/definitions/settings" }", it is not replaced with the contents of /definitions/settings. The error says that this object doesn't have property "properties/client/items" - it indeed does not.

In general, "$ref" is delegation, not inclusion. From this point of view using "$ref" inside "$merge" was a mistake, as inside merge it actually includes the schema.

Hi,

Sorry, I should have been more clear. That's my schema definitions but before I run the merge/patch I resolve all of the references using a library called json-schema-deref. So when I run the merge and patch there are definitely no $ref's in the schema.

I just thought that the error message saying

/properties/include/properties/client/items

couldn't be found was because the inner merge may not have been applied yet. The tree inside the exception message (my last script block in the first message) gives the indication that the inner transform may not have been applied as it appears to still include the $merge property as the top level element:

tree: { '$merge': { source: [Object], with: [Object] } } }

I've tried running the merge on its own and the patch without the inner merge but instead with the original schema and both of those work okay.

A small test case I've come up with

const Ajv = require("ajv")
const ajv = new Ajv({allErrors: true, v5: true})
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json'));
require('ajv-merge-patch')(ajv);

const schema = {
  $merge: {
    source: {
      type: "object",
      properties: {
        hello: { type: "string" }
      },
      required: ["hello"],
      additionalProperties: false
    },
    with: {
      properties: {
        goodbye: { type: "string" }
      },
      required: ["goodbye"]
    }
  }
}

const justMergeValidator = ajv.compile(schema)
console.log(justMergeValidator({ hello: "", goodbye: "" }) === true)
console.log(justMergeValidator({ hello: "" }) === false)

const schemaPatch = {
  $patch: {
    source: {
      type: "object",
      properties: {
        hello: { type: "string" }
      },
      required: ["hello"],
      additionalProperties: false
    },
    with: [{
      op: "replace",
      path: "/additionalProperties",
      value: true
    }]
  }
}
const justPatchValidator = ajv.compile(schemaPatch)
console.log(justPatchValidator({ hello: "", goodbye: "" }) === true)

const schemaMergePatch = {
  $patch: {
    source: {
      $merge: {
        source: {
          type: "object",
          properties: {
            hello: { type: "string" }
          },
          required: ["hello"],
          additionalProperties: false
        },
        with: {
          properties: {
            goodbye: { type: "string" }
          },
          required: ["goodbye"]
        }
      }
    },
    with: [{
      op: "replace",
      path: "/additionalProperties",
      value: true
    }]
  }
}
try {
  const mergePatchValidator = ajv.compile(schemaMergePatch)
  console.log(mergePatchValidator({ hello: "", goodbye: "" }) === true)
  console.log(mergePatchValidator({ hello: "", goodbye: "", addtional: "" }) === true)
  console.log(mergePatchValidator({ hello: "", addtional: "" }) === false)
}
catch(e) {
  console.error(e)
}

It outputs:

C:\git\xyz>node .\src\xyz.js
true
true
jsonpatch.apply is deprecated, please use `applyPatch` for applying patch sequences, or `applyOperation` to apply individual operations.
true
jsonpatch.apply is deprecated, please use `applyPatch` for applying patch sequences, or `applyOperation` to apply individual operations.
{ [OPERATION_PATH_UNRESOLVABLE: Cannot perform the operation at a path that does not exist]
  message: 'Cannot perform the operation at a path that does not exist',
  name: 'OPERATION_PATH_UNRESOLVABLE',
  index: 0,
  operation: { op: 'replace', path: '/additionalProperties', value: true },
  tree: { '$merge': { source: [Object], with: [Object] } } }

it doesn't process them from the inside, the outside keywords are processed first.