31 lines
1.5 KiB
C++
31 lines
1.5 KiB
C++
#pragma once
|
|
#include "schema.hpp"
|
|
#include <optional>
|
|
#include <string_view>
|
|
#include <type_traits>
|
|
namespace structive {
|
|
struct Validation_Error {
|
|
std::string_view property_key;
|
|
std::string_view code;
|
|
};
|
|
template <Valid_Property_Schema Schema, std::size_t Index, class Value>
|
|
std::optional<Validation_Error> validate_property_value(const Schema& schema, const Value& value) {
|
|
std::optional<Validation_Error> error;
|
|
schema.template property<Index>().for_each_constraint([&](const auto& constraint_value) {
|
|
if (!error && !static_cast<bool>(constraint_value.validate(value))) {
|
|
using constraint_type = std::remove_cvref_t<decltype(constraint_value)>;
|
|
error = Validation_Error{declared_property_key<typename Schema::template property_type<Index>>(), constraint_type::code.view()};
|
|
}
|
|
});
|
|
return error;
|
|
}
|
|
template <auto Member, Valid_Property_Schema Schema, class Value>
|
|
std::optional<Validation_Error> validate_property_value(const Schema& schema, const Value& value) requires Schema_Property_Member<Schema, Member> {
|
|
return validate_property_value<Schema, schema_member_property_index_v<Schema, Member>>(schema, value);
|
|
}
|
|
template <Fixed_String Key, Valid_Property_Schema Schema, class Value>
|
|
std::optional<Validation_Error> validate_property_key_value(const Schema& schema, const Value& value) requires Schema_Property_Key<Schema, Key> {
|
|
return validate_property_value<Schema, schema_property_index_v<Schema, Key>>(schema, value);
|
|
}
|
|
}
|