diff --git a/src/json-validator.cpp b/src/json-validator.cpp index c7c5f75..34076c2 100644 --- a/src/json-validator.cpp +++ b/src/json-validator.cpp @@ -40,6 +40,16 @@ protected: root_schema *root_; json default_value_ = nullptr; +protected: + virtual std::shared_ptr make_for_default_( + std::shared_ptr<::schema> & /* sch */, + root_schema * /* root */, + std::vector & /* uris */, + nlohmann::json & /* default_value */) const + { + return nullptr; + }; + public: virtual ~schema() = default; @@ -92,6 +102,21 @@ class schema_ref : public schema return default_value_; } +protected: + virtual std::shared_ptr make_for_default_( + std::shared_ptr<::schema> &sch, + root_schema *root, + std::vector &uris, + nlohmann::json &default_value) const override + { + // create a new reference schema using the original reference (which will be resolved later) + // to store this overloaded default value #209 + auto result = std::make_shared(uris[0].to_string(), root); + result->set_target(sch, true); + result->set_default_value(default_value); + return result; + }; + public: schema_ref(const std::string &id, root_schema *root) : schema(root), id_(id) {} @@ -509,6 +534,18 @@ class type_schema : public schema } } +protected: + virtual std::shared_ptr make_for_default_( + std::shared_ptr<::schema> & /* sch */, + root_schema * /* root */, + std::vector & /* uris */, + nlohmann::json &default_value) const override + { + auto result = std::make_shared(*this); + result->set_default_value(default_value); + return result; + }; + public: type_schema(json &sch, root_schema *root, @@ -1289,16 +1326,8 @@ std::shared_ptr schema::make(json &schema, attr = schema.find("default"); if (attr != schema.end()) { // copy the referenced schema depending on the underlying type and modify the default value - if (dynamic_cast(sch.get())) { - // create a new reference schema use the original reference (which will be resolved later) - // to store this overloaed default value #209 - auto overloaded_ref_sch = std::make_shared(uris[0].to_string(), root); - overloaded_ref_sch->set_target(sch, true); - overloaded_ref_sch->set_default_value(attr.value()); - sch = overloaded_ref_sch; - } else if (auto *type_sch = dynamic_cast(sch.get())) { - sch = std::make_shared(*type_sch); - sch->set_default_value(attr.value()); + if (auto new_sch = sch->make_for_default_(sch, root, uris, attr.value())) { + sch = new_sch; } schema.erase(attr); }