Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

Requiring a JSON property that uses the value of another property as a key

I have a structure that I’d like to validate with a JSON schema, of which this is an example:

  {
  "$schema": "./schemas/demo-schema.json",
  "Configs": {
    "MyConfig": {
        "Option1": true,
        "ReferencedOption": [ "HelloWorld" ]
    },
    "CloneConfig": "MyConfig",
    "LinkedConfig": {
        "Option1": false,
        "ReferencedOption": "MyConfig"
    }
  },
  "Option2": 1
}

Properties within the Configs object can use any keys, and the value of each must be an object or a string. So far pretty easy. But:

? If the value of a property in Configs is a string then there must be a property with that key in Configs.

? If the value in Configs is an object it must have a ReferencedOption property within it, which can take either an array or a string as a value. If it’s a string then there must be a property with that string as a key in Configs.

I’ve tried a lot of approaches using required, if/else, dependencies, but have not found any way to say that a property named for the value of a property is required in the same or the parent object.

I realise that I could construct the object differently to make this reasonably easy, but this project is to evaluate adopting JSON as a replacement for a pre-existing hierarchy of key/value pairs. The ideal solution would be to use the structure as-is with a simple re-encoding into JSON, and also to achieve robust validation.

Thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...