Full C++23 framework tree: runtime, observability, security, data, app, realtime, supervisor, clients, CLI, example plus enterprise tenancy, audit, operability, governance, supplychain. Postgres via docker compose for tests.
73 lines
1.6 KiB
C++
73 lines
1.6 KiB
C++
#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 <map>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#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<FieldError> failures);
|
|
const std::vector<FieldError>& failures() const;
|
|
std::string to_json() const;
|
|
|
|
private:
|
|
std::vector<FieldError> failures_;
|
|
};
|
|
|
|
class Model {
|
|
public:
|
|
explicit Model(std::vector<Field> fields);
|
|
|
|
std::map<std::string, std::string> validate(
|
|
const std::map<std::string, std::string>& input) const;
|
|
|
|
const std::vector<Field>& fields() const;
|
|
|
|
private:
|
|
std::vector<Field> fields_;
|
|
};
|
|
|
|
std::map<std::string, std::string> parse_json_object_flat(
|
|
const std::string& body);
|
|
|
|
class Form {
|
|
public:
|
|
explicit Form(Model model);
|
|
|
|
std::map<std::string, std::string> bind(const Request& req) const;
|
|
|
|
private:
|
|
Model model_;
|
|
};
|
|
|
|
} // namespace molo::example::fw
|