32 namespace fable::schema {
34 template <
typename T,
typename P>
37 using Type = std::remove_cv_t<std::remove_reference_t<T>>;
38 using PrototypeSchema = std::remove_cv_t<std::remove_reference_t<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,
typename S>
94 Const<T, P> make_const_schema(T&& constant, P&& prototype, S&& desc) {
95 return {std::forward<T>(constant), std::forward<P>(prototype), std::forward<S>(desc)};
98 template <
typename T,
typename S>
99 Const<T, decltype(make_prototype<std::remove_cv_t<std::remove_reference_t<T>>>())> make_const_schema(T&& constant, S&& desc) {
100 return {std::forward<T>(constant), std::forward<S>(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