1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 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
|