Skip to content
Permalink
Browse files

#312 JSON schema validation for config.json

Initial JSON schema validation for the main configuration with valijson.
Not yet implemented are:
- hardware.json validation
- configuration received over websocket API
  • Loading branch information
zehnm committed Dec 26, 2019
1 parent 82605ce commit 02df01e6d7e6f6a0f4be11cd152b577bd89b6001
Showing with 17,740 additions and 43 deletions.
  1. +4 βˆ’0 3rdparty/valijson.pri
  2. +35 βˆ’0 3rdparty/valijson/Authors
  3. +23 βˆ’0 3rdparty/valijson/LICENSE
  4. +217 βˆ’0 3rdparty/valijson/README.md
  5. +1,042 βˆ’0 3rdparty/valijson/include/compat/optional.hpp
  6. +472 βˆ’0 3rdparty/valijson/include/valijson/adapters/adapter.hpp
  7. +864 βˆ’0 3rdparty/valijson/include/valijson/adapters/basic_adapter.hpp
  8. +52 βˆ’0 3rdparty/valijson/include/valijson/adapters/frozen_value.hpp
  9. +709 βˆ’0 3rdparty/valijson/include/valijson/adapters/json11_adapter.hpp
  10. +720 βˆ’0 3rdparty/valijson/include/valijson/adapters/jsoncpp_adapter.hpp
  11. +707 βˆ’0 3rdparty/valijson/include/valijson/adapters/nlohmann_json_adapter.hpp
  12. +723 βˆ’0 3rdparty/valijson/include/valijson/adapters/picojson_adapter.hpp
  13. +718 βˆ’0 3rdparty/valijson/include/valijson/adapters/poco_json_adapter.hpp
  14. +752 βˆ’0 3rdparty/valijson/include/valijson/adapters/property_tree_adapter.hpp
  15. +720 βˆ’0 3rdparty/valijson/include/valijson/adapters/qtjson_adapter.hpp
  16. +929 βˆ’0 3rdparty/valijson/include/valijson/adapters/rapidjson_adapter.hpp
  17. +483 βˆ’0 3rdparty/valijson/include/valijson/adapters/std_string_adapter.hpp
  18. +21 βˆ’0 3rdparty/valijson/include/valijson/constraint_builder.hpp
  19. +65 βˆ’0 3rdparty/valijson/include/valijson/constraints/basic_constraint.hpp
  20. +1,231 βˆ’0 3rdparty/valijson/include/valijson/constraints/concrete_constraints.hpp
  21. +50 βˆ’0 3rdparty/valijson/include/valijson/constraints/constraint.hpp
  22. +104 βˆ’0 3rdparty/valijson/include/valijson/constraints/constraint_visitor.hpp
  23. +107 βˆ’0 3rdparty/valijson/include/valijson/internal/custom_allocator.hpp
  24. +30 βˆ’0 3rdparty/valijson/include/valijson/internal/debug.hpp
  25. +253 βˆ’0 3rdparty/valijson/include/valijson/internal/json_pointer.hpp
  26. +60 βˆ’0 3rdparty/valijson/include/valijson/internal/json_reference.hpp
  27. +14 βˆ’0 3rdparty/valijson/include/valijson/internal/optional.hpp
  28. +33 βˆ’0 3rdparty/valijson/include/valijson/internal/uri.hpp
  29. +212 βˆ’0 3rdparty/valijson/include/valijson/schema.hpp
  30. +2,394 βˆ’0 3rdparty/valijson/include/valijson/schema_parser.hpp
  31. +295 βˆ’0 3rdparty/valijson/include/valijson/subschema.hpp
  32. +44 βˆ’0 3rdparty/valijson/include/valijson/utils/file_utils.hpp
  33. +34 βˆ’0 3rdparty/valijson/include/valijson/utils/json11_utils.hpp
  34. +33 βˆ’0 3rdparty/valijson/include/valijson/utils/jsoncpp_utils.hpp
  35. +34 βˆ’0 3rdparty/valijson/include/valijson/utils/nlohmann_json_utils.hpp
  36. +33 βˆ’0 3rdparty/valijson/include/valijson/utils/picojson_utils.hpp
  37. +37 βˆ’0 3rdparty/valijson/include/valijson/utils/poco_json_utils.hpp
  38. +36 βˆ’0 3rdparty/valijson/include/valijson/utils/property_tree_utils.hpp
  39. +44 βˆ’0 3rdparty/valijson/include/valijson/utils/qtjson_utils.hpp
  40. +35 βˆ’0 3rdparty/valijson/include/valijson/utils/rapidjson_utils.hpp
  41. +60 βˆ’0 3rdparty/valijson/include/valijson/utils/utf8_utils.hpp
  42. +122 βˆ’0 3rdparty/valijson/include/valijson/validation_results.hpp
  43. +1,816 βˆ’0 3rdparty/valijson/include/valijson/validation_visitor.hpp
  44. +74 βˆ’0 3rdparty/valijson/include/valijson/validator.hpp
  45. +1,100 βˆ’0 config-schema.json
  46. +1 βˆ’1 qml_qmlcache.qrc
  47. +7 βˆ’4 remote.pro
  48. +10 βˆ’5 sources/config.cpp
  49. +10 βˆ’3 sources/config.h
  50. +1 βˆ’1 sources/hardware/hardwarefactory.cpp
  51. +115 βˆ’17 sources/jsonfile.cpp
  52. +43 βˆ’11 sources/jsonfile.h
  53. +12 βˆ’1 sources/main.cpp
