json/schema: reimplement enum support

This commit is contained in:
Danny Robson 2018-07-12 13:06:11 +10:00
parent bb9d390cf4
commit 647af5504b
19 changed files with 96 additions and 1 deletions

View File

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

View File

@ -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"

View File

@ -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
View 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
View 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;
};
}

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
true

View File

@ -0,0 +1 @@
"bar"

View File

@ -0,0 +1 @@
{ }

View File

@ -0,0 +1 @@
{ "val": "key" }

View File

@ -0,0 +1 @@
{ "key": "val", "additional": "error" }

View File

@ -0,0 +1 @@
[ 1.1 ]

View File

@ -0,0 +1 @@
null

View File

@ -0,0 +1 @@
1.1

View File

@ -0,0 +1 @@
false

View File

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

View File

@ -0,0 +1 @@
{ "key": "val" }

View File

@ -0,0 +1 @@
[]

View File

@ -0,0 +1 @@
{ "enum": [ 1.1, false, "foo", { "key": "val" }, [] ] }