33 namespace fable::schema {
35 template <
typename T,
typename P>
38 using Type = std::vector<T>;
39 using PrototypeSchema = 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)
48 , prototype_(std::move(prototype))
58 [[nodiscard]]
bool extend()
const {
return option_extend_; }
69 option_extend_ = value;
70 return std::move(*
this);
73 [[nodiscard]]
size_t min_items()
const {
return min_items_; }
74 void set_min_items(
size_t value) { min_items_ = value; }
75 Vector<T, P> min_items(
size_t value) && {
77 return std::move(*
this);
80 [[nodiscard]]
size_t max_items()
const {
return max_items_; }
81 void set_max_items(
size_t value) { max_items_ = value; }
82 Vector<T, P> max_items(
size_t value) && {
84 return std::move(*
this);
88 [[nodiscard]] std::string
type_string()
const override {
return "array of " + prototype_.type_string(); }
93 {
"items", prototype_.json_schema()},
95 if (min_items_ != 0) {
96 j[
"minItems"] = min_items_;
98 if (max_items_ != std::numeric_limits<size_t>::max()) {
99 j[
"maxItems"] = max_items_;
101 this->augment_schema(j);
105 bool validate(
const Conf& c, std::optional<SchemaError>& err)
const override {
109 if (c->size() < min_items_) {
110 return this->set_error(err, c,
"require at least {} items in array, got {}", min_items_,
113 if (c->size() > max_items_) {
114 return this->set_error(err, c,
"expect at most {} items in array, got {}", max_items_,
117 for (
const auto& x : c.
to_array()) {
118 if (!prototype_.validate(x, err)) {
127 assert(ptr_ !=
nullptr);
128 if (j.type() != JsonType::array) {
131 serialize_into(j, *ptr_);
135 assert(ptr_ !=
nullptr);
136 assert(c->type() == this->type_);
137 deserialize_into(c, *ptr_);
140 [[nodiscard]]
Json serialize(
const Type& xs)
const {
141 Json j = Json::array();
142 serialize_into(j, xs);
146 [[nodiscard]] Type deserialize(
const Conf& c)
const {
148 vec.reserve(c->size());
153 void serialize_into(Json& j,
const Type& xs)
const {
154 for (
const auto& x : xs) {
155 j.emplace_back(prototype_.serialize(x));
159 void deserialize_into(
const Conf& c, Type& x)
const {
160 auto size = c->size();
161 if (option_extend_) {
173 void fill(Type& vec,
const Conf& c)
const {
174 for (
const auto& x : c.
to_array()) {
175 T inst = prototype_.deserialize(x);
176 vec.emplace_back(std::move(inst));
181 bool option_extend_{
false};
182 size_t min_items_{0};
183 size_t max_items_{std::numeric_limits<size_t>::max()};
184 PrototypeSchema prototype_{};
188 template <
typename T,
typename P>
189 Vector<T, P> make_schema(std::vector<T>* ptr, P prototype, std::string desc) {
190 return Vector<T, P>(ptr, std::move(prototype), std::move(desc));
193 template <
typename T>
194 Vector<T, decltype(make_prototype<T>())> make_schema(std::vector<T>* ptr, std::string desc) {
195 return Vector<T, decltype(make_prototype<T>())>(ptr, std::move(desc));
std::vector< Conf > to_array() const
Definition: conf.cpp:92
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:88
void set_extend(bool value)
Definition: vector.hpp:63
Vector< T, P > extend(bool value) &&
Definition: vector.hpp:68
Json json_schema() const override
Definition: vector.hpp:90
bool extend() const
Definition: vector.hpp:58
void reset_ptr() override
Definition: vector.hpp:170
void from_conf(const Conf &c) override
Definition: vector.hpp:134
bool validate(const Conf &c, std::optional< SchemaError > &err) const override
Definition: vector.hpp:105
void to_json(Json &j) const override
Definition: vector.hpp:126
nlohmann::json Json
Definition: fable_fwd.hpp:35