@@ -0,0 +1,4 @@

INCLUDEPATH += $$PWD/valijson/include

HEADERS+= $$PWD/valijson/include/valijson/adapters/qtjson_adapter.hpp
@@ -0,0 +1,35 @@
Tristan Penman, tristan@tristanpenman.com
Core library development

James Jensen, jjensen@akamai.com, changes Copyright (c) 2016 Akamai Technologies
Polymorphic constraint support

Tengiz Sharafiev, btolfa+github@gmail.com
Adapter for nlohmann/json parser library

hotwatermorning (github username), hotwatermorning@gmail.com
Adapter for json11 parser library

shsfre09 (github username), <email address unknown>
Adapter for picojson parser library

Michael Smith, michael.smith@puppetlabs.com
Memory management improvements for RapidJson parser library

Richard Clamp, richardc@unixbeard.net
Boost-related fixes

Lars Immisch, lars@ibp.de
noboost branch

Nicolas Witczak, n_witczak@yahoo.com
C++11 improvements

Martin Robinson, 0x4d52@gmail.com
Bug fixes

Pierre Lamot, pierre.lamot@yahoo.fr
Adapter for Qt JSON parser

drewxa (github username), bo.ro.da@mail.ru
Adapter for Poco JSON parser
@@ -0,0 +1,23 @@
Copyright (c) 2016, Tristan Penman
Copyright (c) 2016, Akamai Technolgies, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,217 @@
This README has been copied from: <https://github.com/tristanpenman/valijson>

The sources used are from branch <https://github.com/tristanpenman/valijson/tree/v6-and-v7-support>.
Copied December 26, 2019.

- only the include directory has been copied
- examples and testsuite were omitted

---

# Valijson #

## Overview ##

