java-json-tools / json-schema-validator

A JSON Schema validation implementation in pure Java, which aims for correctness and performance, in that order
http://json-schema-validator.herokuapp.com/
Other
1.63k stars 399 forks source link

Question: Json schema Validation for a simple array object. #247

Closed lvimalchandran closed 6 years ago

lvimalchandran commented 6 years ago

Below are the Json schema and its data file. but when I validated against each other, it throws schema validation error.

Error Message: instance type (array) does not match any allowed primitive type (allowed: ["object"])

Schema:


    "$schema": "http://json-schema.org/draft-04/schema#",
    "id": "http://example.com/example.json",    
    "properties": {
        "CompositeLoanContainer":{
            "id": "/properties/CompositeLoanContainer",
            "properties": {
                "PartyContainer": {"type": "array", "id": "/properties/CompositeLoanContainer/properties/PartyContainer",
                                   "items": {"$ref": "#/properties/PartyContainer"}
                },
                "Property": {"id": "/properties/CompositeLoanContainer/properties/Property","$ref": "cdm_json_mini.json#/properties/Property"}
            },
            "type": "object"
        },
        "PartyContainer":{
            "id": "/properties/PartyContainer",
            "properties": {
                "Party": {"id": "/properties/PartyContainer/properties/Party","$ref": "cdm_json_mini.json#/properties/Party"},
                "PartyAddress": {"id": "/properties/PartyContainer/properties/PartyAddress","$ref": "cdm_json_mini.json#/properties/PartyAddress"}
            },
            "type": "object"
        } 
    }
}```

**Data File:**
```{
  "PartyContainer" : [ {
    "Party" : {
      "PartyIdentifier" : "800900100",
      "PartyType" : "Borrower"
    },
    "PartyAddress" : {
      "AddressIdentifier" : 1,
      "AddressType" : "mailing",
      "HeadquartersIndicator" : "N"
    }
  } ],
  "Property" : {
    "PropertyBuiltYearDate" : 1990.0,
    "PropertyName" : "Fairfield",
    "PropertyOccupancyType" : "lease"
  }
}```

The above data file is generated using the pojo class of the schema(which is mentioned above) wit the help of jackson2. Please let me know if I am missing anything above.

Thanks, Vimal.
ghsatpute commented 6 years ago

@lvimalchandran I think your data file is not valid according to your schema.

There are two PartyContainer

  1. At $.CompositeLoanContainer.PartyContainer: This is an array type. But in your schema file, it's at the root level.
  2. The second one is at $.PartyContainer: This is the object type. But in your schema file, it's an array.

I think you can correct the things, then your file can be validated against your schema.

Just a tip: Whenever you're adding code to Github, add triple backticks (```) before and after the code. This makes it easily readable for other people.

lvimalchandran commented 6 years ago

Thanks @ghsatpute. yes you are right. I corrected my schema file by adding 's' to the PartyContainer at the root level. and then generated pojo and json data file. Now the validation looks good against the schema.

yes sure here after I will post code with (```).