Merge/Patch for schema with inner ref question
flipmokid opened this issue · 1 comments
Hi,
I've started using JSON schema in a project and it's all working well until I've tried using merge patch. There doesn't seem to be too many examples out there regarding merge/patch for cases where the schema being modified has references inside it so I was hoping I could get some guidance. I have a test schema:
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);
console.log("Start with base schema ================================")
const a = {
"$id": "a.json#",
type: "object",
properties: {
size: {type: "number"}
},
required: ["size"],
additionalProperties: false
}
ajv.addSchema(a, "a.json")
A simple merge works okay
console.log("Simple Merge Test =====================================")
const b = {
"$id": "b.json#",
$merge: {
source: { "$ref": "a.json#" },
with: {
properties: {
length: { type: "number" }
},
required: ["length"],
additionalProperties: false
}
}
}
const bv = ajv.compile(b);
console.log(bv({ length: 3, size: 4}))
however, if I create a schema with a reference to the first one inside and try to extend the inner schema via the outer schema it fails:
const c = {
"$id": "c.json#",
type: "object",
properties: {
inner: { "$ref": "a.json#" }
}
}
console.log("Nested Ref Merge test ==================================")
const d = {
"$id": "d.json#",
$merge: {
source: { "$ref": "c.json#" },
with: {
properties: {
inner: {
properties: {
length: { type: "number" }
},
required: ["length"]
}
}
}
}
}
const dv = ajv.compile(d)
I believe this is due to refs not being replaced in the schema. Firstly I was wondering if what I am doing is odd or not - the main reason I was doing it this way is because in my actual scenario I have multiple properties based off of the same schema but only want to merge additional properties into some of them.
If it's not odd then how would I get the above to work?
Many thanks
I started using json-schema-deref with my specs and it's working okay now so I'm closing this. Apologies for the noise!