33 namespace fable::schema {
35 template <
typename T,
typename P>
38 using Type = std::vector<T>;
39 using PrototypeSchema = std::remove_cv_t<std::remove_reference_t<P>>;
41 Vector(Type* ptr, std::string desc) :
Vector(ptr, make_prototype<T>(), std::move(desc)) {}
43 Vector(Type* ptr, PrototypeSchema prototype)
44 :
Base<Vector<T, P>>(JsonType::array), prototype_(std::move(prototype)), ptr_(ptr) {}
46 Vector(Type* ptr, PrototypeSchema prototype, std::string desc)
47 :
Base<Vector<T, P>>(JsonType::array, std::move(desc)), prototype_(std::move(prototype)), ptr_(ptr) {}
56 [[nodiscard]]
bool extend()
const {
return option_extend_; }
67 option_extend_ = value;
68 return std::move(*
this);
71 [[nodiscard]]
size_t min_items()
const {
return min_items_; }
72 void set_min_items(
size_t value) { min_items_ = value; }
73 [[nodiscard]] Vector<T, P> min_items(
size_t value) && {
75 return std::move(*
this);
78 [[nodiscard]]
size_t max_items()
const {
return max_items_; }
79 void set_max_items(
size_t value) { max_items_ = value; }
80 [[nodiscard]] Vector<T, P> max_items(
size_t value) && {
82 return std::move(*
this);
86 [[nodiscard]] std::string
type_string()
const override {
return "array of " + prototype_.type_string(); }
91 {
"items", prototype_.json_schema()},
93 if (min_items_ != 0) {
94 j[
"minItems"] = min_items_;
96 if (max_items_ != std::numeric_limits<size_t>::max()) {
97 j[
"maxItems"] = max_items_;
99 this->augment_schema(j);
103 bool validate(
const Conf& c, std::optional<SchemaError>& err)
const override {
107 if (c->size() < min_items_) {
108 return this->set_error(err, c,
"require at least {} items in array, got {}", min_items_,
111 if (c->size() > max_items_) {
112 return this->set_error(err, c,
"expect at most {} items in array, got {}", max_items_,
115 for (
const auto& x : c.
to_array()) {
116 if (!prototype_.validate(x, err)) {
125 assert(ptr_ !=
nullptr);
126 if (j.type() != JsonType::array) {
129 serialize_into(j, *ptr_);
133 assert(ptr_ !=
nullptr);
134 assert(c->type() == this->type_);
135 deserialize_into(c, *ptr_);
138 [[nodiscard]]
Json serialize(
const Type& xs)
const {
139 Json j = Json::array();
140 serialize_into(j, xs);
144 [[nodiscard]] Type deserialize(
const Conf& c)
const {
146 vec.reserve(c->size());
151 void serialize_into(Json& j,
const Type& xs)
const {
152 for (
const auto& x : xs) {
153 j.emplace_back(prototype_.serialize(x));
157 void deserialize_into(
const Conf& c, Type& x)
const {
158 auto size = c->size();
159 if (option_extend_) {
171 void fill(Type& vec,
const Conf& c)
const {
172 for (
const auto& x : c.
to_array()) {
173 T inst = prototype_.deserialize(x);
174 vec.emplace_back(std::move(inst));
179 bool option_extend_{
false};
180 size_t min_items_{0};
181 size_t max_items_{std::numeric_limits<size_t>::max()};
182 PrototypeSchema prototype_{};
186 template <
typename T,
typename P,
typename S>
187 Vector<T, P> make_schema(std::vector<T>* ptr, P&& prototype, S&& desc) {
188 return {ptr, std::forward<P>(prototype), std::forward<S>(desc)};
191 template <
typename T,
typename S>
192 Vector<T, decltype(make_prototype<T>())> make_schema(std::vector<T>* ptr, S&& desc) {
193 return {ptr, std::forward<S>(desc)};
std::vector< Conf > to_array() const
Definition: conf.cpp:93
Definition: interface.hpp:398
bool validate_type(const Conf &c, std::optional< SchemaError > &err) const
Definition: interface.hpp:459
virtual Json to_json() const
Definition: interface.hpp:254
Definition: vector.hpp:36
std::string type_string() const override
Definition: vector.hpp:86
void set_extend(bool value)
Definition: vector.hpp:61
Vector< T, P > extend(bool value) &&
Definition: vector.hpp:66
Json json_schema() const override
Definition: vector.hpp:88
bool extend() const
Definition: vector.hpp:56
void reset_ptr() override
Definition: vector.hpp:168
void from_conf(const Conf &c) override
Definition: vector.hpp:132
bool validate(const Conf &c, std::optional< SchemaError > &err) const override
Definition: vector.hpp:103
void to_json(Json &j) const override
Definition: vector.hpp:124
nlohmann::json Json
Definition: fable_fwd.hpp:35