简体   繁体   中英

Can a JSON Validator inspect candidate json for type definitions to validate itself?

I have a DML that supports custom structures like "customType1" below and which can be used for defining the type of a list of properties:

{
 "types": {
    "customType1" : {
      "var1" : "string",
      "var2" : "int"
    }
    .
    .
    .
 },
 "properties": {
   "prop1" : {
     "type": "customType1",
     "value": {
     "var1" : "Hello",
     "var2" : 123
    },
   "prop2" : {
     "type" : "String",
     "value" : "www.google.com"
    }
    .
    .
    .
 }
}

Is it possible to write a JSON Schema that can evaluate prop1 to validate it against the structure defined in customType1 ? The description of custom types specified in types would not be known a priori , only at the time of evaluation.

If this is beyond the capabilities of the JSON Schema specification, any other suggestions for how to validate it?

You can do it using a JSON reference:

{
 "types": {
    "customType1" : {
      "var1" : "string",
      "var2" : "int"
    }
    .
    .
    .
 },
 "properties": {
   "prop1" : {
     "$ref" : "#/types/customType1"
    },
   "prop2" : {
     "type" : "String",
     "value" : "www.google.com"
    }
    .
    .
    .
 }
}

it is a "pointer" denoting the place of the property schema.

(Note: in JSON Schema we mostly use the key "definitions" instead of "types" by convention).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM