88use PHPUnit \Framework \TestCase ;
99use Yiisoft \Validator \Error ;
1010use Yiisoft \Validator \Result ;
11+ use Yiisoft \Validator \Rule \Callback ;
12+ use Yiisoft \Validator \Rule \Nested ;
13+ use Yiisoft \Validator \Validator ;
1114
1215class ResultTest extends TestCase
1316{
@@ -68,8 +71,8 @@ public function testGetErrorMessagesIndexedByPathWithAttributes(): void
6871 'attribute2 ' => ['error2.1 ' , 'error2.2 ' ],
6972 'attribute2.nested ' => ['error2.3 ' , 'error2.4 ' ],
7073 '' => ['error3.1 ' , 'error3.2 ' ],
71- 'attribute4.subattribute4\.1.subattribute4\ *2 ' => ['error4.1 ' ],
72- 'attribute4.subattribute4\.3.subattribute4\ *4 ' => ['error4.2 ' ],
74+ 'attribute4.subattribute4\.1.subattribute4*2 ' => ['error4.1 ' ],
75+ 'attribute4.subattribute4\.3.subattribute4*4 ' => ['error4.2 ' ],
7376 ],
7477 $ this ->createAttributeErrorResult ()->getErrorMessagesIndexedByPath ()
7578 );
@@ -164,8 +167,8 @@ public function testGetAttributeErrorMessagesIndexedByPath(): void
164167 );
165168 $ this ->assertEquals (['' => ['error3.1 ' , 'error3.2 ' ]], $ result ->getAttributeErrorMessagesIndexedByPath ('' ));
166169 $ this ->assertEquals ([
167- 'subattribute4\.1.subattribute4\ *2 ' => ['error4.1 ' ],
168- 'subattribute4\.3.subattribute4\ *4 ' => ['error4.2 ' ],
170+ 'subattribute4\.1.subattribute4*2 ' => ['error4.1 ' ],
171+ 'subattribute4\.3.subattribute4*4 ' => ['error4.2 ' ],
169172 ], $ result ->getAttributeErrorMessagesIndexedByPath ('attribute4 ' ));
170173 }
171174
@@ -174,6 +177,59 @@ public function testGetCommonErrorMessages(): void
174177 $ this ->assertEquals (['error3.1 ' , 'error3.2 ' ], $ this ->createAttributeErrorResult ()->getCommonErrorMessages ());
175178 }
176179
180+ /**
181+ * @see https://github.com/yiisoft/validator/issues/610
182+ */
183+ public function testDataKeysWithDots (): void
184+ {
185+ $ result = (new Validator ())->validate (
186+ [
187+ 'user.age ' => 17 ,
188+ 'meta ' => [
189+ 'tag ' => 'hi ' ,
190+ ],
191+ ],
192+ [
193+ 'user.age ' => static fn () => (new Result ())->addError ('Too young. ' ),
194+ 'meta ' => new Nested ([
195+ 'tag ' => new Callback (static fn () => (new Result ())->addError ('Too short. ' )),
196+ ]),
197+ ],
198+ );
199+
200+ $ this ->assertSame (
201+ [
202+ 'user\.age ' => ['Too young. ' ],
203+ 'meta.tag ' => ['Too short. ' ],
204+ ],
205+ $ result ->getErrorMessagesIndexedByPath ()
206+ );
207+ }
208+
209+ public function testEscapeInGetErrorMessagesIndexedByPath (): void
210+ {
211+ $ result = (new Result ())->addError ('e1 ' , valuePath: ['user ' , 'meta.the-age ' ]);
212+
213+ $ this ->assertSame (
214+ [
215+ 'user.meta.the\-age ' => ['e1 ' ],
216+ ],
217+ $ result ->getErrorMessagesIndexedByPath (escape: '- ' ),
218+ );
219+ }
220+
221+ public function testEscapeInGetAttributeErrorMessagesIndexedByPath (): void
222+ {
223+ $ result = (new Result ())->addError ('e1 ' , valuePath: ['user ' , 'data ' , 'meta.the-age ' ]);
224+
225+ $ this ->assertSame (
226+ [
227+ 'data.meta.the\-age ' => ['e1 ' ],
228+ ],
229+ $ result ->getAttributeErrorMessagesIndexedByPath ('user ' , escape: '- ' ),
230+ );
231+ }
232+
177233 private function createAttributeErrorResult (): Result
178234 {
179235 $ result = new Result ();
0 commit comments