简体   繁体   中英

Json Meta Schema : How to restrict another Json Schema from having nested objects

So I am using a Json Meta Schema https://json-schema.org/draft/2019-09/meta/core to further validate JSONSchema using https://github.com/java-json-tools/json-schema-validator

I have a requirement where I have to restrict a schema from having nested objects , like the below schema should be invalid

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "http://example.com/product.schema.json",
  "title": "test",
  "description": "A product from Acme's catalog",
  "type": "object",
  "properties": {
    "productId": {
      "description": "Outer",
      "type": "object",
      "properties": {
        "lineId": {
          "description": "Outer",
          "type": "object"
        }
      }
    }
  }
}

Since productId is an object and it has another object lineId , productId can have only string or number fields but never an object , How to extend the MetaSchema to enforce this. Any help is appreciated

Your question has some version inconsistency, but I'll assume draft-04 since the validator you are using only supports up to draft-04. If you need to do this for other drafts, this procedure will be similar through draft-07. Draft 2019-09 would be more complicated.

  1. Make a copy of the draft-04 meta schema
  2. Remove anything you don't want to be allowed in sub-schemas including the "object" type and any keywords related to objects such as properties .
  3. Change the id to something unique like https://my-project.com/nested-meta-schema .
  4. Make another copy of the draft-04 meta schema
  5. Replace all recursive references ( { "$ref": "#" } ) with references to the schema you just made ( { "$ref": "https://my-project.com/nested-meta-schema" } )
  6. Change the id of the second schema to something unique like https://my-project.com/flat-meta-schema .
  7. For any schema you want to validate against your meta-schema, change the $schema to the id you gave the second meta-schema.

Note that not all implementations support custom meta-schemas, so your mileage may vary.

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