So much technology, do you want to see the test of JSON schema?

Source: Internet
Author: User

Catalogue 1. What is JSON schema?2. How to define a JSON Schema3. How to test JSON schema     a) using the JSON schema validator GUI     

What is JSON Schema?

JSON schemas are specifications that define JSON data structures based on JSON format.

    • Describe the existing data format
    • Clean human and machine readable documentation
    • To complete the structure validation, the user
      • Automated testing
      • Validating client-Submitted data
How to define a JSON SchemaA simple example

JSON Data is as follows

"Hello, World"

The JSON Schema is defined as

{  "type": "String"}

With this schema, we can validate the JSON data.

The JSON Schema generated from data has a ready-made tool that can be used http://jsonschema.net/#/

# #转载注明出处: http://www.cnblogs.com/wade-xu/p/4662127.html

Next, look at a basic JSON Schema
{  "$schema": "http://json-schema.org/draft-04/schema#",  "title": "User",  "description": "Demo schema",  "type": "Object",  "Properties": {"    firstName": {      "type": "String"    },    "LastName": {      " Type ":" String "    },    " age ": {      " description ":" Years ","      Type ":" Integer ",      " minimum ": 0    }  },  "required": [    "FirstName",    "LastName"  ]}

Let's take a look at the various important keywords that can be used in this pattern:

More keywords can refer to http://json-schema.org/latest/json-schema-validation.html

or http://tools.ietf.org/html/draft-zyp-json-schema-03#page-11

# #转载注明出处: http://www.cnblogs.com/wade-xu/p/4662127.html

How to test JSON Schema

Before we write a JSON schema to validate the data submitted by the client, we have to make sure that the JSON schema we write is correct, and of course we can construct some data to reverse verify whether our JSON schema is correct or not.

There are more than 30 JSON Schema validator implemented in various languages on the web, and we use the very popular in Java, the GitHub address here.

Using the JSON Schema validator GUI

Address http://json-schema-validator.herokuapp.com/

Validation results: success

Validation results: failure

The information for Error message is very detailed.

# #转载注明出处: http://www.cnblogs.com/wade-xu/p/4662127.html

Use JSON Schema validator in Java code

Maven pom.xml Configuration

        <Dependency>              <groupId>Com.fasterxml.jackson.core</groupId>              <Artifactid>Jackson-core</Artifactid>              <version>2.3.0</version>          </Dependency>
<Dependency> <groupId>Com.fasterxml.jackson.core</groupId> <Artifactid>Jackson-databind</Artifactid> <version>2.3.0</version> </Dependency>
<Dependency> <groupId>Com.github.fge</groupId> <Artifactid>Json-schema-validator</Artifactid> <version>2.2.6</version> </Dependency>

1 import Com.fasterxml.jackson.databind.JsonNode; 2 import Com.github.fge.jackson.JsonNodeReader; 3 import com.github.fge.jsonschema.core.report.ProcessingMessage; 4 import Com.github.fge.jsonschema.core.report.ProcessingReport; 5 Import com.github.fge.jsonschema.main.JsonSchemaFactory;

Validation Success

1 @Test2      Public voidValidate_driverset_schema () {3 4         //some code to create Json schema, which we want to use data to validate5         6Jsonnode schema = Readjsonfile ("Src/test/resources/json/schema.json");7         8Jsonnode data = Readjsonfile ("Src/test/resources/json/datagoodexample.json");9 TenProcessingreport report = One Jsonschemafactory.bydefault (). Getvalidator (). validateunchecked (schema, data); A assert.asserttrue (Report.issuccess ()); -}

# #转载注明出处: http://www.cnblogs.com/wade-xu/p/4662127.html

Validation Failure

1 @Test2     //Wrong Data3      Public voidValidate_driverset_schema2 () {4 5         //some code to create Json schema, which we want to use data to validate6         7Jsonnode data = Readjsonfile ("Src/test/resources/json/databadexample.json");8Jsonnode schema = Readjsonfile ("Src/test/resources/json/schema.json");9 TenProcessingreport report = One Jsonschemafactory.bydefault (). Getvalidator (). validateunchecked (schema, data); A Assert.assertfalse (Report.issuccess ()); -  -         //assert error message theIterator<processingmessage> it =report.iterator (); - Assert.assertequals ( -"Instance type (string) does not match any allowed primitive type (allowed: [\" Integer\ "])", - It.next (). GetMessage ()); +  -     } +  A     Privatejsonnode readjsonfile (String filePath) { atJsonnode instance =NULL; -         Try { -Instance =NewJsonnodereader (). Fromreader (NewFileReader (FilePath)); -}Catch(FileNotFoundException e) { - e.printstacktrace (); -}Catch(IOException e) { in e.printstacktrace (); -         } to         returninstance; +}

 

Reference Documents

Official Document: http://json-schema.org/

GitHub: json-schema-validator

Thank you for reading, if you think the content of this article is helpful to your study, you can click the Recommendation button at the bottom right, your encouragement is the motive force of my creation.

# #转载注明出处: http://www.cnblogs.com/wade-xu/p/4662127.html

So much technology, do you want to see the test of JSON schema?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.