Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate JSON schema in java? [duplicate]

I try to check user detail object with use JSON schema. But I don't know how to check JSON object in Java.

My Schema:

{
     "type" : "object",
     "properties" : {
     "first_name" : {
                     "type" : "string" , 
                     "minLength"  : 3 , 
                     "maxLength" : 255 
                  }, 
   "last_name" : {
                     "type" : "string" , 
                     "minLength"  : 3 , 
                     "maxLength" : 255 
                  },
    "age"       : { 
                     "type" : "integer" , 
                     "minimum" : 16 ,
                      "maximum" : 40
                  },
    "phone_number" : {
                        "type" : "integer",
                        "pattern" : "[6-9][0-9]{9}"
                     } ,
     "email"     : { 
                       "type" : "string",
                       "pattern" : "[a-z0-9]+"
                   } , 
     "password" : { 
                       "type" : "string" ,
                       "minLength" : 7 ,
                       "maxLength" : 255 ,
                       "pattern" : "^.{7, 255}$"
                  } , 
      "gender" : { "enum" : ["Male" , "Female"] }   
},

"required" : ["first_name","last_name" , "age"  ,"email" , "password" 
, "gender" ]
}

My Sample Input:

{
"first_name" : "Sample" ,
"last_name" : "Name" ,
"age"  : 19,
"gender" : "Male",
"phone_number"  :  9080245591,
"email" : "[email protected]",
"password" : "uni=versity"
}

Any one can say how to check this input with use JSON schema in Java.

like image 777
Obeth Samuel Avatar asked Jan 17 '18 07:01

Obeth Samuel


2 Answers

The json-schema-validator in GitHub, Perhaps It will help you to check the json object in java.

like image 159
holten Avatar answered Oct 17 '22 12:10

holten


You can use FasterXML jackson with the module:json-schema-validator as holten proposed.

Include it in maven: com.github.java-json-tools json-schema-validator 2.2.8

Complementary to this, to generate schema from object instead of writing it manually you can also use another module: https://github.com/FasterXML/jackson-module-jsonSchema

I may add a functional example if needed

like image 11
pdem Avatar answered Oct 17 '22 11:10

pdem