简体   繁体   中英

JSON schema validation using java - ref

I am trying to validate my JSON using ( https://github.com/fge/json-schema-validator ) schema validator.

  1. Do you recommend using jackson schema generation to generate JSON schemas or is there a better way ?

  2. I have an object called (Location) which has list of objects (BaseObject). I created a schema for location like this with a $ref to BaseObject. But validation fails with the error message - ["": domain: validation; keyword: properties; message: required property(ies) not found; missing: ["id","refName"]; required: ["id","refName"]]

Is there a mistake in the way I used the refs ?

Location.json - schema

{
   "type":"object",
   "properties":{
      "locationType":{
         "type":"string"
      },
      "mapsRefs":{
          "$ref": "file://localhost/c:/baseobject.json" 
         }
      }
   }
}

baseobject.json - schema

{
   "type":"object",
   "properties":{
      "refName":{
         "type":"string",
          "required":true
      },
      "id":{
         "type":"integer",
          "required":true
      },
      "refs":{
         "type":"array",
          "required":false,
         "items":{
            "type":"string"
         }
      }
   }

}

To answer your first question, Jackson in my experience is the most easy to use and documented API to handle JSON on java.

For the second question you define the "id" and "refName" as required, you are either validating with the wrong schema, or not passing the required properties.

This does look a lot like this closed issue on github: https://github.com/fge/json-schema-validator/issues/22

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