60 template<
class Jsonish,
class Key,
class Val
idator>
61 bool cast(Jsonish &ret,
const Jsonish &input, Key &&k, Validator &&f)
63 if (!input.contains(k)) {
66 auto [res, output] = std::invoke(std::forward<Validator>(f), input[k]);
87 return [f=std::forward<Func>(f)](
auto &&val)
mutable {
88 auto res = std::invoke(std::forward<Func>(f), val);
89 return std::pair<bool, json>(res, std::forward<decltype(val)>(val));
153 template<
class Jsonish,
class Key,
class Val
idator>
156 using JsonT = std::decay_t<Jsonish>;
158 if (!(input.contains(k) && input[k].is_array())) {
162 auto array = JsonT::array();
164 for (
const auto &inputItem : input[k]) {
165 auto [res, outputItem] = std::invoke(f, inputItem);
171 array.push_back(std::move(outputItem));
175 ret[k] = std::move(array);
244 template<
class Jsonish,
class Key,
class Val
idator>
247 using JsonT = std::decay_t<Jsonish>;
249 if (!(input.contains(k) && input[k].is_object())) {
253 auto obj = JsonT::object();
255 for (
const auto &[inputKey, inputValue] : input[k].items()) {
256 auto inputItem = JsonT::array({JsonT(inputKey), inputValue});
257 auto [res, outputItem] = std::invoke(f, inputItem);
263 auto outputKey = outputItem[0];
264 auto outputValue = std::move(outputItem)[1];
265 obj[std::move(outputKey)] = std::move(outputValue);
269 ret[k] = std::move(obj);
286 template<
class Jsonish,
class Key,
class Jsonish2>
289 using JsonT = std::decay_t<Jsonish>;
291 if (input.contains(k)) {
292 return std::forward<Jsonish>(input);
294 JsonT output = std::forward<Jsonish>(input);
295 output[std::forward<Key>(k)] = std::forward<Jsonish2>(defaultValue);
Definition: location.hpp:10
bool castObject(Jsonish &ret, const Jsonish &input, Key &&k, Validator &&f, CastObjectStrategy strategy)
Cast an object of items.
Definition: validator.hpp:245
std::decay_t< Jsonish > makeDefaultValue(Jsonish &&input, Key &&k, Jsonish2 &&defaultValue)
Replace a non-existent value with a default one.
Definition: validator.hpp:287
bool castArray(Jsonish &ret, const Jsonish &input, Key &&k, Validator &&f, CastArrayStrategy strategy)
Cast an array of items.
Definition: validator.hpp:154
CastObjectStrategy
Strategy used for castObject().
Definition: validator.hpp:184
@ IgnoreInvalid
If any item is invalid, ignore them and add all valid entries into the output.
@ FailAll
If any item is invalid, fail the entire cast.
bool cast(Jsonish &ret, const Jsonish &input, Key &&k, Validator &&f)
Run validator against input[k] and add it to ret.
Definition: validator.hpp:61
CastArrayStrategy
Strategy used for castArray().
Definition: validator.hpp:97
@ IgnoreInvalid
If any item is invalid, ignore them and add all valid entries into the output.
@ FailAll
If any item is invalid, fail the entire cast.
auto identValidate(Func &&f)
Make an identity validator.
Definition: validator.hpp:85