32 namespace fable::schema {
34 template <
typename T,
typename P>
38 using PrototypeSchema = P;
40 Const(Type constant, std::string desc)
41 :
Const(std::move(constant), make_prototype<Type>(), std::move(desc)) {}
43 Const(Type constant, PrototypeSchema prototype, std::string desc)
45 , prototype_(std::move(prototype))
46 , constant_(std::move(constant)) {
47 prototype_.reset_ptr();
55 this->augment_schema(j);
59 bool validate(
const Conf& c, std::optional<SchemaError>& err)
const override {
60 Type tmp = prototype_.deserialize(c);
61 if (tmp != constant_) {
62 return this->set_error(err, c,
"expected const value {}, got {}", constant_, tmp);
68 void to_json(
Json& j)
const override { j = serialize(constant_); }
72 [[nodiscard]]
Json serialize(
const Type& x)
const {
return prototype_.serialize(x); }
74 [[nodiscard]] Type deserialize(
const Conf& c)
const {
79 void serialize_into(Json& j,
const Type& x)
const { prototype_.serialize_into(j, x); }
81 void deserialize_into(
const Conf& c, Type& x)
const {
89 PrototypeSchema prototype_;
93 template <
typename T,
typename P>
94 Const<T, P> make_const_schema(T constant, P prototype, std::string desc) {
95 return Const<T, P>(std::move(constant), std::move(prototype), std::move(desc));
99 Const<T, decltype(make_prototype<T>())> make_const_schema(T constant, std::string desc) {
100 return Const<T, decltype(make_prototype<T>())>(std::move(constant), std::move(desc));
Definition: interface.hpp:398
void from_conf(const Conf &c) override
Definition: const.hpp:70
void reset_ptr() override
Definition: const.hpp:86
bool validate(const Conf &c, std::optional< SchemaError > &err) const override
Definition: const.hpp:59
void to_json(Json &j) const override
Definition: const.hpp:68
Json json_schema() const override
Definition: const.hpp:51
virtual void validate_or_throw(const Conf &c) const final
Definition: interface.hpp:218
virtual Json to_json() const
Definition: interface.hpp:254
nlohmann::json Json
Definition: fable_fwd.hpp:35