#pragma once // Single validation framework: field descriptors plus whole-model validation // that reports ALL failures in one error. Forms bind JSON and urlencoded // bodies into the same models. Gap: real molo::validate and molo::forms in // 05-request-routing.md own coercions, nested models, and OpenAPI schemas. #include #include #include #include #include "App.hpp" namespace molo::example::fw { enum class FieldType { String, Integer, Boolean, Email }; struct Field { std::string name; FieldType type = FieldType::String; bool required = true; std::size_t min_len = 0; std::size_t max_len = 1024; long min_value = 0; long max_value = 0; bool has_range = false; std::string default_value; }; struct FieldError { std::string field; std::string message; }; class ValidationError : public HttpError { public: explicit ValidationError(std::vector failures); const std::vector& failures() const; std::string to_json() const; private: std::vector failures_; }; class Model { public: explicit Model(std::vector fields); std::map validate( const std::map& input) const; const std::vector& fields() const; private: std::vector fields_; }; std::map parse_json_object_flat( const std::string& body); class Form { public: explicit Form(Model model); std::map bind(const Request& req) const; private: Model model_; }; } // namespace molo::example::fw