RuleValidationTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. <?php declare(strict_types=1);
  2. namespace Inhere\ValidateTest;
  3. use Inhere\Validate\RuleValidation;
  4. use Inhere\Validate\RV;
  5. use Inhere\Validate\Validation;
  6. use Inhere\ValidateTest\Validator\AdemoValidator as AdemoValidator;
  7. use PHPUnit\Framework\TestCase;
  8. use PHPUnit\Runner\Version;
  9. use Throwable;
  10. use function version_compare;
  11. /**
  12. * Class RuleValidationTest
  13. */
  14. class RuleValidationTest extends TestCase
  15. {
  16. public function testBasic(): void
  17. {
  18. $v = Validation::make([
  19. 'key' => 'val',
  20. ]);
  21. $this->assertTrue($v->has('key'));
  22. $this->assertSame('val', $v->get('key'));
  23. $this->assertNotEmpty($v->all());
  24. $this->assertFalse($v->has('key1'));
  25. $v->setRaw('key1', 'val1');
  26. $this->assertTrue($v->has('key1'));
  27. $this->assertFalse($v->hasRule());
  28. $rv = RuleValidation::make(['name' => 'inhere'], [
  29. []
  30. ]);
  31. try {
  32. $rv->validate();
  33. } catch (Throwable $e) {
  34. $this->assertSame(
  35. 'Please setting the fields(string|array) to wait validate! position: rule[0]',
  36. $e->getMessage()
  37. );
  38. }
  39. $rv = RuleValidation::make(['name' => 'inhere'], [
  40. ['name']
  41. ]);
  42. try {
  43. $rv->validate();
  44. } catch (Throwable $e) {
  45. $this->assertSame('The rule validator is must be setting! position: rule[1]', $e->getMessage());
  46. }
  47. }
  48. public function testRequired(): void
  49. {
  50. $data = [
  51. 'userId' => 0,
  52. 'tagId' => 10,
  53. 'goods' => [
  54. 'apple' => 34,
  55. 'pear' => 50,
  56. ],
  57. ];
  58. $v = RuleValidation::makeAndValidate($data, [
  59. ['userId, tagId, goods.apple', 'required']
  60. ]);
  61. $this->assertCount(0, $v->getErrors());
  62. }
  63. /**
  64. * 如果指定的其它字段( anotherField )等于任何一个 value 时,被验证的字段必须存在且不为空。
  65. */
  66. public function testRequiredIf(): void
  67. {
  68. $data = [
  69. 'userId' => 0,
  70. 'targetId' => null,
  71. 'status' => 10,
  72. ];
  73. $v = RuleValidation::makeAndValidate($data, [
  74. ['userId, targetId', 'requiredIf', 'status', [10]]
  75. ]);
  76. $this->assertCount(1, $v->getErrors());
  77. $this->assertFalse($v->inError('userId'));
  78. $this->assertTrue($v->inError('targetId'));
  79. $v = RuleValidation::check($data, [
  80. ['userId, targetId', 'requiredIf', 'status', 5]
  81. ]);
  82. $this->assertCount(0, $v->getErrors());
  83. $this->assertCount(0, $v->getSafeData());
  84. }
  85. /**
  86. * 如果指定的另一个字段( anotherField )值等于任何一个 value 时,此字段为 不必填
  87. */
  88. public function testRequiredUnless(): void
  89. {
  90. $data = [
  91. 'userId' => null,
  92. 'targetId' => null,
  93. 'status' => 10,
  94. ];
  95. $v = RuleValidation::check($data, [
  96. ['targetId', 'requiredUnless', 'status', [10]],
  97. ['userId', 'requiredUnless', 'status', [11]],
  98. ['userId', 'requiredUnless', 'not-exists', [11]],
  99. ]);
  100. $this->assertCount(1, $v->getErrors());
  101. $this->assertFalse($v->inError('targetId'));
  102. $this->assertTrue($v->inError('userId'));
  103. }
  104. /**
  105. * 只要在指定的其他字段中有任意一个字段存在时,被验证的字段就必须存在并且不能为空。
  106. */
  107. public function testRequiredWith(): void
  108. {
  109. $data = [
  110. 'userId' => null,
  111. 'targetId' => 2,
  112. 'status' => 10,
  113. ];
  114. $v = RuleValidation::check($data, [
  115. ['targetId', 'requiredWith', 'status'],
  116. ['userId', 'requiredWith', ['status', 'someField']],
  117. ]);
  118. $this->assertCount(1, $v->getErrors());
  119. $this->assertFalse($v->inError('targetId'));
  120. $this->assertTrue($v->inError('userId'));
  121. }
  122. /**
  123. * 只有当所有的 其他指定字段 全部存在时,被验证的字段才 必须存在并且不能为空。
  124. */
  125. public function testRequiredWithAll(): void
  126. {
  127. $data = [
  128. 'userId' => null,
  129. 'targetId' => null,
  130. 'status' => 10,
  131. ];
  132. $v = RuleValidation::check($data, [
  133. ['targetId', 'requiredWithAll', 'status'],
  134. ['userId', 'requiredWithAll', ['status', 'someField']],
  135. ]);
  136. $this->assertCount(1, $v->getErrors());
  137. $this->assertTrue($v->inError('targetId'));
  138. $this->assertFalse($v->inError('userId'));
  139. }
  140. /**
  141. * 只要在其他指定的字段中 有任意一个字段不存在,被验证的字段就 必须存在且不为空。
  142. */
  143. public function testRequiredWithout(): void
  144. {
  145. $data = [
  146. 'userId' => null,
  147. 'targetId' => null,
  148. 'status' => 10,
  149. ];
  150. $v = RuleValidation::check($data, [
  151. ['targetId', 'requiredWithout', 'status'],
  152. ['userId', 'requiredWithout', ['status', 'someField']],
  153. ]);
  154. $this->assertCount(1, $v->getErrors());
  155. $this->assertTrue($v->inError('userId'));
  156. $this->assertFalse($v->inError('targetId'));
  157. }
  158. /**
  159. * 只有当所有的 其他指定的字段 都不存在时,被验证的字段才 必须存在且不为空。
  160. */
  161. public function testRequiredWithoutAll(): void
  162. {
  163. $data = [
  164. 'userId' => null,
  165. 'targetId' => null,
  166. 'status' => 10,
  167. ];
  168. $v = RuleValidation::makeAndValidate($data, [
  169. ['targetId', 'requiredWithoutAll', 'someField'],
  170. ['userId', 'requiredWithoutAll', ['status', 'someField']],
  171. ]);
  172. $this->assertCount(1, $v->getErrors());
  173. $this->assertTrue($v->inError('targetId'));
  174. $this->assertFalse($v->inError('userId'));
  175. }
  176. public function testCollectRules(): void
  177. {
  178. $data = [
  179. 'userId' => 234,
  180. 'tagId' => 35,
  181. 'freeTime' => '1456767657',
  182. 'status' => 2,
  183. 'name' => '1234a2',
  184. 'goods' => [
  185. 'apple' => 34,
  186. 'pear' => 50,
  187. ],
  188. ];
  189. $rules = [
  190. ['tagId,userId,freeTime', 'required'],
  191. ['tagId,userId,freeTime', 'number', 'on' => 's1'],
  192. ['tagId', 'size', 'max' => 567, 'min' => 4, 'on' => 's2'],
  193. ['name', 'string', 'on' => 's2'],
  194. ['goods.pear', 'max', 60],
  195. ];
  196. $v = RuleValidation::make($data, $rules)->validate();
  197. $this->assertTrue($v->isOk());
  198. $this->assertCount(2, $v->getUsedRules());
  199. $v = RuleValidation::make($data, $rules)->atScene('s1')->validate();
  200. $this->assertTrue($v->isOk());
  201. $this->assertCount(3, $v->getUsedRules());
  202. $v = RuleValidation::make($data, $rules)->atScene('s2')->validate();
  203. $this->assertTrue($v->isOk());
  204. $this->assertCount(4, $v->getUsedRules());
  205. }
  206. public array $data = [
  207. // 'userId' => 234,
  208. 'userId' => 'is not an integer',
  209. 'tagId' => '35',
  210. // 'freeTime' => '1456767657', // filed not exists
  211. 'note' => '',
  212. 'status' => 2,
  213. 'name' => '1234a2',
  214. 'existsField' => 'test',
  215. 'passwd' => 'password',
  216. 'repasswd' => 'repassword',
  217. 'insertTime' => '1456767657',
  218. 'goods' => [
  219. 'apple' => 34,
  220. 'pear' => 50,
  221. ],
  222. ];
  223. public function testValidatePassed(): void
  224. {
  225. $data = $this->data;
  226. // change value
  227. $data['userId'] = '456';
  228. $rules = [
  229. // ['tagId,userId,freeTime', 'required'],// set message
  230. ['tagId,userId,freeTime', 'number', 'filter' => 'int'],
  231. ['tagId', 'size', 'max' => 567, 'min' => 4, 'filter' => 'int'], // 4<= tagId <=567
  232. // ['goods', 'isList'],
  233. ['goods.pear', 'max', 60],
  234. ['insertTime', 'safe']
  235. ];
  236. $v = RuleValidation::make($data, $rules);
  237. $v->setTranslates([
  238. 'goods.pear' => '梨子'
  239. ])->setMessages([
  240. 'freeTime.required' => 'freeTime is required!!!!'
  241. ])->validate([], false);
  242. $this->assertTrue($v->isOk());
  243. $this->assertFalse($v->failed());
  244. $this->assertEmpty($v->getErrors());
  245. $this->assertSame($v->getSafe('userId'), 456);
  246. $this->assertSame($v->getSafe('tagId'), 35);
  247. }
  248. public function testValidateFailed(): void
  249. {
  250. $rules = $this->someRules();
  251. ob_start();
  252. $v = RuleValidation::make($this->data, $rules);
  253. $v->setTranslates([
  254. 'goods.pear' => '梨子'
  255. ])->setMessages([
  256. 'freeTime.required' => 'freeTime is required!!!!'
  257. ])->validate([], false);
  258. $out = ob_get_clean();
  259. $needle = 'use when pre-check';
  260. if (version_compare(Version::id(), '7.0.0', '<')) {
  261. $this->assertContains($needle, $out);
  262. } else {
  263. $this->assertStringContainsString($needle, $out);
  264. }
  265. $needle = 'use custom validate';
  266. if (version_compare(Version::id(), '7.0.0', '<')) {
  267. $this->assertContains($needle, $out);
  268. } else {
  269. $this->assertStringContainsString($needle, $out);
  270. }
  271. $this->assertFalse($v->isOk());
  272. $this->assertTrue($v->failed());
  273. $errors = $v->getErrors();
  274. $this->assertNotEmpty($errors);
  275. $this->assertTrue(count($errors) > 3);
  276. $this->assertEquals($v->getSafe('tagId'), null);
  277. }
  278. public function testValidateRegex(): void
  279. {
  280. $v = RuleValidation::check([
  281. 'text1' => 'hello-world',
  282. 'text2' => 'hello world中文',
  283. ], [
  284. ['text1, text2', 'string'],
  285. ['text1', 'regex', '/^[\w-]+$/'],
  286. ['text2', 'regex', '/[\x{4e00}-\x{9fa5}]+/u'],
  287. ]);
  288. $this->assertTrue($v->isOk());
  289. $this->assertFalse($v->isFail());
  290. $errors = $v->getErrors();
  291. $this->assertEmpty($errors);
  292. $safeData = $v->getSafeData();
  293. $this->assertArrayHasKey('text2', $safeData);
  294. }
  295. public function testValidateString(): void
  296. {
  297. $val = '123482';
  298. $v = RuleValidation::make([
  299. 'user_name' => $val
  300. ], [
  301. ['user_name', 'string', 'min' => 6],
  302. ['user_name', 'string', 'max' => 17],
  303. ])->validate();
  304. $this->assertTrue($v->isOk());
  305. $this->assertFalse($v->failed());
  306. $errors = $v->getErrors();
  307. $this->assertEmpty($errors);
  308. $this->assertCount(0, $errors);
  309. $this->assertEquals($v->getSafe('user_name'), $val);
  310. }
  311. public function testValidateJson(): void
  312. {
  313. $v = RuleValidation::make([
  314. 'log_level' => 'debug',
  315. 'log_data' => '[23]',
  316. 'log_data1' => '234',
  317. ], [
  318. ['log_level, log_data', 'required'],
  319. ['log_level, log_data', 'string'],
  320. ['log_data', 'json'],
  321. ['log_data1', 'json', false],
  322. ])->validate();
  323. $this->assertTrue($v->isOk());
  324. $this->assertFalse($v->failed());
  325. $errors = $v->getErrors();
  326. $this->assertEmpty($errors);
  327. $this->assertCount(0, $errors);
  328. }
  329. protected function someRules(): array
  330. {
  331. return [
  332. ['tagId,userId,freeTime', 'required'],// set message
  333. ['tagId,userId,freeTime', 'number'],
  334. ['note', 'email', 'skipOnEmpty' => false], // set skipOnEmpty is false.
  335. ['insertTime', 'email', 'scene' => 'otherScene'],// set scene. will is not validate it on default.
  336. ['tagId', 'size', 'max' => 567, 'min' => 4,], // 4<= tagId <=567
  337. ['passwd', 'compare', 'repasswd'], //
  338. ['name', 'regexp', '/^[a-z]\w{2,12}$/'],
  339. ['goods.pear', 'max', 30], //
  340. ['goods', 'isList'], //
  341. ['notExistsField1', 'requiredWithout', 'notExistsField2'], //
  342. // ['notExistsField1', 'requiredWithout', 'existsField'], //
  343. [
  344. 'freeTime',
  345. 'size',
  346. 'min' => 4,
  347. 'max' => 567,
  348. 'when' => function () {
  349. echo " use when pre-check\n";
  350. // $valid is current validation instance.
  351. return true;
  352. }
  353. ], // 4<= tagId <=567
  354. [
  355. 'userId',
  356. function () {
  357. echo " use custom validate to check userId \n";
  358. // echo __LINE__ . "\n";
  359. return false;
  360. },
  361. 'msg' => 'userId check failure by closure!'
  362. ],
  363. ];
  364. }
  365. /**
  366. * 测试自定义验证器
  367. */
  368. public function testValidator(): void
  369. {
  370. $rule = [
  371. [
  372. 'user',
  373. new AdemoValidator(),
  374. 'msg' => 'userId check failure by closure!'
  375. ],
  376. ];
  377. $data = [
  378. 'user' => 1
  379. ];
  380. $validation = Validation::makeAndValidate($data, $rule);
  381. $this->assertTrue($validation->isOk());
  382. $validation = Validation::makeAndValidate(['user' => 2], $rule);
  383. $this->assertTrue($validation->isFail());
  384. }
  385. public function testArrayValidate(): void
  386. {
  387. $data = [
  388. 'options' => [
  389. 'opt1' => true,
  390. 'opt2' => 34,
  391. 'opt3' => 'string',
  392. 'opt4' => '0',
  393. ],
  394. 'key1' => [23, '56'],
  395. 'key2' => [23, 56],
  396. 'key3' => ['23', 'str'],
  397. ];
  398. $v = RuleValidation::makeAndValidate($data, [
  399. ['options, key1, key2, key3', 'isArray'],
  400. ['options', 'isMap'],
  401. ['key1', 'isList'],
  402. ['key1, key2', 'intList'],
  403. ['key3', 'strList'],
  404. ['options.opt2', 'num', 'min' => 30, 'max' => 50],
  405. ['options.opt3', 'string', 'min' => 3, 'max' => 12],
  406. ['options.opt1, options.opt4', 'bool'],
  407. ['options.opt1, options.opt4', 'in', [true, false]],
  408. ]);
  409. $this->assertTrue($v->isOk());
  410. $this->assertFalse($v->failed());
  411. }
  412. /**
  413. * 验证的 字段值 必须存在于另一个字段(anotherField)的值中。
  414. */
  415. public function testInField(): void
  416. {
  417. $v = RuleValidation::check([
  418. 'status' => 3,
  419. 'some' => 30,
  420. 'allowed' => [3, 4, 5],
  421. ], [
  422. ['status', 'inField', 'allowed'],
  423. ['some', 'inField', 'allowed'],
  424. ]);
  425. $this->assertFalse($v->isOk());
  426. $this->assertCount(1, $v->getErrors());
  427. $this->assertTrue($v->inError('some'));
  428. }
  429. /**
  430. * 验证的 字段值 必须存在于另一个字段(anotherField)的值中。
  431. */
  432. public function testRange(): void
  433. {
  434. $v = RuleValidation::make([
  435. 'num' => 3,
  436. 'id' => 300,
  437. ], [
  438. ['num', 'range', 'min' => 1, 'max' => 100],
  439. ['id', 'range', 'min' => 1, 'max' => 100],
  440. ])->setMessages([
  441. 'id.range' => 'range error message',
  442. ])->validate();
  443. $this->assertFalse($v->isOk());
  444. $this->assertCount(1, $v->getErrors());
  445. $this->assertTrue($v->inError('id'));
  446. $this->assertEquals('range error message', $v->firstError());
  447. }
  448. public function testDistinct(): void
  449. {
  450. $v = RuleValidation::makeAndValidate([
  451. 'tags' => [3, 4, 4],
  452. 'goods' => ['apple', 'pear'],
  453. 'users' => [
  454. ['id' => 34, 'name' => 'tom'],
  455. ['id' => 89, 'name' => 'john'],
  456. ],
  457. ], [
  458. ['tags', 'distinct'],
  459. ['goods.*', 'distinct'],
  460. ['users.*.id', 'distinct'],
  461. ]);
  462. $this->assertFalse($v->isOk());
  463. $this->assertCount(1, $v->getErrors());
  464. $this->assertTrue($v->inError('tags'));
  465. }
  466. public function testEach(): void
  467. {
  468. $v = RuleValidation::check([
  469. 'tags' => [3, 4, 5],
  470. 'goods' => ['apple', 'pear'],
  471. 'users' => [
  472. ['id' => 34, 'name' => 'tom'],
  473. ['id' => 89, 'name' => 'john'],
  474. ],
  475. ], [
  476. ['tags', 'each', 'number'],
  477. ['goods.*', 'each', 'string', 'min' => 4],
  478. ['users.*.id', 'each', 'required'],
  479. ['users.*.id', 'each', 'number', 'min' => 34],
  480. ['users.*.name', 'each', 'string', 'min' => 5],
  481. ]);
  482. $this->assertFalse($v->isOk());
  483. $this->assertCount(1, $v->getErrors());
  484. $this->assertTrue($v->inError('users.*.name'));
  485. $v = RuleValidation::check([
  486. 'users' => [
  487. ['id' => 34, 'name' => 'tom'],
  488. ['name' => 'john'],
  489. ],
  490. ], [
  491. ['users.*.id', 'each', 'required'],
  492. ['users.*.id', 'each', 'number', 'min' => 34],
  493. ]);
  494. $this->assertFalse($v->isOk());
  495. $this->assertCount(1, $v->getErrors());
  496. $this->assertTrue($v->inError('users.*.id'));
  497. }
  498. public function testMultiLevelData(): void
  499. {
  500. $v = RuleValidation::check([
  501. 'prod' => [
  502. 'key0' => 'val0',
  503. [
  504. 'attr' => [
  505. 'wid' => 1
  506. ]
  507. ]
  508. ]
  509. ], [
  510. ['prod.*.attr', 'each', 'required'],
  511. // ['prod.*.attr.wid', 'each', 'required'],
  512. ['prod.0.attr.wid', 'number'],
  513. ]);
  514. $this->assertTrue($v->isOk());
  515. $this->assertNotEmpty($v->getSafeData());
  516. }
  517. public function testGetMessage(): void
  518. {
  519. $v = Validation::check([
  520. 'inTest' => 3,
  521. ], [
  522. ['inTest', 'in', [1, 2]],
  523. ]);
  524. $this->assertFalse($v->isOk());
  525. $this->assertEquals('in test must in (1,2)', $v->firstError());
  526. }
  527. public function testValidatorAlias(): void
  528. {
  529. $v = Validation::check([
  530. 'arrTest' => [12, 23],
  531. ], [
  532. ['arrTest', 'list'],
  533. ['arrTest', 'array'],
  534. ]);
  535. $this->assertTrue($v->isOk());
  536. $v = Validation::make([
  537. 'arrVal' => 'string',
  538. 'listVal' => 'string',
  539. ], [
  540. ['arrVal', 'list'],
  541. ['listVal', 'array'],
  542. ]);
  543. $v->setStopOnError(false);
  544. $v->validate();
  545. $this->assertTrue($v->isFail());
  546. $this->assertFalse($v->isStopOnError());
  547. $this->assertCount(2, $v->getErrors());
  548. $this->assertTrue($v->inError('listVal'));
  549. $this->assertEquals('arr val must be an array of nature', $v->firstError());
  550. $this->assertEquals('list val must be an array', $v->lastError());
  551. }
  552. /**
  553. * @link https://github.com/inhere/php-validate/issues/13
  554. */
  555. public function testIssue13(): void
  556. {
  557. $rule = [
  558. ['goods_id', 'list', 'msg' => '商品id数组为空或不合法'],
  559. ['goods_id.*', 'each', 'integer', 'msg' => '商品分类id必须是一串数字']
  560. ];
  561. $v = Validation::check([
  562. 'goods_id' => [
  563. // 1144181460261978556,
  564. 114418146,
  565. 1144
  566. ]
  567. ], $rule);
  568. $this->assertTrue($v->isOk());
  569. $this->assertFalse($v->isFail());
  570. // not array
  571. $v = Validation::check([
  572. 'goods_id' => 'string'
  573. ], $rule);
  574. $this->assertFalse($v->isOk());
  575. $this->assertSame('商品id数组为空或不合法', $v->firstError());
  576. // not list
  577. $v = Validation::check([
  578. 'goods_id' => ['k' => 'v']
  579. ], $rule);
  580. $this->assertFalse($v->isOk());
  581. $this->assertSame('商品id数组为空或不合法', $v->firstError());
  582. // value not int
  583. $v = Validation::check([
  584. 'goods_id' => ['v']
  585. ], $rule);
  586. $this->assertFalse($v->isOk());
  587. $this->assertSame('商品分类id必须是一串数字', $v->firstError());
  588. }
  589. /**
  590. * @link https://github.com/inhere/php-validate/issues/17
  591. */
  592. public function testIssues17(): void
  593. {
  594. $data = [
  595. 'users' => [
  596. ['id' => 12,],
  597. ['id' => 23,],
  598. ],
  599. ];
  600. $rules = [
  601. ['users.*.id', 'required'],
  602. ['users.*.id', 'each', 'required'],
  603. ];
  604. $v = RV::check($data, $rules);
  605. $this->assertTrue($v->isOk());
  606. $rules = [
  607. [
  608. 'users.*.id',
  609. 'each',
  610. 'number',
  611. 'min' => 34,
  612. 'msg' => 'xxx error'
  613. ],
  614. ];
  615. $v = RV::check($data, $rules);
  616. $this->assertFalse($v->isOk());
  617. $this->assertSame('xxx error', $v->firstError());
  618. }
  619. /**
  620. * @link https://github.com/inhere/php-validate/issues/20
  621. */
  622. public function testIssues20(): void
  623. {
  624. $d = [
  625. 'product' => [
  626. [
  627. 'sku_id' => 1,
  628. 'properties' => 'aaa'
  629. ],
  630. [
  631. 'sku_id' => 2,
  632. 'properties' => 'bbb'
  633. ]
  634. ]
  635. ];
  636. $r = [
  637. ['product.*.properties', 'each', 'string', 'max' => 40],
  638. ];
  639. $v = RV::check($d, $r);
  640. $this->assertTrue($v->isOk());
  641. $r = [
  642. ['product.*.properties', 'each', 'string', 'max' => 2],
  643. ];
  644. $v = RV::check($d, $r);
  645. $this->assertFalse($v->isOk());
  646. $this->assertSame('product.*.properties each value must be through the "string" verify', $v->firstError());
  647. }
  648. /**
  649. * @link https://github.com/inhere/php-validate/issues/21
  650. */
  651. public function testIssues21(): void
  652. {
  653. $d1 = [
  654. // all items missing 'id' field
  655. 'users' => [
  656. ['name' => 'n1'],
  657. ['name' => 'n1'],
  658. ],
  659. ];
  660. $rs = [
  661. ['users.*.id', 'each', 'required'],
  662. ];
  663. $v = RV::check($d1, $rs);
  664. $this->assertFalse($v->isOk());
  665. $this->assertSame('users.*.id each value must be through the "required" verify', $v->firstError());
  666. $d2 = [
  667. 'users' => [
  668. ['name' => 'n1'], // missing id field
  669. ['id' => 2, 'name' => 'n1'], // has id field
  670. ],
  671. ];
  672. $v = RV::check($d2, $rs);
  673. $this->assertFalse($v->isOk());
  674. $this->assertSame('users.*.id each value must be through the "required" verify', $v->firstError());
  675. $rs = [
  676. ['users.*.id', 'required'], // will not pass.
  677. ];
  678. $v = RV::check($d1, $rs);
  679. //parameter users.*.id is required!
  680. $this->assertFalse($v->isOk());
  681. $v = RV::check($d2, $rs);
  682. //parameter users.*.id is required!
  683. $this->assertFalse($v->isOk());
  684. $d3 = [
  685. 'users' => [
  686. ['id' => 1, 'name' => 'n1'],
  687. ['id' => 2, 'name' => 'n1'],
  688. ],
  689. ];
  690. $rs[] = ['users.*.id', 'each', 'int', 'max' => 3];
  691. $v = RV::check($d3, $rs);
  692. $this->assertTrue($v->isOk());
  693. }
  694. /**
  695. * @link https://github.com/inhere/php-validate/issues/33
  696. */
  697. public function testIssues33(): void
  698. {
  699. $d = [
  700. 'users' => [
  701. ['id' => 34, 'name' => 'tom'],
  702. ['id' => 89],
  703. ],
  704. ];
  705. $rs1 = [
  706. ['users.*.name', 'each', 'required'],
  707. ['users.*.name', 'each', 'string']
  708. ];
  709. $v2 = RuleValidation::check($d, $rs1);
  710. $this->assertTrue($v2->isFail());
  711. $rs2[] = ['users.*.name', 'each', 'string'];
  712. $v1 = RuleValidation::check($d, $rs2);
  713. $this->assertFalse($v1->isFail());
  714. $this->assertTrue($v1->isOk());
  715. }
  716. }