0
votes

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

1
You very rarely want to extend a meta-schema. Could you elaborate on WHY you believe this is the thing you want to do, and what your original objective is? This might by an XY type problem. - Relequestual
Additionally I notice you've linked to the draft 2019-09 meta-schema, while your schema defines you're using draft-07. Which do you plan to use? - Relequestual
Also, the implementation you're using only supports draft-03 and draft-04. - Jason Desrosiers

1 Answers

1
votes

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.