Valijson is a header-only [JSON Schema](http://json-schema.org/) Validation library for C++11.

Valijson provides a simple validation API that allows you to 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 a hand-written schema validator.

## 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>

These are the classes that we'll be using:

using valijson::Schema;
using valijson::SchemaParser;
using valijson::Validator;
using valijson::adapters::RapidJsonAdapter;

We are going to use RapidJSON to load the schema and the target document:

// 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;
RapidJsonAdapter myTargetAdapter(myTargetDoc);
if (!validator.validate(mySchema, 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.

## Memory Management ##

Valijson has been designed to safely manage, and eventually free, the memory that is allocated while parsing a schema or validating a document. When working with an externally loaded schema (i.e. one that is populated using the `SchemaParser` class) you can rely on RAII semantics.

Things get more interesting when you build a schema using custom code, as illustrated in the following snippet. This code demonstrates how you would create a schema to verify that the value of a 'description' property (if present) is always a string:

{
// Root schema object that manages memory allocated for
// constraints or sub-schemas
Schema schema;

// Allocating memory for a sub-schema returns a const pointer
// which allows inspection but not mutation. This memory will be
// freed only when the root schema goes out of scope
const Subschema *subschema = schema.createSubschema();
{ // Limited scope, for example purposes
// Construct a constraint on the stack
TypeConstraint typeConstraint;
typeConstraint.addNamedType(TypeConstraint::kString);
// Constraints are added to a sub-schema via the root schema,
// which will make a copy of the constraint
schema.addConstraintToSubschema(typeConstraint, subschema);
// Constraint on the stack goes out of scope, but the copy
// held by the root schema continues to exist
}
// Include subschema in properties constraint
PropertiesConstraint propertiesConstraint;
propertiesConstraint.addPropertySubschema("description", subschema);
// Add the properties constraint
schema.addConstraint(propertiesConstraint);
// Root schema goes out of scope and all allocated memory is freed
}
## JSON References ##
The library includes support for local JSON References. Remote JSON References are supported only when the appropriate callback functions are provided.
Valijson's JSON Reference implementation requires that two callback functions are required. The first is expected to return a pointer to a newly fetched document. Valijson takes ownership of this pointer. The second callback function is used to release ownership of that pointer back to the application. Typically, this would immediately free the memory that was allocated for the document.
## Test Suite ##
Valijson's' test suite currently contains several hand-crafted tests and uses the standard [JSON Schema Test Suite](https://github.com/json-schema/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](http://tools.ietf.org/search/draft-zyp-json-schema-03) and [Draft 4](http://tools.ietf.org/search/draft-zyp-json-schema-04) of the JSON Schema specification.
The exceptions for Draft 3 are:
- disallow
- extends
- format (optional)
- readonly
The exceptions for Draft 4 are:
- format (optional)
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 ##
Valijson requires a compiler with C++11 support.
When building the test suite, Boost 1.54, Qt 5 and Poco are optional dependencies.
## Supported Parsers ##
Valijson supports JSON documents loaded using various JSON parser libraries. It has been tested against the following versions of these libraries:
- [boost::property_tree 1.54](http://www.boost.org/doc/libs/1_54_0/doc/html/boost_propertytree/synopsis.html)
- [json11 (commit afcc8d0)](https://github.com/dropbox/json11/tree/afcc8d0d82b1ce2df587a7a0637d05ba493bf5e6)
- [jsoncpp 0.9.4](https://github.com/open-source-parsers/jsoncpp/archive/0.9.4.tar.gz)
- [nlohmann/json 1.1.0](https://github.com/nlohmann/json/archive/v1.1.0.tar.gz)
- [rapidjson 1.1.0](https://github.com/miloyip/rapidjson/releases/tag/v1.1.0)
- [PicoJSON 1.3.0](https://github.com/kazuho/picojson/archive/v1.3.0.tar.gz)
- [Poco JSON 1.7.8](https://pocoproject.org/docs/Poco.JSON.html)
- [Qt 5.8](http://doc.qt.io/qt-5/json.html)
Other versions of these libraries may work, but have not been tested. In particular, versions of JsonCpp going back to 0.5.0 should also work correctly, but versions from 1.0 onwards have not yet been tested.
## Package Managers ##
If you are using [vcpkg](https://github.com/Microsoft/vcpkg) on your project for external dependencies, then you can use the [valijson](https://github.com/microsoft/vcpkg/tree/master/ports/valijson) package. Please see the vcpkg project for any issues regarding the packaging.
## Test Suite Requirements ##
Supported versions of these libraries have been included in the 'thirdparty' directory so as to support Valijson's examples and test suite.
The exceptions to this are boost, Poco and Qt5, which due to their size must be installed to a location that CMake can find.
## Known Issues ##
When using PicoJSON, it may be necessary to include the `picojson.h` before other headers to ensure that the appropriate macros have been enabled.
When building Valijson using CMake on Mac OS X, with Qt 5 installed via Homebrew, you may need to set `CMAKE_PREFIX_PATH` so that CMake can find your Qt installation, e.g:
mkdir build
cd build
cmake .. -DCMAKE_PREFIX_PATH=$(brew --prefix qt5)
make
## License ##
Valijson is licensed under the Simplified BSD License.
See the LICENSE file for more information.

0 comments on commit 02df01e

Please sign in to comment.
You can’t perform that action at this time.