I have a json schema defining several properties. I've moved 2 of the properties to definitions and I make references to them. I did this because I wanted to group them together and do some testing for these properties in a generic way. This works fine and all the json data is handled as before.
But, I noticed that when I read the json schema file into my javascript file, I only see the last $ref. I don't know what the cause of this is. I'd really need to know all of the properties that are referenced.
Here's an snippet of my json schema (in file schemas/schema1.json):
{
"type": "object",
"properties": {
"$ref": "#/definitions/groupedProperties/property1",
"$ref": "#/definitions/groupedProperties/property2"
},
"definitions": {
"groupedProperties": {
"type": "object",
"properties": {
"property1": {
"type": "string"
},
"property2": {
"type": "string"
}
}
}
}
}
Then I'm reading it into my js file like this (in file test.js):
var schemas = requireDir('./schemas')
for (var prop in schemas['schema1'].properties) {
console.log(prop)
}
When I iterate over the properties in the schema from my js file, all I can see is one $ref. I imagine this is because it thinks the property name is '$ref' and there can be only unique names. Is there a certain way I need to require this file so that the first $ref doesn't get clobbered?
EDIT: My syntax wasn't passing the json schema validators, although I'm not sure why, so instead of struggling with that, I decided to do it a bit differently. All I wanted was a way to group certain properties, so I put the properties back in the main schema, and changed the definition to be just an enum of the property names comprising the group. So now my schema looks like:
{
"type": "object",
"properties": {
"property1": {
"type": "string"
},
"property2": {
"type": "string"
}
},
"definitions": {
"groupedProperties": {
"enum": ["property1", "property2"]
}
}
}
And then in my js file:
var myGroup = (schema.definitions ? schema.definitions.groupedProperties : [])
console.log(myGroup.enum) // [ 'property1', 'property2' ]
This has nothing to do with
require, object keys are not unique (in that you may declare them several times in one object), but they are overwritable (in the same way that a variable declared twice is overwritable). You will only receive the last value declared on two keys with the same name.I'd suggesting giving the refs a distinguishing ID, this will also aid clarity when your code expands