2015-03-18 16:08:18 +11:00
|
|
|
/*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2015-03-18 16:08:18 +11:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-03-18 16:08:18 +11:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2015-03-18 16:08:18 +11:00
|
|
|
*
|
2018-07-12 13:06:11 +10:00
|
|
|
* Copyright 2015-2018 Danny Robson <danny@nerdcruft.net>
|
2015-03-18 16:08:18 +11:00
|
|
|
*/
|
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "schema.hpp"
|
2015-03-18 16:08:18 +11:00
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "tree.hpp"
|
|
|
|
#include "except.hpp"
|
2016-06-28 16:59:56 +10:00
|
|
|
|
2017-08-01 14:16:55 +10:00
|
|
|
#include "../debug.hpp"
|
2016-06-28 16:59:56 +10:00
|
|
|
#include "../io.hpp"
|
2016-04-05 11:06:01 +10:00
|
|
|
#include "../maths.hpp"
|
2015-03-18 16:08:18 +11:00
|
|
|
|
|
|
|
#include <regex>
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-03-23 18:42:00 +11:00
|
|
|
struct length_error : public json::schema_error {
|
2016-10-11 21:43:28 +11:00
|
|
|
length_error (const std::string &what):
|
|
|
|
schema_error (what)
|
|
|
|
{ ; }
|
2015-03-23 18:42:00 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-09-20 16:02:08 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-23 18:42:00 +11:00
|
|
|
struct format_error : public json::schema_error {
|
2016-10-11 21:43:28 +11:00
|
|
|
format_error (const std::string &what):
|
|
|
|
schema_error (what)
|
|
|
|
{ ; }
|
2015-03-23 18:42:00 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
static void validate (json::tree::node&, const json::tree::object&);
|
2015-03-18 16:08:18 +11:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-03-23 18:42:00 +11:00
|
|
|
static void
|
2015-03-18 16:08:18 +11:00
|
|
|
validate (json::tree::object &node,
|
|
|
|
const json::tree::object &schema)
|
|
|
|
{
|
|
|
|
auto properties = schema.find ("properties");
|
|
|
|
auto additional = schema.find ("additionalProperties");
|
|
|
|
auto pattern = schema.find ("patternProperties");
|
|
|
|
|
|
|
|
if (properties != schema.cend ()) {
|
|
|
|
for (const auto &kv: properties->second->as_object ()) {
|
|
|
|
auto p = node.find (kv.first);
|
|
|
|
if (p != node.cend ())
|
2015-03-23 18:42:00 +11:00
|
|
|
validate (*p->second, kv.second->as_object ());
|
2015-03-18 16:08:18 +11:00
|
|
|
else {
|
|
|
|
try {
|
|
|
|
node.insert (kv.first, (*kv.second)["default"].clone ());
|
2015-03-23 18:42:00 +11:00
|
|
|
validate (node[kv.first], kv.second->as_object ());
|
|
|
|
continue;
|
2015-03-18 16:08:18 +11:00
|
|
|
} catch (const json::key_error&)
|
|
|
|
{ ; }
|
2015-03-23 18:42:17 +11:00
|
|
|
|
|
|
|
if (additional != schema.cend ()) {
|
|
|
|
if (additional->second->is_boolean () && !additional->second->as_bool ())
|
|
|
|
throw json::schema_error ("additionalProperties");
|
|
|
|
|
|
|
|
validate (*p->second, additional->second->as_object ());
|
|
|
|
}
|
2015-03-18 16:08:18 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-23 18:42:17 +11:00
|
|
|
if (pattern != schema.cend ()) {
|
|
|
|
for (auto &cond: pattern->second->as_object ()) {
|
|
|
|
std::regex expr (cond.first, std::regex_constants::ECMAScript);
|
2015-03-18 16:08:18 +11:00
|
|
|
|
2015-03-23 18:42:17 +11:00
|
|
|
for (auto &props: node) {
|
|
|
|
if (std::regex_search (props.first, expr))
|
|
|
|
validate (*props.second, cond.second->as_object ());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-18 16:08:18 +11:00
|
|
|
|
|
|
|
if (schema.has ("dependencies"))
|
|
|
|
not_implemented ();
|
|
|
|
|
|
|
|
// properties must be checked after the 'properties' check has a chance to
|
|
|
|
// create the defaulted entries.
|
|
|
|
auto maxProperties = schema.find ("maxProperties");
|
|
|
|
if (maxProperties != schema.cend ())
|
2015-03-23 18:42:00 +11:00
|
|
|
if (node.size () > maxProperties->second->as_uint ())
|
|
|
|
throw json::schema_error ("maxProperties");
|
2015-03-18 16:08:18 +11:00
|
|
|
|
|
|
|
auto minProperties = schema.find ("minProperties");
|
|
|
|
if (minProperties != schema.cend ())
|
2015-03-23 18:42:00 +11:00
|
|
|
if (node.size () < minProperties->second->as_uint ())
|
|
|
|
throw json::schema_error ("minProperties");
|
2015-03-18 16:08:18 +11:00
|
|
|
|
|
|
|
auto required = schema.find ("required");
|
|
|
|
if (required != schema.cend ())
|
|
|
|
for (const auto &i: required->second->as_array ())
|
2015-03-23 18:42:00 +11:00
|
|
|
if (!node.has (i.as_string ()))
|
|
|
|
throw json::schema_error ("required");
|
2015-03-18 16:08:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-23 18:42:00 +11:00
|
|
|
static void
|
2015-03-18 16:08:18 +11:00
|
|
|
validate (json::tree::array &node,
|
|
|
|
const json::tree::object &schema)
|
|
|
|
{
|
|
|
|
// attempt to match the item and additionalItem schemas
|
|
|
|
auto items = schema.find ("items");
|
|
|
|
auto additional = schema.find ("additionalItems");
|
|
|
|
|
|
|
|
if (items != schema.cend ()) {
|
|
|
|
// items is an object, test all elements with it as a schema
|
|
|
|
if (items->second->is_object ()) {
|
|
|
|
for (auto &i: node)
|
2015-03-23 18:42:00 +11:00
|
|
|
validate (i, items->second->as_object ());
|
2015-03-18 16:08:18 +11:00
|
|
|
// items is a list of schemas, test n-elements with it as a schema
|
|
|
|
} else if (items->second->is_array ()) {
|
|
|
|
const auto &itemArray = items->second->as_array ();
|
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
for (; i < itemArray.size () && i < node.size (); ++i)
|
2015-03-23 18:42:00 +11:00
|
|
|
validate (node[i], itemArray[i].as_object ());
|
2015-03-18 16:08:18 +11:00
|
|
|
|
|
|
|
// we've exhausted the schema list, use the additional schema
|
|
|
|
if (i == itemArray.size ()) {
|
|
|
|
if (additional->second->is_boolean ()) {
|
2015-03-23 18:42:00 +11:00
|
|
|
if (!additional->second->as_boolean ())
|
|
|
|
throw json::schema_error ("additional");
|
2015-03-18 16:08:18 +11:00
|
|
|
} else if (additional->second->is_object ()) {
|
|
|
|
for ( ; i < node.size (); ++i)
|
2015-03-23 18:42:00 +11:00
|
|
|
validate (node[i], additional->second->as_object ());
|
2015-03-18 16:08:18 +11:00
|
|
|
} else {
|
2015-03-23 18:42:00 +11:00
|
|
|
throw json::schema_error ("items");
|
2015-03-18 16:08:18 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto maxItems = schema.find ("maxItems");
|
|
|
|
if (maxItems != schema.cend ())
|
2015-03-23 18:42:00 +11:00
|
|
|
if (node.size () > maxItems->second->as_uint ())
|
|
|
|
throw json::schema_error ("maxItems");
|
2015-03-18 16:08:18 +11:00
|
|
|
|
|
|
|
auto minItems = schema.find ("minItems");
|
|
|
|
if (minItems != schema.cend ())
|
2015-03-23 18:42:00 +11:00
|
|
|
if (node.size () < minItems->second->as_uint ())
|
|
|
|
throw json::schema_error ("minItems");
|
2015-03-18 16:08:18 +11:00
|
|
|
|
|
|
|
// check all element are unique
|
|
|
|
// XXX: uses a naive n^2 brute force search on equality because it's 2am
|
|
|
|
// and I don't want to write a type aware comparator for the sort.
|
|
|
|
auto unique = schema.find ("uniqueItems");
|
2015-03-23 18:42:00 +11:00
|
|
|
if (unique != schema.cend () && unique->second->as_boolean ())
|
2015-03-18 16:08:18 +11:00
|
|
|
for (size_t a = 0; a < node.size (); ++a)
|
|
|
|
for (size_t b = a + 1; b < node.size (); ++b)
|
2015-03-23 18:42:00 +11:00
|
|
|
if (node[a] == node[b])
|
|
|
|
throw json::schema_error ("uniqueItems");
|
2015-03-18 16:08:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-23 18:42:00 +11:00
|
|
|
static void
|
2015-03-18 16:08:18 +11:00
|
|
|
validate (json::tree::string &node,
|
|
|
|
const json::tree::object &schema)
|
|
|
|
{
|
|
|
|
const auto &val = node.native ();
|
|
|
|
|
|
|
|
// check length is less than a maximum
|
|
|
|
auto maxLength = schema.find ("maxLength");
|
|
|
|
if (maxLength != schema.cend ()) {
|
2016-05-12 17:41:31 +10:00
|
|
|
auto cmp = maxLength->second->as_number ().uint ();
|
2015-11-16 11:42:20 +11:00
|
|
|
if (!util::is_integer (cmp))
|
2015-03-23 18:42:00 +11:00
|
|
|
throw length_error ("maxLength");
|
2015-03-18 16:08:18 +11:00
|
|
|
|
|
|
|
if (val.size () > cmp)
|
2015-03-23 18:42:00 +11:00
|
|
|
throw length_error ("maxLength");
|
2015-03-18 16:08:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// check length is greater than a maximum
|
|
|
|
auto minLength = schema.find ("minLength");
|
|
|
|
if (minLength != schema.cend ()) {
|
2016-05-12 17:41:31 +10:00
|
|
|
auto cmp = minLength->second->as_number ().uint ();
|
2015-11-16 11:42:20 +11:00
|
|
|
if (!util::is_integer (cmp))
|
2015-03-23 18:42:00 +11:00
|
|
|
throw length_error ("minLength");
|
2015-03-18 16:08:18 +11:00
|
|
|
|
|
|
|
if (val.size () < cmp)
|
2015-03-23 18:42:00 +11:00
|
|
|
throw length_error ("minLength");
|
2015-03-18 16:08:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// check the string conforms to a regex
|
|
|
|
// Note: this uses the c++11 regex engine which slightly differs from ECMA 262
|
|
|
|
auto pattern = schema.find ("pattern");
|
|
|
|
if (pattern != schema.cend ()) {
|
|
|
|
std::regex r (pattern->second->as_string ().native (),
|
|
|
|
std::regex_constants::ECMAScript);
|
|
|
|
if (!std::regex_search (val, r))
|
2015-03-23 18:42:00 +11:00
|
|
|
throw format_error ("pattern");
|
2015-03-18 16:08:18 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2016-05-12 17:41:31 +10:00
|
|
|
template <typename T>
|
2015-03-23 18:42:00 +11:00
|
|
|
static void
|
2016-05-12 17:41:31 +10:00
|
|
|
validate_number (T val, const json::tree::object &schema) {
|
|
|
|
using R = json::tree::number::repr_t;
|
2015-03-18 16:08:18 +11:00
|
|
|
|
|
|
|
// check strictly positive integer multiple
|
|
|
|
auto mult = schema.find ("multipleOf");
|
|
|
|
if (mult != schema.cend ()) {
|
2016-05-12 17:41:31 +10:00
|
|
|
const auto &div = mult->second->as_number ();
|
2015-03-18 16:08:18 +11:00
|
|
|
|
2016-05-12 17:41:31 +10:00
|
|
|
switch (div.repr ()) {
|
|
|
|
case R::REAL: if (util::exactly_zero (std::fmod (val, div.real ()))) throw json::schema_error ("multipleOf"); break;
|
|
|
|
case R::SINT: if (util::exactly_zero (std::fmod (val, div.sint ()))) throw json::schema_error ("multipleOf"); break;
|
|
|
|
case R::UINT: if (util::exactly_zero (std::fmod (val, div.uint ()))) throw json::schema_error ("multipleOf"); break;
|
|
|
|
}
|
2015-03-18 16:08:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// check maximum holds. exclusive requires max condition.
|
|
|
|
auto max = schema.find ("maximum");
|
|
|
|
auto exclusiveMax = schema.find ("exclusiveMaximum");
|
2015-03-19 00:25:04 +11:00
|
|
|
if (max != schema.end ()) {
|
2016-05-12 17:41:31 +10:00
|
|
|
const auto &cmp = max->second->as_number ();
|
2015-03-18 16:08:18 +11:00
|
|
|
|
2016-05-12 17:41:31 +10:00
|
|
|
if (exclusiveMax != schema.end () && exclusiveMax->second->as_boolean ()) {
|
|
|
|
switch (cmp.repr ()) {
|
2016-09-20 16:02:08 +10:00
|
|
|
case R::REAL:
|
|
|
|
if (T(val) >= cmp.real ())
|
|
|
|
throw json::schema_error ("exclusiveMax");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case R::SINT:
|
|
|
|
if (json::tree::number::sint_t(std::numeric_limits<T>::max ()) >= cmp.sint () &&
|
|
|
|
val >= T(cmp.sint ()))
|
|
|
|
{
|
|
|
|
throw json::schema_error ("exclusiveMax");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case R::UINT:
|
|
|
|
if (json::tree::number::uint_t(std::numeric_limits<T>::max ()) >= cmp.uint () &&
|
|
|
|
val >= T(cmp.uint ()))
|
|
|
|
{
|
|
|
|
throw json::schema_error ("exclusiveMax");
|
|
|
|
}
|
|
|
|
break;
|
2016-05-12 17:41:31 +10:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (cmp.repr ()) {
|
2016-09-20 16:02:08 +10:00
|
|
|
case R::REAL:
|
|
|
|
if (T(val) > cmp.real ())
|
|
|
|
throw json::schema_error ("maximum");
|
|
|
|
break;
|
|
|
|
case R::SINT:
|
|
|
|
if (json::tree::number::sint_t(std::numeric_limits<T>::max ()) >= cmp.sint () &&
|
|
|
|
val >= T(cmp.sint ()))
|
|
|
|
{
|
|
|
|
throw json::schema_error ("maximum");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case R::UINT:
|
|
|
|
if (json::tree::number::uint_t(std::numeric_limits<T>::max ()) >= cmp.uint () &&
|
|
|
|
val >= T(cmp.uint ()))
|
|
|
|
{
|
|
|
|
throw json::schema_error ("maximum");
|
|
|
|
}
|
|
|
|
break;
|
2016-05-12 17:41:31 +10:00
|
|
|
}
|
|
|
|
}
|
2015-03-18 16:08:18 +11:00
|
|
|
} else {
|
|
|
|
if (exclusiveMax != schema.cend ())
|
2015-03-23 18:42:00 +11:00
|
|
|
throw json::schema_error ("exclusiveMax");
|
2015-03-18 16:08:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// check minimum holds. exclusive requires min condition
|
|
|
|
auto min = schema.find ("minimum");
|
|
|
|
auto exclusiveMin = schema.find ("exclusiveMinimum");
|
2015-03-19 00:25:04 +11:00
|
|
|
if (min != schema.end ()) {
|
2016-05-12 17:41:31 +10:00
|
|
|
const auto &cmp = min->second->as_number ();
|
2015-03-18 16:08:18 +11:00
|
|
|
|
2016-05-12 17:41:31 +10:00
|
|
|
if (exclusiveMin != schema.end () && exclusiveMin->second->as_boolean ()) {
|
|
|
|
switch (cmp.repr ()) {
|
2016-09-20 16:02:08 +10:00
|
|
|
case R::REAL:
|
|
|
|
if (T(val) < cmp.real ())
|
|
|
|
throw json::schema_error ("exclusiveMin");
|
|
|
|
break;
|
|
|
|
case R::SINT:
|
|
|
|
if (cmp.sint () > json::tree::number::sint_t(std::numeric_limits<T>::min ()) &&
|
|
|
|
val < T(cmp.sint ()))
|
|
|
|
{
|
|
|
|
throw json::schema_error ("exclusiveMin");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case R::UINT:
|
|
|
|
if (cmp.uint () > json::tree::number::uint_t(std::numeric_limits<T>::min ()) &&
|
|
|
|
val < T(cmp.uint ()))
|
|
|
|
{
|
|
|
|
throw json::schema_error ("exclusiveMin");
|
|
|
|
}
|
|
|
|
break;
|
2016-05-12 17:41:31 +10:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (cmp.repr ()) {
|
2016-09-20 16:02:08 +10:00
|
|
|
case R::REAL:
|
|
|
|
if (T(val) <= cmp.real ())
|
|
|
|
throw json::schema_error ("minimum");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case R::SINT:
|
|
|
|
if (cmp.sint () >= json::tree::number::sint_t(std::numeric_limits<T>::min ()) &&
|
|
|
|
val <= T(cmp.sint ()))
|
|
|
|
{
|
|
|
|
throw json::schema_error ("minimum");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case R::UINT:
|
|
|
|
if (cmp.uint () >= json::tree::number::uint_t(std::numeric_limits<T>::min ()) &&
|
|
|
|
val <= T(cmp.uint ()))
|
|
|
|
{
|
|
|
|
throw json::schema_error ("minimum");
|
|
|
|
}
|
|
|
|
break;
|
2016-05-12 17:41:31 +10:00
|
|
|
}
|
|
|
|
}
|
2015-03-18 16:08:18 +11:00
|
|
|
} else {
|
|
|
|
if (exclusiveMin != schema.cend ())
|
2015-03-23 18:42:00 +11:00
|
|
|
throw json::schema_error ("exclusiveMin");
|
2015-03-18 16:08:18 +11:00
|
|
|
}
|
2016-05-12 17:41:31 +10:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
static void
|
|
|
|
validate (json::tree::number &node,
|
|
|
|
const json::tree::object &schema)
|
|
|
|
{
|
|
|
|
using N = json::tree::number;
|
|
|
|
using R = N::repr_t;
|
|
|
|
|
|
|
|
switch (node.repr ()) {
|
|
|
|
case R::REAL: validate_number<N::real_t> (node.real (), schema); break;
|
|
|
|
case R::SINT: validate_number<N::sint_t> (node.sint (), schema); break;
|
|
|
|
case R::UINT: validate_number<N::uint_t> (node.uint (), schema); break;
|
|
|
|
}
|
2015-03-18 16:08:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-23 18:42:00 +11:00
|
|
|
static void
|
2015-03-18 16:08:18 +11:00
|
|
|
validate (json::tree::boolean&,
|
|
|
|
const json::tree::object&)
|
2015-03-23 18:42:00 +11:00
|
|
|
{ ; }
|
2015-03-18 16:08:18 +11:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-23 18:42:00 +11:00
|
|
|
static void
|
2015-03-18 16:08:18 +11:00
|
|
|
validate (json::tree::null&,
|
|
|
|
const json::tree::object&)
|
2015-03-23 18:42:00 +11:00
|
|
|
{ ; }
|
2015-03-18 16:08:18 +11:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
static std::string
|
|
|
|
to_string (json::tree::type_t t)
|
|
|
|
{
|
|
|
|
switch (t) {
|
|
|
|
case json::tree::OBJECT: return "object";
|
|
|
|
case json::tree::ARRAY: return "array";
|
|
|
|
case json::tree::STRING: return "string";
|
|
|
|
case json::tree::NUMBER: return "number";
|
|
|
|
case json::tree::BOOLEAN: return "boolean";
|
|
|
|
case json::tree::NONE: return "null";
|
|
|
|
}
|
|
|
|
|
|
|
|
unreachable ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-04 15:46:01 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
static bool
|
|
|
|
is_type_valid (const json::tree::node &node, const std::string &type)
|
|
|
|
{
|
|
|
|
if (type == "integer") return node.is_integer ();
|
|
|
|
if (type == "number") return node.is_number ();
|
|
|
|
if (type == "string") return node.is_string ();
|
|
|
|
if (type == "object") return node.is_object ();
|
|
|
|
if (type == "array") return node.is_array ();
|
|
|
|
if (type == "boolean") return node.is_boolean ();
|
|
|
|
if (type == "null") return node.is_null ();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-18 16:08:18 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-23 18:42:00 +11:00
|
|
|
static void
|
2015-03-18 16:08:18 +11:00
|
|
|
validate (json::tree::node &node,
|
|
|
|
const json::tree::object &schema)
|
|
|
|
{
|
|
|
|
// check the value is in the prescribed list
|
|
|
|
auto enumPos = schema.find ("enum");
|
|
|
|
if (enumPos != schema.cend ()) {
|
|
|
|
auto pos = std::find (enumPos->second->as_array ().cbegin (),
|
|
|
|
enumPos->second->as_array ().cend (),
|
|
|
|
node);
|
|
|
|
if (pos == enumPos->second->as_array ().cend ())
|
2015-03-23 18:42:00 +11:00
|
|
|
throw json::schema_error ("enum");
|
2015-03-18 16:08:18 +11:00
|
|
|
}
|
|
|
|
|
2015-03-23 18:42:00 +11:00
|
|
|
// check the value is the correct type
|
2015-03-18 16:08:18 +11:00
|
|
|
auto type = schema.find ("type");
|
|
|
|
if (type != schema.cend ()) {
|
2015-03-23 18:42:00 +11:00
|
|
|
// check against a single named type
|
2015-03-18 16:08:18 +11:00
|
|
|
if (type->second->is_string ()) {
|
2016-07-04 15:46:01 +10:00
|
|
|
if (!is_type_valid (node, type->second->as_string ()))
|
2015-03-23 18:42:00 +11:00
|
|
|
throw json::schema_error ("type");
|
|
|
|
// check against an array of types
|
2015-03-18 16:08:18 +11:00
|
|
|
} else if (type->second->is_array ()) {
|
|
|
|
auto pos = std::find_if (type->second->as_array ().begin (),
|
|
|
|
type->second->as_array ().end (),
|
|
|
|
[&] (const auto &i) { return i.as_string () == to_string (node.type ()); });
|
|
|
|
if (pos == type->second->as_array ().end ())
|
2015-03-23 18:42:00 +11:00
|
|
|
throw json::schema_error ("type");
|
2015-03-18 16:08:18 +11:00
|
|
|
} else
|
2015-03-23 18:42:00 +11:00
|
|
|
throw json::schema_error ("type");
|
2015-03-18 16:08:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
auto allOf = schema.find ("allOf");
|
|
|
|
if (allOf != schema.cend ()) {
|
|
|
|
for (const auto &i: allOf->second->as_array ())
|
2015-03-23 18:42:00 +11:00
|
|
|
validate (node, i.as_object ());
|
2015-03-18 16:08:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
auto anyOf = schema.find ("anyOf");
|
|
|
|
if (anyOf != schema.cend ()) {
|
|
|
|
bool success = false;
|
|
|
|
for (const auto &i: anyOf->second->as_array ()) {
|
2015-03-23 18:42:00 +11:00
|
|
|
try {
|
|
|
|
validate (node, i.as_object ());
|
|
|
|
success = true;
|
2015-03-18 16:08:18 +11:00
|
|
|
break;
|
2015-03-23 18:42:00 +11:00
|
|
|
} catch (const json::schema_error&)
|
|
|
|
{ continue; }
|
2015-03-18 16:08:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!success)
|
2015-03-23 18:42:00 +11:00
|
|
|
throw json::schema_error ("anyOf");
|
2015-03-18 16:08:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
auto oneOf = schema.find ("oneOf");
|
|
|
|
if (oneOf != schema.cend ()) {
|
|
|
|
unsigned count = 0;
|
|
|
|
|
|
|
|
for (const auto &i: oneOf->second->as_array ()) {
|
2015-03-23 18:42:00 +11:00
|
|
|
try {
|
|
|
|
validate (node, i.as_object ());
|
2015-03-18 16:08:18 +11:00
|
|
|
count++;
|
2015-03-23 18:42:00 +11:00
|
|
|
} catch (const json::schema_error&)
|
|
|
|
{ ; }
|
|
|
|
|
2015-03-18 16:08:18 +11:00
|
|
|
if (count > 1)
|
2015-03-23 18:42:00 +11:00
|
|
|
throw json::schema_error ("oneOf");
|
2015-03-18 16:08:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count != 1)
|
2015-03-23 18:42:00 +11:00
|
|
|
throw json::schema_error ("oneOf");
|
2015-03-18 16:08:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
auto notSchema = schema.find ("not");
|
|
|
|
if (notSchema != schema.cend ()) {
|
2015-03-23 18:42:00 +11:00
|
|
|
for (const auto &i: notSchema->second->as_array ()) {
|
|
|
|
bool valid = false;
|
|
|
|
try {
|
|
|
|
validate (node, i.as_object ());
|
|
|
|
valid = true;
|
|
|
|
} catch (const json::schema_error&)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
if (valid)
|
|
|
|
throw json::schema_error ("not");
|
|
|
|
}
|
2015-03-18 16:08:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (node.type ()) {
|
2015-03-23 18:42:00 +11:00
|
|
|
case json::tree::OBJECT: validate (node.as_object (), schema); return;
|
|
|
|
case json::tree::ARRAY: validate (node.as_array (), schema); return;
|
|
|
|
case json::tree::STRING: validate (node.as_string (), schema); return;
|
|
|
|
case json::tree::NUMBER: validate (node.as_number (), schema); return;
|
|
|
|
case json::tree::BOOLEAN: validate (node.as_boolean (), schema); return;
|
|
|
|
case json::tree::NONE: validate (node.as_null (), schema); return;
|
2015-03-18 16:08:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unreachable ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-23 18:42:00 +11:00
|
|
|
void
|
2015-03-18 16:08:18 +11:00
|
|
|
json::schema::validate (json::tree::node &data,
|
|
|
|
const json::tree::object &schema)
|
|
|
|
{
|
|
|
|
auto title = schema.find ("title");
|
|
|
|
if (title != schema.cend ())
|
|
|
|
if (!title->second->is_string ())
|
2015-03-23 18:42:00 +11:00
|
|
|
throw json::schema_error ("title");
|
2015-03-18 16:08:18 +11:00
|
|
|
|
|
|
|
auto description = schema.find ("description");
|
|
|
|
if (description != schema.cend ())
|
|
|
|
if (!description->second->is_string ())
|
2015-03-23 18:42:00 +11:00
|
|
|
throw json::schema_error ("description");
|
2015-03-18 16:08:18 +11:00
|
|
|
|
|
|
|
return ::validate (data, schema.as_object ());
|
|
|
|
}
|
2016-06-28 16:59:56 +10:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
json::schema::validate (json::tree::node &data,
|
2016-10-07 19:48:42 +11:00
|
|
|
const std::experimental::filesystem::path &schema_path)
|
2016-06-28 16:59:56 +10:00
|
|
|
{
|
2016-11-17 18:07:57 +11:00
|
|
|
const util::mapped_file schema_data (schema_path);
|
2018-05-10 12:52:01 +10:00
|
|
|
auto schema_object = json::tree::parse (util::view(schema_data).cast<const char*> ());
|
2016-06-28 16:59:56 +10:00
|
|
|
validate (data, schema_object->as_object ());
|
|
|
|
}
|
2018-07-11 19:28:13 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-07-16 13:00:14 +10:00
|
|
|
#include "constraint/except.hpp"
|
|
|
|
#include "constraint/base.hpp"
|
2018-07-11 19:28:13 +10:00
|
|
|
|
|
|
|
#include "../io.hpp"
|
|
|
|
#include "../view.hpp"
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
json::schema::validator::validator(std::experimental::filesystem::path const &path):
|
|
|
|
validator (*json::tree::parse (path))
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
json::schema::validator::validator (json::tree::node const &definition):
|
|
|
|
m_base (definition)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
json::schema::validator::validate (json::tree::node &data) const
|
|
|
|
{
|
|
|
|
std::vector<util::json::schema::constraint::failure> res;
|
|
|
|
m_base.validate (std::back_inserter(res), data);
|
|
|
|
return res.empty ();
|
|
|
|
}
|