json/schema: reimplement enum support
This commit is contained in:
parent
bb9d390cf4
commit
647af5504b
@ -303,6 +303,8 @@ list (
|
|||||||
json/schema/base.hpp
|
json/schema/base.hpp
|
||||||
json/schema/combine.cpp
|
json/schema/combine.cpp
|
||||||
json/schema/combine.hpp
|
json/schema/combine.hpp
|
||||||
|
json/schema/enum.cpp
|
||||||
|
json/schema/enum.hpp
|
||||||
json/schema/except.hpp
|
json/schema/except.hpp
|
||||||
json/schema/introspection.hpp
|
json/schema/introspection.hpp
|
||||||
json/schema/length.cpp
|
json/schema/length.cpp
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*
|
*
|
||||||
* Copyright 2015-2017 Danny Robson <danny@nerdcruft.net>
|
* Copyright 2015-2018 Danny Robson <danny@nerdcruft.net>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "schema.hpp"
|
#include "schema.hpp"
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
#include "length.hpp"
|
#include "length.hpp"
|
||||||
#include "inequality.hpp"
|
#include "inequality.hpp"
|
||||||
#include "properties.hpp"
|
#include "properties.hpp"
|
||||||
|
#include "enum.hpp"
|
||||||
|
|
||||||
using util::json::schema::constraint::base;
|
using util::json::schema::constraint::base;
|
||||||
|
|
||||||
@ -34,6 +35,7 @@ base::instantiate (std::string const &name, ::json::tree::node const &def)
|
|||||||
if (name == "minimum") return std::make_unique<minimum> (def);
|
if (name == "minimum") return std::make_unique<minimum> (def);
|
||||||
if (name == "maximum") return std::make_unique<maximum> (def);
|
if (name == "maximum") return std::make_unique<maximum> (def);
|
||||||
if (name == "properties") return std::make_unique<properties> (def);
|
if (name == "properties") return std::make_unique<properties> (def);
|
||||||
|
if (name == "enum") return std::make_unique<enumeration> (def);
|
||||||
|
|
||||||
throw unknown_constraint (name);
|
throw unknown_constraint (name);
|
||||||
}
|
}
|
||||||
|
39
json/schema/enum.cpp
Normal file
39
json/schema/enum.cpp
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
#include "enum.hpp"
|
||||||
|
|
||||||
|
#include "../tree.hpp"
|
||||||
|
|
||||||
|
using util::json::schema::constraint::enumeration;
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
enumeration::enumeration (::json::tree::node const &def)
|
||||||
|
{
|
||||||
|
for (auto const &i: def.as_array ())
|
||||||
|
m_values.push_back (i.clone ());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
enumeration::output_iterator
|
||||||
|
enumeration::validate (util::json::schema::constraint::base::output_iterator res,
|
||||||
|
::json::tree::node &target) const noexcept
|
||||||
|
{
|
||||||
|
for (auto const &i: m_values)
|
||||||
|
if (target == *i)
|
||||||
|
return res;
|
||||||
|
|
||||||
|
return *res++ = { .rule = *this, .target = target };
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
std::ostream&
|
||||||
|
enumeration::describe (std::ostream &os) const
|
||||||
|
{
|
||||||
|
os << "{ enumeration: [ ";
|
||||||
|
|
||||||
|
for (auto const &i: m_values)
|
||||||
|
os << *i << ", ";
|
||||||
|
|
||||||
|
return os << " ] }";
|
||||||
|
}
|
38
json/schema/enum.hpp
Normal file
38
json/schema/enum.hpp
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "base.hpp"
|
||||||
|
|
||||||
|
#include "../fwd.hpp"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
namespace util::json::schema::constraint {
|
||||||
|
class enumeration final : public base {
|
||||||
|
public:
|
||||||
|
enumeration (::json::tree::node const &def);
|
||||||
|
|
||||||
|
virtual ~enumeration () = default;
|
||||||
|
|
||||||
|
output_iterator validate (output_iterator res, ::json::tree::node &) const noexcept override;
|
||||||
|
std::ostream& describe (std::ostream&) const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<std::unique_ptr<::json::tree::node>> m_values;
|
||||||
|
};
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
1
|
1
test/json/schema/validation/7000_enum/bad/1000_true.json
Normal file
1
test/json/schema/validation/7000_enum/bad/1000_true.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
true
|
@ -0,0 +1 @@
|
|||||||
|
"bar"
|
@ -0,0 +1 @@
|
|||||||
|
{ }
|
@ -0,0 +1 @@
|
|||||||
|
{ "val": "key" }
|
@ -0,0 +1 @@
|
|||||||
|
{ "key": "val", "additional": "error" }
|
@ -0,0 +1 @@
|
|||||||
|
[ 1.1 ]
|
1
test/json/schema/validation/7000_enum/bad/5000_none.json
Normal file
1
test/json/schema/validation/7000_enum/bad/5000_none.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
null
|
@ -0,0 +1 @@
|
|||||||
|
1.1
|
@ -0,0 +1 @@
|
|||||||
|
false
|
@ -0,0 +1 @@
|
|||||||
|
"foo"
|
@ -0,0 +1 @@
|
|||||||
|
{ "key": "val" }
|
@ -0,0 +1 @@
|
|||||||
|
[]
|
1
test/json/schema/validation/7000_enum/schema.json
Normal file
1
test/json/schema/validation/7000_enum/schema.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{ "enum": [ 1.1, false, "foo", { "key": "val" }, [] ] }
|
Loading…
Reference in New Issue
Block a user