Simple JSON validation facility using Draft v4, v6 and v7. This is oriented to be executed as a part of a batch process, on a server or as part of integrated tests. It can be used to test your API in development server or local files.
- Preferences configuration with a queue to process several JSONs.
- Validator can fetch local files or make http/https requests.
- Remote API requests can be configured with custom headers.
- Errors reported in output:
- Not found / IO Error.
- Invalid Json (i.e. malformed json).
- Invalid against schema.
- Maven 3 (for development only)
- java 1.8 compatible compiler to run.
- Clone and execute bundled examples:
git clone https://github.com/lpenap/json-schema-validator.git
mvn exec:java
- Download latest release and execute
java -jar json-validator-0.X-jar-with-dependencies.jar
After executing the bundled examples, you can see the following output in the console:
INFO c.p.json.validator.CliLauncher - Starting json-schema-validator
INFO c.p.json.validator.CliLauncher - Preferences file was not specified, running bundled examples.
INFO c.p.json.validator.JSONValidator - Validating 2 elements in queue...
INFO c.p.json.validator.JSONValidator - /example/com.example.api.json -> [OK] !
INFO c.p.json.validator.JSONValidator - https://jsonplaceholder.typicode.com/users -> [OK] !
INFO c.p.json.validator.CliLauncher - Done json-schema-validator
In order to validate each of your desired JSONs, you need to get a working schema v4, v6 or v7 for each of your JSONs. You can use JSONSchema.net to generate a schema from a valid Json.
Example:
{
"version": 2,
"queue": [
{
"is_remote": false,
"is_array": false,
"path": "/example/com.example.api.json",
"schema": "/example/com.example.api-schema.json"
},
{
"is_remote": true,
"is_array": true,
"path": "https://jsonplaceholder.typicode.com/users",
"schema": "/example/com.typicode.jsonplaceholder-users.json",
"method": "GET",
"headers": [
{
"key": "Cache-Control",
"value": "no-cache"
},
{
"key": "Accept",
"value": "application/json"
}
]
}
]
}
Queue objects:
is_remote
:true
for specifies a remote JSON/API.false
for a file in the filesystem.is_array
: Iftrue
, the root Json object present inpath
will be treated as an array.path
: Local filesystem path or URL (include the protocol, i.e. http, https).schema
: Local path of the JSON schema to be used.method
(optional): Method to be used whenis_remote
istrue
(defaults to GET).headers
(optional): Custom headers to be included when invoking a remote API.
mvn exec:java -Dexec.arguments="path/to/preferences.json"
java -jar json-validator-0.X-jar-with-dependencies.jar "path/to/preferences.json"
mvn install
The jar file will be located in target/json-validator-0.X-jar-with-dependencies.jar