| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- #pragma once
- #include <map>
- #include <stdexcept>
- #include <string_view>
- #include <vector>
- #include <jvalidate/adapter.h>
- #include <jvalidate/detail/number.h>
- #include <jvalidate/detail/simple_adapter.h>
- #include <jvalidate/enum.h>
- #include <jvalidate/status.h>
- namespace jvalidate::detail {
- /**
- * @brief An ArrayAdapter implmenetation for JSON "types" which do not support
- * arrays. This is for example caused when attempting to apply json schema
- * validation to non-json types representation, such as a some forms of
- * PropertyTree implementations.
- *
- * This is specifically provided for making StringAdapter compatible with the
- * Adapter concept.
- */
- template <typename CRTP> class UnsupportedArrayAdapter {
- public:
- size_t size() const { return 0; }
- CRTP operator[](size_t) const { throw std::runtime_error("stub implementation"); }
- std::vector<CRTP>::const_iterator begin() const { return {}; }
- std::vector<CRTP>::const_iterator end() const { return {}; }
- };
- /**
- * @brief An ObjectAdapter implmenetation for JSON "types" which do not support
- * objects. This is for example caused when attempting to apply json schema
- * validation to non-json types representation.
- *
- * This is specifically provided for making StringAdapter compatible with the
- * Adapter concept.
- */
- template <typename CRTP> class UnsupportedObjectAdapter {
- public:
- size_t size() const { return 0; }
- bool contains(std::string_view) const { return false; }
- CRTP operator[](std::string_view) const { throw std::runtime_error("stub implementation"); }
- std::map<std::string, CRTP>::const_iterator begin() const { return {}; }
- std::map<std::string, CRTP>::const_iterator end() const { return {}; }
- };
- /**
- * @brief An Adapter for strings, required for implmenting propertyNames
- * constraints, which are applied to the keys of a JSON object.
- *
- * Unfortunately requires a large number of stub function implementations in
- * order to satisfy adapter::Adapter AND Adapter concept.
- */
- class StringAdapter final : public adapter::Adapter {
- public:
- using value_type = std::string_view;
- StringAdapter(std::string_view value) : value_(value) {}
- adapter::Type type() const { return adapter::Type::String; }
- bool as_boolean() const { die("boolean"); }
- int64_t as_integer() const { die("integer"); }
- double as_number() const { die("number"); }
- std::string as_string() const { return std::string(value_); }
- size_t array_size() const { die("array"); }
- UnsupportedArrayAdapter<StringAdapter> as_array() const { die("array"); }
- Status apply_array(adapter::AdapterCallback const &) const { return Status::Noop; }
- size_t object_size() const { die("object"); }
- UnsupportedObjectAdapter<StringAdapter> as_object() const { die("object"); }
- Status apply_object(adapter::ObjectAdapterCallback const &) const { return Status::Noop; }
- bool equals(adapter::Adapter const & rhs, bool strict) const {
- if (std::optional str = rhs.maybe_string(strict)) {
- return str == value_;
- }
- return false;
- }
- std::unique_ptr<adapter::Const const> freeze() const final {
- return std::make_unique<adapter::detail::GenericConst<std::string_view>>(value_);
- }
- private:
- [[noreturn]] static void die(std::string expected) {
- throw std::runtime_error("StringAdapter is not an " + expected);
- }
- private:
- std::string_view value_;
- };
- }
- template <> struct jvalidate::adapter::AdapterTraits<std::string_view> {
- template <typename> using Adapter = jvalidate::detail::StringAdapter;
- using ConstAdapter = jvalidate::detail::StringAdapter;
- };
|