@@ -32,6 +32,7 @@ public function testImmutability(): void
3232 self ::assertNotSame ($ schema , $ schema ->minItems (1 ));
3333 self ::assertNotSame ($ schema , $ schema ->maxItems (1 ));
3434 self ::assertNotSame ($ schema , $ schema ->contains ('test ' ));
35+ self ::assertNotSame ($ schema , $ schema ->uniqueItems ());
3536 self ::assertNotSame ($ schema , $ schema ->filter (static fn (mixed $ value ) => true ));
3637 self ::assertNotSame ($ schema , $ schema ->map (static fn (mixed $ value ) => $ value ));
3738 self ::assertNotSame ($ schema , $ schema ->sort ());
@@ -654,6 +655,100 @@ public function testParseWithInvalidIncludes(): void
654655 }
655656 }
656657
658+ public function testParseWithValidUniqueItems (): void
659+ {
660+ $ input = [1 , 2 , 3 ];
661+
662+ $ schema = (new ArraySchema (new IntSchema ()))->uniqueItems ();
663+
664+ self ::assertSame ($ input , $ schema ->parse ($ input ));
665+ }
666+
667+ public function testParseIntItemsWithInvalidUniqueItems (): void
668+ {
669+ $ input = [5 , 7 , 7 , 12 , 3 , 9 , 5 , 14 , 2 , 9 , 6 , 3 , 18 , 4 , 14 , 10 , 2 , 11 , 6 , 13 , 8 , 12 , 15 , 16 , 8 , 17 , 19 , 20 , 21 , 15 ];
670+
671+ $ schema = (new ArraySchema (new IntSchema ()))->uniqueItems ();
672+
673+ try {
674+ $ schema ->parse ($ input );
675+
676+ throw new \Exception ('code should not be reached ' );
677+ } catch (ErrorsException $ errorsException ) {
678+ self ::assertSame ([
679+ [
680+ 'path ' => '' ,
681+ 'error ' => [
682+ 'code ' => 'array.uniqueItems ' ,
683+ 'template ' => 'Duplicate keys {{duplicateKeys}}, {{given}} given ' ,
684+ 'variables ' => [
685+ 'duplicateKeys ' => [2 , 6 , 9 , 11 , 14 , 16 , 18 , 21 , 24 , 29 ],
686+ 'given ' => $ input ,
687+ ],
688+ ],
689+ ],
690+ ], $ errorsException ->errors ->jsonSerialize ());
691+ }
692+ }
693+
694+ public function testParseIStringItemsWithInvalidUniqueItems (): void
695+ {
696+ $ input = [
697+ 'apple ' ,
698+ 'pear ' ,
699+ 'pear ' ,
700+ 'plum ' ,
701+ 'kiwi ' ,
702+ 'mango ' ,
703+ 'apple ' ,
704+ 'peach ' ,
705+ 'lime ' ,
706+ 'mango ' ,
707+ 'fig ' ,
708+ 'kiwi ' ,
709+ 'grape ' ,
710+ 'melon ' ,
711+ 'peach ' ,
712+ 'berry ' ,
713+ 'lime ' ,
714+ 'coconut ' ,
715+ 'fig ' ,
716+ 'papaya ' ,
717+ 'date ' ,
718+ 'plum ' ,
719+ 'lemon ' ,
720+ 'orange ' ,
721+ 'date ' ,
722+ 'guava ' ,
723+ 'nectarine ' ,
724+ 'apricot ' ,
725+ 'banana ' ,
726+ 'lemon ' ,
727+ ];
728+
729+ $ schema = (new ArraySchema (new StringSchema ()))->uniqueItems ();
730+
731+ try {
732+ $ schema ->parse ($ input );
733+
734+ throw new \Exception ('code should not be reached ' );
735+ } catch (ErrorsException $ errorsException ) {
736+ self ::assertSame ([
737+ [
738+ 'path ' => '' ,
739+ 'error ' => [
740+ 'code ' => 'array.uniqueItems ' ,
741+ 'template ' => 'Duplicate keys {{duplicateKeys}}, {{given}} given ' ,
742+ 'variables ' => [
743+ 'duplicateKeys ' => [2 , 6 , 9 , 11 , 14 , 16 , 18 , 21 , 24 , 29 ],
744+ 'given ' => $ input ,
745+ ],
746+ ],
747+ ],
748+ ], $ errorsException ->errors ->jsonSerialize ());
749+ }
750+ }
751+
657752 public function testParseWithFilter (): void
658753 {
659754 $ input = [1 , 2 , 3 , 4 , 5 ];
0 commit comments