Add schema class that is aware of a default entry

This commit is contained in:
Sven Fink 2020-03-02 15:22:59 +01:00
parent 7264fa0a05
commit 39c7b4f6f0

View File

@ -49,6 +49,33 @@ public:
std::vector<nlohmann::json_uri> uris); std::vector<nlohmann::json_uri> uris);
}; };
class schema_with_default : public schema
{
json defaultValue_{};
public:
schema_with_default(json &sch, root_schema *root)
: schema(root)
{
const auto attr = sch.find("default");
if (attr != sch.end()) {
defaultValue_ = std::move(attr.value());
sch.erase(attr);
}
}
schema_with_default(const json &sch, root_schema *root)
: schema(root)
{
const auto attr = sch.find("default");
if (attr != sch.end()) {
defaultValue_ = attr.value();
}
}
virtual ~schema_with_default() = default;
};
class schema_ref : public schema class schema_ref : public schema
{ {
const std::string id_; const std::string id_;
@ -523,7 +550,7 @@ public:
} }
}; };
class string : public schema class string : public schema_with_default
{ {
std::pair<bool, size_t> maxLength_{false, 0}; std::pair<bool, size_t> maxLength_{false, 0};
std::pair<bool, size_t> minLength_{false, 0}; std::pair<bool, size_t> minLength_{false, 0};
@ -583,7 +610,7 @@ class string : public schema
public: public:
string(json &sch, root_schema *root) string(json &sch, root_schema *root)
: schema(root) : schema_with_default(sch, root)
{ {
auto attr = sch.find("maxLength"); auto attr = sch.find("maxLength");
if (attr != sch.end()) { if (attr != sch.end()) {
@ -616,7 +643,7 @@ public:
}; };
template <typename T> template <typename T>
class numeric : public schema class numeric : public schema_with_default
{ {
std::pair<bool, T> maximum_{false, 0}; std::pair<bool, T> maximum_{false, 0};
std::pair<bool, T> minimum_{false, 0}; std::pair<bool, T> minimum_{false, 0};
@ -655,7 +682,7 @@ class numeric : public schema
public: public:
numeric(const json &sch, root_schema *root, std::set<std::string> &kw) numeric(const json &sch, root_schema *root, std::set<std::string> &kw)
: schema(root) : schema_with_default(sch, root)
{ {
auto attr = sch.find("maximum"); auto attr = sch.find("maximum");
if (attr != sch.end()) { if (attr != sch.end()) {
@ -713,7 +740,7 @@ public:
: schema(root) {} : schema(root) {}
}; };
class boolean : public schema class boolean : public schema_with_default
{ {
bool true_; bool true_;
void validate(const json::json_pointer &ptr, const json &instance, error_handler &e) const override void validate(const json::json_pointer &ptr, const json &instance, error_handler &e) const override
@ -733,7 +760,7 @@ class boolean : public schema
public: public:
boolean(json &sch, root_schema *root) boolean(json &sch, root_schema *root)
: schema(root), true_(sch) {} : schema_with_default(sch, root), true_(sch) {}
}; };
class required : public schema class required : public schema
@ -752,7 +779,7 @@ public:
: schema(root), required_(r) {} : schema(root), required_(r) {}
}; };
class object : public schema class object : public schema_with_default
{ {
std::pair<bool, size_t> maxProperties_{false, 0}; std::pair<bool, size_t> maxProperties_{false, 0};
std::pair<bool, size_t> minProperties_{false, 0}; std::pair<bool, size_t> minProperties_{false, 0};
@ -822,7 +849,7 @@ public:
object(json &sch, object(json &sch,
root_schema *root, root_schema *root,
const std::vector<nlohmann::json_uri> &uris) const std::vector<nlohmann::json_uri> &uris)
: schema(root) : schema_with_default(sch, root)
{ {
auto attr = sch.find("maxProperties"); auto attr = sch.find("maxProperties");
if (attr != sch.end()) { if (attr != sch.end()) {
@ -896,7 +923,7 @@ public:
} }
}; };
class array : public schema class array : public schema_with_default
{ {
std::pair<bool, size_t> maxItems_{false, 0}; std::pair<bool, size_t> maxItems_{false, 0};
std::pair<bool, size_t> minItems_{false, 0}; std::pair<bool, size_t> minItems_{false, 0};
@ -966,7 +993,7 @@ class array : public schema
public: public:
array(json &sch, root_schema *root, const std::vector<nlohmann::json_uri> &uris) array(json &sch, root_schema *root, const std::vector<nlohmann::json_uri> &uris)
: schema(root) : schema_with_default(sch, root)
{ {
auto attr = sch.find("maxItems"); auto attr = sch.find("maxItems");
if (attr != sch.end()) { if (attr != sch.end()) {