json/schema: add 'integer' type support
This commit is contained in:
parent
8694b95cec
commit
f793175fdf
@ -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 ()) {
|
||||
|
1
test/json/schema/type_integer.schema
Normal file
1
test/json/schema/type_integer.schema
Normal file
@ -0,0 +1 @@
|
||||
{"type": "integer"}
|
1
test/json/schema/type_integer_0001_pass.json
Normal file
1
test/json/schema/type_integer_0001_pass.json
Normal file
@ -0,0 +1 @@
|
||||
1
|
1
test/json/schema/type_integer_0002_fail.json
Normal file
1
test/json/schema/type_integer_0002_fail.json
Normal file
@ -0,0 +1 @@
|
||||
1.0
|
1
test/json/schema/type_integer_0003_fail.json
Normal file
1
test/json/schema/type_integer_0003_fail.json
Normal file
@ -0,0 +1 @@
|
||||
1.1
|
1
test/json/schema/type_integer_0004_fail.json
Normal file
1
test/json/schema/type_integer_0004_fail.json
Normal file
@ -0,0 +1 @@
|
||||
"foo"
|
Loading…
Reference in New Issue
Block a user