mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-05 18:26:18 +08:00
fix: incorrect json schema for size validated fields (#146)
Co-authored-by: toropov <toropov@protei.ru>
This commit is contained in:
@@ -22,7 +22,10 @@ struct Size {
|
||||
|
||||
template <class T>
|
||||
static parsing::schema::ValidationType to_schema() {
|
||||
return V::template to_schema<size_t>();
|
||||
using ValidationType = parsing::schema::ValidationType;
|
||||
return ValidationType{ValidationType::Size{
|
||||
.size_limit_ =
|
||||
rfl::Ref<ValidationType>::make(V::template to_schema<size_t>())}};
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -32,6 +32,8 @@ struct Type {
|
||||
struct String {
|
||||
Literal<"string"> type;
|
||||
std::optional<std::string> description;
|
||||
rfl::Rename<"minLength", std::optional<size_t>> minSize;
|
||||
rfl::Rename<"maxLength", std::optional<size_t>> maxSize;
|
||||
};
|
||||
|
||||
using NumericType = std::variant<Integer, Number>;
|
||||
@@ -62,8 +64,8 @@ struct Type {
|
||||
Literal<"array"> type;
|
||||
std::optional<std::string> description;
|
||||
rfl::Ref<Type> items;
|
||||
size_t minContains;
|
||||
size_t maxContains;
|
||||
size_t minItems;
|
||||
size_t maxItems;
|
||||
};
|
||||
|
||||
struct Maximum {
|
||||
@@ -130,6 +132,8 @@ struct Type {
|
||||
Literal<"array"> type;
|
||||
std::optional<std::string> description;
|
||||
rfl::Ref<Type> items;
|
||||
rfl::Rename<"minItems", std::optional<size_t>> minSize;
|
||||
rfl::Rename<"maxItems", std::optional<size_t>> maxSize;
|
||||
};
|
||||
|
||||
using ReflectionType =
|
||||
|
||||
+34
-3
@@ -179,7 +179,38 @@ schema::Type handle_validation_type(
|
||||
return schema::Type{.value = schema::Type::Regex{.pattern = _v.pattern_}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::Size>()) {
|
||||
return type_to_json_schema_type(_type);
|
||||
auto t = type_to_json_schema_type(_type);
|
||||
auto handle_size_variant = [&](auto& _t, const auto& _size_limit) {
|
||||
using U = std::remove_cvref_t<decltype(_t)>;
|
||||
using V = std::remove_cvref_t<decltype(_size_limit)>;
|
||||
if constexpr (std::is_same<U, schema::Type::TypedArray>() ||
|
||||
std::is_same<U, schema::Type::String>()) {
|
||||
if constexpr (std::is_same<V, ValidationType::Minimum>()) {
|
||||
_t.minSize = std::get<int>(_size_limit.value_);
|
||||
return t;
|
||||
|
||||
} else if constexpr (std::is_same<V, ValidationType::Maximum>()) {
|
||||
_t.maxSize = std::get<int>(_size_limit.value_);
|
||||
return t;
|
||||
|
||||
} else if constexpr (std::is_same<V, ValidationType::EqualTo>()) {
|
||||
_t.minSize = std::get<int>(_size_limit.value_);
|
||||
_t.maxSize = std::get<int>(_size_limit.value_);
|
||||
return t;
|
||||
|
||||
} else if constexpr (std::is_same<V, ValidationType::AnyOf>() ||
|
||||
std::is_same<V, ValidationType::AllOf>()) {
|
||||
V v;
|
||||
for (const auto& limiter : _size_limit.types_) {
|
||||
v.types_.push_back(ValidationType{ValidationType::Size{
|
||||
.size_limit_ = rfl::Ref<ValidationType>::make(limiter)}});
|
||||
}
|
||||
return handle_validation_type(_type, ValidationType{.variant_ = v});
|
||||
}
|
||||
}
|
||||
return t;
|
||||
};
|
||||
return std::visit(handle_size_variant, t.value, _v.size_limit_->variant_);
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::ExclusiveMaximum>()) {
|
||||
return schema::Type{.value = schema::Type::ExclusiveMaximum{
|
||||
@@ -271,8 +302,8 @@ schema::Type type_to_json_schema_type(const parsing::schema::Type& _type) {
|
||||
return schema::Type{.value = schema::Type::FixedSizeTypedArray{
|
||||
.items = Ref<schema::Type>::make(
|
||||
type_to_json_schema_type(*_t.type_)),
|
||||
.minContains = _t.size_,
|
||||
.maxContains = _t.size_}};
|
||||
.minItems = _t.size_,
|
||||
.maxItems = _t.size_}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Literal>()) {
|
||||
return schema::Type{.value =
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
#include <array>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace test_json_schema_size_validation {
|
||||
|
||||
using rfl::EqualTo;
|
||||
using rfl::Maximum;
|
||||
using rfl::Minimum;
|
||||
using rfl::json::to_schema;
|
||||
|
||||
template <typename T, typename... Validators>
|
||||
using SizedAnyOf = rfl::Validator<T, rfl::Size<rfl::AnyOf<Validators...>>>;
|
||||
template <typename T, typename... Validators>
|
||||
using SizedAllOf = rfl::Validator<T, rfl::Size<rfl::AllOf<Validators...>>>;
|
||||
template <typename T, auto Min, auto Max>
|
||||
using SizedMinMax =
|
||||
rfl::Validator<T, rfl::Size<Minimum<Min>>, rfl::Size<Maximum<Max>>>;
|
||||
|
||||
TEST(json, test_json_schema_sized_vector_min1_max2) {
|
||||
constexpr auto expected =
|
||||
R"({"$schema":"https://json-schema.org/draft/2020-12/schema","allOf":[{"type":"array","items":{"type":"integer"},"minItems":1},{"type":"array","items":{"type":"integer"},"maxItems":2}],"definitions":{}})";
|
||||
|
||||
auto actual = to_schema<SizedMinMax<std::vector<int>, 1, 2>>();
|
||||
ASSERT_EQ(expected, actual);
|
||||
actual = to_schema<SizedAllOf<std::vector<int>, Minimum<1>, Maximum<2>>>();
|
||||
ASSERT_EQ(expected, actual);
|
||||
}
|
||||
|
||||
TEST(json, test_json_schema_sized_set_min2_max3) {
|
||||
constexpr auto expected =
|
||||
R"({"$schema":"https://json-schema.org/draft/2020-12/schema","allOf":[{"type":"array","items":{"type":"integer"},"minItems":2},{"type":"array","items":{"type":"integer"},"maxItems":3}],"definitions":{}})";
|
||||
|
||||
auto actual = to_schema<SizedMinMax<std::set<int>, 2, 3>>();
|
||||
ASSERT_EQ(expected, actual);
|
||||
actual = to_schema<SizedAllOf<std::set<int>, Minimum<2>, Maximum<3>>>();
|
||||
ASSERT_EQ(expected, actual);
|
||||
}
|
||||
|
||||
TEST(json, test_json_schema_sized_string_min4_max6) {
|
||||
constexpr auto expected =
|
||||
R"({"$schema":"https://json-schema.org/draft/2020-12/schema","allOf":[{"type":"string","minLength":4},{"type":"string","maxLength":6}],"definitions":{}})";
|
||||
|
||||
auto actual = to_schema<SizedMinMax<std::string, 4, 6>>();
|
||||
ASSERT_EQ(expected, actual);
|
||||
actual = to_schema<SizedAllOf<std::string, Minimum<4>, Maximum<6>>>();
|
||||
ASSERT_EQ(expected, actual);
|
||||
}
|
||||
|
||||
TEST(json, test_json_schema_sized_vector_anyof_eq3_eq7) {
|
||||
constexpr auto expected =
|
||||
R"({"$schema":"https://json-schema.org/draft/2020-12/schema","anyOf":[{"type":"array","items":{"type":"integer"},"minItems":3,"maxItems":3},{"type":"array","items":{"type":"integer"},"minItems":7,"maxItems":7}],"definitions":{}})";
|
||||
|
||||
auto actual =
|
||||
to_schema<SizedAnyOf<std::vector<int>, EqualTo<3>, EqualTo<7>>>();
|
||||
ASSERT_EQ(expected, actual);
|
||||
}
|
||||
|
||||
TEST(json, test_json_schema_sized_set_anyof_eq15_eq16) {
|
||||
constexpr auto expected =
|
||||
R"({"$schema":"https://json-schema.org/draft/2020-12/schema","anyOf":[{"type":"array","items":{"type":"integer"},"minItems":16,"maxItems":16},{"type":"array","items":{"type":"integer"},"minItems":16,"maxItems":16}],"definitions":{}})";
|
||||
|
||||
auto actual =
|
||||
to_schema<SizedAnyOf<std::vector<int>, EqualTo<16>, EqualTo<16>>>();
|
||||
ASSERT_EQ(expected, actual);
|
||||
}
|
||||
|
||||
TEST(json, test_json_schema_sized_string_anyof_eq1_eq10) {
|
||||
constexpr auto expected =
|
||||
R"({"$schema":"https://json-schema.org/draft/2020-12/schema","anyOf":[{"type":"array","items":{"type":"integer"},"minItems":1,"maxItems":1},{"type":"array","items":{"type":"integer"},"minItems":10,"maxItems":10}],"definitions":{}})";
|
||||
|
||||
auto actual =
|
||||
to_schema<SizedAnyOf<std::vector<int>, EqualTo<1>, EqualTo<10>>>();
|
||||
ASSERT_EQ(expected, actual);
|
||||
}
|
||||
} // namespace test_json_schema_size_validation
|
||||
Reference in New Issue
Block a user