json/schema: add 'integer' type support

This commit is contained in:
Danny Robson 2016-07-04 15:46:01 +10:00
parent 8694b95cec
commit f793175fdf
6 changed files with 22 additions and 4 deletions

View File

@ -323,6 +323,22 @@ to_string (json::tree::type_t t)
}
//-----------------------------------------------------------------------------
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;
}
//-----------------------------------------------------------------------------
static void
validate (json::tree::node &node,
@ -343,10 +359,7 @@ validate (json::tree::node &node,
if (type != schema.cend ()) {
// check against a single named type
if (type->second->is_string ()) {
const auto &a = type->second->as_string ();
auto b = to_string (node.type ());
if (a != b)
if (!is_type_valid (node, type->second->as_string ()))
throw json::schema_error ("type");
// check against an array of types
} else if (type->second->is_array ()) {

View File

@ -0,0 +1 @@
{"type": "integer"}

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
1.0

View File

@ -0,0 +1 @@
1.1

View File

@ -0,0 +1 @@
"foo"