0

enter image description hereI have a json schema looks like below,and I want load the definitions into D and E based on the values of B and C for that I've written allOf conditioning. and i'm using json-schema-validator for json schema validation in application.

i)the below schema always passing as valid because the allOf condition never evaluated and it's not loading validators properties like maxLenth,multipleOf from the definitions.

ii)I was suspecting I did the conditioning in a wrong place(the root schema or sub schema) and i tried moving this allof logic to subschema level(inside the B,C and D,E)

iii)I've tried executing the allOf example mentioned on https://json-schema.org/understanding-json-schema/reference/conditionals.html it is also passing as valid. for this I did verified on a online josn schema validator http://json-schema-validator.herokuapp.com/ which is also using same library json-schema-validator.

iv)is there any ValidationConfiguration requires for JsonSchemaFactory to validate the Draft7 jsonSchema conditioning since the Defaultlibrary is DRAFT-4 on this json-schema-validator.

{
      "$schema": "http://json-schema.org/draft-07/schema#",
      "type": "object",
      "required": [
        "A",
        "B",
        "C",
        "D",
        "E"
      ],
      "properties": {
        "A": {
          "type": "string"
        },
        "B": {
          "type": "string",
          "enum": ["TEST1","TEST2"]
        },
        "C": {
          "type": "string",
          "enum": ["TEST3","TEST4"]
        },
        "D": {
          "type": "object"
        },
        "E": {
          "type": "object"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": { "B": { "const": "TEST1" } }
          },
          "then": {
            "properties": { "D": {  "$ref": "#/definitions/test" } }
          }
        },
        {
          "if": {
            "properties": { "B": { "const": "TEST2" } }
          },
          "then": {
            "properties": { "D": {  "$ref": "#/definitions/testTwo" } }
          }
        },
        {
          "if": {
            "properties": { "C": { "const": "TEST3" } }
          },
          "then": {
            "properties": { "E": {  "$ref": "#/definitions/testThree" } }
          }
        },
        {
          "if": {
            "properties": { "C": { "const": "TEST4" } }
          },
          "then": {
            "properties": { "E": {  "$ref": "#/definitions/test4" } }
          }
        }
      ],

      "definitions": {
       "testOne":{"type":"object"},
       "testTwo":{"type":"object"},
       "testThree":{"type":"object"},
       "testFour":{"type":"object"}
        }
    }

And the javaCode looks like

@PostMapping("/sendMessage")
    public ProcessingReport sendMessage(@RequestBody SampleRequest request) throws IOException, ProcessingException {

        //step-1 writing request object into String
        String requestJson = objectMapper.writeValueAsString(request);

        //Step-2 getting jsonNode for requested json
        JsonNode dataNode = JsonLoader.fromString(requestJson);

        //step -3 creating jsonSchema factory(default)
        JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
        //validating requested jsonNode(dataNode) against SchemaNode(schema of request json,which is loaded from resources)
        ProcessingReport report = factory.getJsonSchema(schemaNode).validate(dataNode);


        //Processing report resulting the given json validation is successful or not
        if(!report.isSuccess()) {
            System.out.println(report);
        }
        return report;
    }
Praneeth
  • 3
  • 3
  • I have found the similar one [Json complex if else](https://stackoverflow.com/questions/55597775/is-it-possible-to-create-a-json-schema-with-allof-multiple-if-and-then-and-re) – Praneeth Nov 14 '19 at 21:51

1 Answers1

1

json-schema-validator only supports draft-03 and draft-04. if/then/const were added in later drafts. Those keywords get ignored resulting in the no-op behavior you're experiencing.

You have two choices

  1. pick a different implementation that supports draft-07
  2. Use the Implication Pattern instead. It's a little more verbose, but the result is the same.
Jason Desrosiers
  • 22,479
  • 5
  • 47
  • 53
  • Thanks for the clarification @Jason.Actually I tried using **org.everit.json.schema** for Draft7 and it's still passing the empty arrays for allOf conditions.I've used the same address example which i mentioned above. – Praneeth Nov 18 '19 at 15:44
  • Please have a look at at [Debugging image(]https://i.stack.imgur.com/gLS47.png) – Praneeth Nov 18 '19 at 15:52
  • I'm not familiar with @everit's implementation, so that image means little to me. I suggest making sure your schema works as expected on https://jsonschema.dev. That way if something doesn't work with your validator, you know the problem is with the validator, not the schema. – Jason Desrosiers Nov 18 '19 at 23:09
  • I did verified my schema on [jsonschema] (https://jsonschema.dev/) and it is passing as valid.As you said the problem is with validator.Please have a look at [my project].(https://github.com/pranee513/json-schema-allOf/tree/master/validator) .Probably i misunderstood the concept of sub-schema conditional design,I was excepting error or exception when i pass the different zip code to the my restcontroller in project here is the [example](https://json-schema.org/understanding-json-schema/reference/conditionals.html) – Praneeth Nov 19 '19 at 15:41
  • Your schemas look fine. I think your problem is just that you haven't included the `$schema` keyword to indicate what draft your schema uses. That validator defaults to draft-04 if there is no `$schema`. You need draft-07 to use `if`/`then`. https://github.com/everit-org/json-schema/#draft-4-draft-6-or-draft-7 – Jason Desrosiers Nov 19 '19 at 19:10
  • Thanks for the all explanations @Jason Desrosiers. I'v tried using [networknt](https://github.com/networknt/json-schema-validator) library so which is able to validate my allOf scenario's and it has details error reports compare to the other libraries. However i am working on the issue with **everit** library I'll post what went wrong in validation code – Praneeth Nov 20 '19 at 20:31