|
5 | 5 | use Jane\Component\JsonSchema\Console\Command\GenerateCommand; |
6 | 6 | use Jane\Component\JsonSchema\Console\Loader\ConfigLoader; |
7 | 7 | use Jane\Component\JsonSchema\Console\Loader\SchemaLoader; |
| 8 | +use Jane\Component\JsonSchema\Tests\Validation\Generated\Model\ArrayItemsObject; |
8 | 9 | use Jane\Component\JsonSchema\Tests\Validation\Generated\Model\ArrayObject; |
9 | 10 | use Jane\Component\JsonSchema\Tests\Validation\Generated\Model\FormatObject; |
10 | 11 | use Jane\Component\JsonSchema\Tests\Validation\Generated\Model\NumericObject; |
|
16 | 17 | use Jane\Component\JsonSchema\Tests\Validation\Generated\Model\StringObject; |
17 | 18 | use Jane\Component\JsonSchema\Tests\Validation\Generated\Model\TypeObject; |
18 | 19 | use Jane\Component\JsonSchema\Tests\Validation\Generated\Model\VerifyNullableStringPropertyWithMinLengthValidatesCorrectly; |
| 20 | +use Jane\Component\JsonSchema\Tests\Validation\Generated\Normalizer\ArrayItemsObjectNormalizer; |
19 | 21 | use Jane\Component\JsonSchema\Tests\Validation\Generated\Normalizer\ArrayObjectNormalizer; |
20 | 22 | use Jane\Component\JsonSchema\Tests\Validation\Generated\Normalizer\FormatObjectNormalizer; |
21 | 23 | use Jane\Component\JsonSchema\Tests\Validation\Generated\Normalizer\NumericObjectNormalizer; |
@@ -81,6 +83,9 @@ public function testValidation(): void |
81 | 83 |
|
82 | 84 | // 12. |
83 | 85 | $this->verifyNullableStringPropertyWithMinLengthValidatesCorrectly(); |
| 86 | + |
| 87 | + // 13. Array items validation |
| 88 | + $this->arrayItemsValidation(); |
84 | 89 | } |
85 | 90 |
|
86 | 91 | private function numericValidation(): void |
@@ -948,4 +953,168 @@ private function verifyNullableStringPropertyWithMinLengthValidatesCorrectly(): |
948 | 953 | self::assertInstanceOf(VerifyNullableStringPropertyWithMinLengthValidatesCorrectly::class, $data); |
949 | 954 | self::assertNull($data->getName()); |
950 | 955 | } |
| 956 | + |
| 957 | + private function arrayItemsValidation(): void |
| 958 | + { |
| 959 | + $normalizer = new ArrayItemsObjectNormalizer(); |
| 960 | + |
| 961 | + // Valid uuid array |
| 962 | + $data = $normalizer->denormalize([ |
| 963 | + 'uuidArray' => ['8309e3b3-0c6c-4ab8-b450-e7564f6d07fd', 'a1b2c3d4-e5f6-7890-abcd-ef1234567890'], |
| 964 | + ], ArrayItemsObject::class); |
| 965 | + $this->assertInstanceOf(ArrayItemsObject::class, $data); |
| 966 | + |
| 967 | + // Invalid uuid in array |
| 968 | + $caughtException = null; |
| 969 | + try { |
| 970 | + $normalizer->denormalize([ |
| 971 | + 'uuidArray' => ['8309e3b3-0c6c-4ab8-b450-e7564f6d07fd', 'not-a-uuid'], |
| 972 | + ], ArrayItemsObject::class); |
| 973 | + } catch (ValidationException $exception) { |
| 974 | + $caughtException = $exception; |
| 975 | + } |
| 976 | + |
| 977 | + $this->assertInstanceOf(ValidationException::class, $caughtException); |
| 978 | + $this->assertEquals(400, $caughtException->getCode()); |
| 979 | + $this->assertGreaterThanOrEqual(1, $caughtException->getViolationList()->count()); |
| 980 | + |
| 981 | + // Valid enum string array |
| 982 | + $data = $normalizer->denormalize([ |
| 983 | + 'enumStringArray' => ['alpha', 'beta'], |
| 984 | + ], ArrayItemsObject::class); |
| 985 | + $this->assertInstanceOf(ArrayItemsObject::class, $data); |
| 986 | + |
| 987 | + // Invalid enum value in array |
| 988 | + $caughtException = null; |
| 989 | + try { |
| 990 | + $normalizer->denormalize([ |
| 991 | + 'enumStringArray' => ['alpha', 'invalid'], |
| 992 | + ], ArrayItemsObject::class); |
| 993 | + } catch (ValidationException $exception) { |
| 994 | + $caughtException = $exception; |
| 995 | + } |
| 996 | + |
| 997 | + $this->assertInstanceOf(ValidationException::class, $caughtException); |
| 998 | + $this->assertEquals(400, $caughtException->getCode()); |
| 999 | + $this->assertGreaterThanOrEqual(1, $caughtException->getViolationList()->count()); |
| 1000 | + |
| 1001 | + // Valid constrained string array |
| 1002 | + $data = $normalizer->denormalize([ |
| 1003 | + 'constrainedStringArray' => ['ab', 'abcde'], |
| 1004 | + ], ArrayItemsObject::class); |
| 1005 | + $this->assertInstanceOf(ArrayItemsObject::class, $data); |
| 1006 | + |
| 1007 | + // String too short in array |
| 1008 | + $caughtException = null; |
| 1009 | + try { |
| 1010 | + $normalizer->denormalize([ |
| 1011 | + 'constrainedStringArray' => ['a'], |
| 1012 | + ], ArrayItemsObject::class); |
| 1013 | + } catch (ValidationException $exception) { |
| 1014 | + $caughtException = $exception; |
| 1015 | + } |
| 1016 | + |
| 1017 | + $this->assertInstanceOf(ValidationException::class, $caughtException); |
| 1018 | + $this->assertEquals(400, $caughtException->getCode()); |
| 1019 | + $this->assertGreaterThanOrEqual(1, $caughtException->getViolationList()->count()); |
| 1020 | + |
| 1021 | + // String too long in array |
| 1022 | + $caughtException = null; |
| 1023 | + try { |
| 1024 | + $normalizer->denormalize([ |
| 1025 | + 'constrainedStringArray' => ['this-is-way-too-long-string'], |
| 1026 | + ], ArrayItemsObject::class); |
| 1027 | + } catch (ValidationException $exception) { |
| 1028 | + $caughtException = $exception; |
| 1029 | + } |
| 1030 | + |
| 1031 | + $this->assertInstanceOf(ValidationException::class, $caughtException); |
| 1032 | + $this->assertEquals(400, $caughtException->getCode()); |
| 1033 | + $this->assertGreaterThanOrEqual(1, $caughtException->getViolationList()->count()); |
| 1034 | + |
| 1035 | + // Valid constrained integer array |
| 1036 | + $data = $normalizer->denormalize([ |
| 1037 | + 'constrainedIntegerArray' => [1, 50, 100], |
| 1038 | + ], ArrayItemsObject::class); |
| 1039 | + $this->assertInstanceOf(ArrayItemsObject::class, $data); |
| 1040 | + |
| 1041 | + // Integer below minimum in array |
| 1042 | + $caughtException = null; |
| 1043 | + try { |
| 1044 | + $normalizer->denormalize([ |
| 1045 | + 'constrainedIntegerArray' => [0], |
| 1046 | + ], ArrayItemsObject::class); |
| 1047 | + } catch (ValidationException $exception) { |
| 1048 | + $caughtException = $exception; |
| 1049 | + } |
| 1050 | + |
| 1051 | + $this->assertInstanceOf(ValidationException::class, $caughtException); |
| 1052 | + $this->assertEquals(400, $caughtException->getCode()); |
| 1053 | + $this->assertGreaterThanOrEqual(1, $caughtException->getViolationList()->count()); |
| 1054 | + |
| 1055 | + // Integer above maximum in array |
| 1056 | + $caughtException = null; |
| 1057 | + try { |
| 1058 | + $normalizer->denormalize([ |
| 1059 | + 'constrainedIntegerArray' => [101], |
| 1060 | + ], ArrayItemsObject::class); |
| 1061 | + } catch (ValidationException $exception) { |
| 1062 | + $caughtException = $exception; |
| 1063 | + } |
| 1064 | + |
| 1065 | + $this->assertInstanceOf(ValidationException::class, $caughtException); |
| 1066 | + $this->assertEquals(400, $caughtException->getCode()); |
| 1067 | + $this->assertGreaterThanOrEqual(1, $caughtException->getViolationList()->count()); |
| 1068 | + |
| 1069 | + // Valid email array |
| 1070 | + $data = $normalizer->denormalize([ |
| 1071 | + 'emailArray' => ['foo@bar.com', 'test@example.org'], |
| 1072 | + ], ArrayItemsObject::class); |
| 1073 | + $this->assertInstanceOf(ArrayItemsObject::class, $data); |
| 1074 | + |
| 1075 | + // Invalid email in array |
| 1076 | + $caughtException = null; |
| 1077 | + try { |
| 1078 | + $normalizer->denormalize([ |
| 1079 | + 'emailArray' => ['not-an-email'], |
| 1080 | + ], ArrayItemsObject::class); |
| 1081 | + } catch (ValidationException $exception) { |
| 1082 | + $caughtException = $exception; |
| 1083 | + } |
| 1084 | + |
| 1085 | + $this->assertInstanceOf(ValidationException::class, $caughtException); |
| 1086 | + $this->assertEquals(400, $caughtException->getCode()); |
| 1087 | + $this->assertGreaterThanOrEqual(1, $caughtException->getViolationList()->count()); |
| 1088 | + |
| 1089 | + // Valid pattern array |
| 1090 | + $data = $normalizer->denormalize([ |
| 1091 | + 'patternArray' => ['ABC', 'XYZ'], |
| 1092 | + ], ArrayItemsObject::class); |
| 1093 | + $this->assertInstanceOf(ArrayItemsObject::class, $data); |
| 1094 | + |
| 1095 | + // Invalid pattern in array |
| 1096 | + $caughtException = null; |
| 1097 | + try { |
| 1098 | + $normalizer->denormalize([ |
| 1099 | + 'patternArray' => ['abc'], |
| 1100 | + ], ArrayItemsObject::class); |
| 1101 | + } catch (ValidationException $exception) { |
| 1102 | + $caughtException = $exception; |
| 1103 | + } |
| 1104 | + |
| 1105 | + $this->assertInstanceOf(ValidationException::class, $caughtException); |
| 1106 | + $this->assertEquals(400, $caughtException->getCode()); |
| 1107 | + $this->assertGreaterThanOrEqual(1, $caughtException->getViolationList()->count()); |
| 1108 | + |
| 1109 | + // Empty array is always valid |
| 1110 | + $data = $normalizer->denormalize([ |
| 1111 | + 'uuidArray' => [], |
| 1112 | + 'enumStringArray' => [], |
| 1113 | + 'constrainedStringArray' => [], |
| 1114 | + 'constrainedIntegerArray' => [], |
| 1115 | + 'emailArray' => [], |
| 1116 | + 'patternArray' => [], |
| 1117 | + ], ArrayItemsObject::class); |
| 1118 | + $this->assertInstanceOf(ArrayItemsObject::class, $data); |
| 1119 | + } |
951 | 1120 | } |
0 commit comments