Go to file
Tengiz Sharafiev 992fa11736 Add empty header from nlohmann/json adapter
Based on json11_adapter.hpp
2016-02-02 17:50:35 +03:00
doc Initial commit. 2013-10-30 07:51:11 +11:00
examples Include exception details when parsing fails in external_schema example 2015-10-13 13:04:14 +11:00
include/valijson Add empty header from nlohmann/json adapter 2016-02-02 17:50:35 +03:00
tests Disable json11 unless VALIJSON_BUILD_CXX11_ADAPTERS macro is defined. 2016-01-28 13:16:08 +09:00
thirdparty Add nlohman/json library 2016-02-02 17:50:35 +03:00
xcode/valijson.xcodeproj Update builds in Xcode project to use project defaults for deployment target and whether to strip debug symbols 2016-01-29 17:31:43 -08:00
.gitignore Update CMake build to include external_schema example, and add usage info to external_schema.cpp. 2014-06-15 15:24:26 +10:00
.travis.yml Move Travis CI build steps to separate script that can build/run tests with C++11 enabled (when compiled with clang) 2016-01-27 10:12:05 -08:00
CMakeLists.txt Add nlohman/json library 2016-02-02 17:50:35 +03:00
Doxyfile Disable auto-linking in Doxygen 2015-05-07 09:08:35 +10:00
LICENSE Initial commit. 2013-10-30 07:51:11 +11:00
README.md Update README to include link to json11 2016-02-01 11:38:50 -08:00
travis.sh Suppress output from pushd and popd in travis build script 2016-01-27 10:16:14 -08:00
valgrind.supp Add Valgrind suppressions file for Mac OS X system libraries 2015-10-19 11:54:52 +11:00

Valijson Build Status

Overview

Valijson is a header-only JSON Schema Validation library for C++.

Valijson provides a simple validation API that allows you load JSON Schemas, and validate documents loaded by one of several supported parser libraries.

Project Goals

The goal of this project is to support validation of all constraints available in JSON Schema v4, while being competitive with the performance of hand-written JSON validators.

JSON References

The library is intended to include support for both local and remote JSON References. This feature is currently a work in progress and is subject to change. The current implementation attempts to resolve JSON References while parsing a JSON Schema, but this has proven ineffective for several use cases and loses some of the flexibility intended by the JSON Reference and JSON Schema specifications.

There is a branch of the project that is intended to fix these issues by implementing a two phase schema parser. This parser would first build a graph from a JSON document by resolving any references, then load a JSON Schema model by traversing that graph. This will hopefully be complete in the near future.

Usage

The following code snippets show how you might implement a simple validator using RapidJson as the underlying JSON Parser.

Include the necessary headers:

#include <rapidjson/document.h>

#include <valijson/adapters/rapidjson_adapter.hpp>
#include <valijson/utils/rapidjson_utils.hpp>
#include <valijson/schema.hpp>
#include <valijson/schema_parser.hpp>
#include <valijson/validator.hpp>

Loading a schema using RapidJSON:

// Load JSON document using RapidJSON with Valijson helper function
rapidjson::Document mySchemaDoc;
if (!valijson::utils::loadDocument("mySchema.json", mySchemaDoc)) {
    throw std::runtime_error("Failed to load schema document");
}

// Parse JSON schema content using valijson
Schema mySchema;
SchemaParser parser;
RapidJsonAdapter mySchemaAdapter(mySchemaDoc);
parser.populateSchema(mySchemaAdapter, mySchema);

Load a document to validate:

rapidjson::Document myTargetDoc;
if (!valijson::utils::loadDocument("myTarget.json", myTargetDoc)) {
    throw std::runtime_error("Failed to load target document");
}

Validate a document:

Validator validator(mySchema);
RapidJsonAdapter myTargetAdapter(myTargetDoc);
if (!validator.validate(myTargetAdapter, NULL)) {
    std::runtime_error("Validation failed.");
}

Note that Valijson's SchemaParser and Validator classes expect you to pass in a RapidJsonAdapter rather than a rapidjson::Document. This is due to the fact that SchemaParser and Validator are template classes that can be used with any of the JSON parsers supported by Valijson.

Test Suite

Valijson's' test suite currently contains several hand-crafted tests and uses the standard JSON Schema Test Suite to test support for parts of the JSON Schema feature set that have been implemented.

cmake

The examples and test suite can be built using cmake:

# Build examples and test suite
mkdir build
cd build
cmake ..
make

# Run test suite (from build directory)
./test_suite

Xcode

An Xcode project has also been provided, in the 'xcode' directory. Note that in order to run the test suite, you may need to configure the working directory for the 'test_suite' scheme. It is recommended that you use the 'xcode' directory as the working directory.

The Xcode project has been configured so that /usr/local/include is in the include path, and /usr/local/lib is in the library path. These are the locations that homebrew installed Boost on my test system.

Examples

Building the Valijson Test Suite, using the instructions above, will also compile two example applications: custom_schema and external_schema.

custom_schema shows how you can hard-code a schema definition into an application, while external_schema builds on the example code above to show you how to validate and document and report on any validation errors.

JSON Schema Support

Valijson supports most of the constraints defined in Draft 3 and Draft 4 of the JSON Schema specification.

The exceptions for Draft 3 are:

  • disallow
  • extends
  • format (optional)
  • readonly
  • ref
  • refRemote

The exceptions for Draft 4 are:

  • definitions
  • format (optional)
  • ref
  • refRemote

Support for JSON References is in development.

Documentation

Doxygen documentation can be built by running 'doxygen' from the project root directory. Generated documentation will be placed in 'doc/html'. Other relevant documentation such as schemas and specifications have been included in the 'doc' directory.

Dependencies

Required:

  • boost 1.54

Later versions of boost (up to 1.59) are also known to work correctly.

Supported Parsers

Valijson supports JSON documents loaded using JsonCpp, RapidJson, Boost Property Tree and PicoJSON. It has been tested against the following versions of these libraries:

Version of JsonCpp going back to 0.5.0 should also work correctly, but versions from 1.0 onwards have not yet been tested.

Also note that when using PicoJSON, it may be necessary to include the picojson.h before other headers to ensure that the appropriate macros have been enabled.

Other versions of these libraries may work, but have not been tested.

License

Valijson is licensed under the Simplified BSD License.

See the LICENSE file for more information.