ValidationRuleParsingTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Unit;
  3. use Illuminate\Foundation\Application;
  4. use Illuminate\Support\Facades\Schema;
  5. use Illuminate\Support\Facades\Validator;
  6. use Illuminate\Translation\Translator;
  7. use Illuminate\Validation\Rule;
  8. use Illuminate\Validation\ValidationException;
  9. use Knuckles\Scribe\Extracting\ParsesValidationRules;
  10. use Knuckles\Scribe\Tests\BaseLaravelTest;
  11. use Knuckles\Scribe\Tools\DocumentationConfig;
  12. use Knuckles\Scribe\Tests\Fixtures;
  13. $invokableRulesSupported = interface_exists(\Illuminate\Contracts\Validation\InvokableRule::class);
  14. $laravel10Rules = version_compare(Application::VERSION, '10.0', '>=');
  15. class ValidationRuleParsingTest extends BaseLaravelTest
  16. {
  17. private $strategy;
  18. public function __construct(?string $name = null, array $data = [], $dataName = '')
  19. {
  20. parent::__construct($name, $data, $dataName);
  21. $this->strategy = new class {
  22. use ParsesValidationRules;
  23. public function parse($validationRules, $customParameterData = []): array
  24. {
  25. $this->config = new DocumentationConfig([]);
  26. $bodyParametersFromValidationRules = $this->getParametersFromValidationRules($validationRules, $customParameterData);
  27. return $this->normaliseArrayAndObjectParameters($bodyParametersFromValidationRules);
  28. }
  29. };
  30. }
  31. /**
  32. * @test
  33. * @dataProvider supportedRules
  34. */
  35. public function can_parse_supported_rules(array $ruleset, array $customInfo, array $expected)
  36. {
  37. // Needed for `exists` rule
  38. Schema::create('users', function ($table) {
  39. $table->id();
  40. });
  41. $results = $this->strategy->parse($ruleset, $customInfo);
  42. $parameterName = array_keys($ruleset)[0];
  43. $this->assertEquals($expected['description'], $results[$parameterName]['description']);
  44. if (isset($expected['type'])) {
  45. $this->assertEquals($expected['type'], $results[$parameterName]['type']);
  46. }
  47. // Validate that the generated values actually pass validation (for rules where we can generate some data)
  48. if (is_string($ruleset[$parameterName]) && str_contains($ruleset[$parameterName], "exists")) return;
  49. $exampleData = [$parameterName => $results[$parameterName]['example']];
  50. $validator = Validator::make($exampleData, $ruleset);
  51. try {
  52. $validator->validate();
  53. } catch (ValidationException $e) {
  54. dump('Rules: ', $ruleset);
  55. dump('Generated value: ', $exampleData[$parameterName]);
  56. dump($e->errors());
  57. $this->fail("Generated example data from validation rule failed to match actual.");
  58. }
  59. }
  60. /** @test */
  61. public function can_parse_rule_objects()
  62. {
  63. $results = $this->strategy->parse([
  64. 'in_param' => ['numeric', Rule::in([3,5,6])]
  65. ]);
  66. $this->assertEquals(
  67. [3, 5, 6],
  68. $results['in_param']['enumValues']
  69. );
  70. }
  71. /** @test */
  72. public function can_transform_arrays_and_objects()
  73. {
  74. $ruleset = [
  75. 'array_param' => 'array|required',
  76. 'array_param.*' => 'string',
  77. ];
  78. $results = $this->strategy->parse($ruleset);
  79. $this->assertCount(1, $results);
  80. $this->assertEquals('string[]', $results['array_param']['type']);
  81. $ruleset = [
  82. 'object_param' => 'array|required',
  83. 'object_param.field1.*' => 'string',
  84. 'object_param.field2' => 'integer|required',
  85. ];
  86. $results = $this->strategy->parse($ruleset);
  87. $this->assertCount(3, $results);
  88. $this->assertEquals('object', $results['object_param']['type']);
  89. $this->assertEquals('string[]', $results['object_param.field1']['type']);
  90. $this->assertEquals('integer', $results['object_param.field2']['type']);
  91. $ruleset = [
  92. 'array_of_objects_with_array.*.another.*.one.field1.*' => 'string|required',
  93. 'array_of_objects_with_array.*.another.*.one.field2' => 'integer',
  94. 'array_of_objects_with_array.*.another.*.two.field2' => 'numeric',
  95. ];
  96. $results = $this->strategy->parse($ruleset);
  97. $this->assertCount(7, $results);
  98. $this->assertEquals('object[]', $results['array_of_objects_with_array']['type']);
  99. $this->assertEquals('object[]', $results['array_of_objects_with_array[].another']['type']);
  100. $this->assertEquals('object', $results['array_of_objects_with_array[].another[].one']['type']);
  101. $this->assertEquals('object', $results['array_of_objects_with_array[].another[].two']['type']);
  102. $this->assertEquals('string[]', $results['array_of_objects_with_array[].another[].one.field1']['type']);
  103. $this->assertEquals('integer', $results['array_of_objects_with_array[].another[].one.field2']['type']);
  104. $this->assertEquals('number', $results['array_of_objects_with_array[].another[].two.field2']['type']);
  105. $ruleset = [
  106. '*.foo' => 'required|array',
  107. '*.foo.*' => 'required|array',
  108. '*.foo.*.bar' => 'required',
  109. ];
  110. $results = $this->strategy->parse($ruleset);
  111. $this->assertCount(3, $results);
  112. $this->assertEquals('object', $results['*']['type']);
  113. $this->assertEquals('object[]', $results['*.foo']['type']);
  114. $this->assertEquals('string', $results['*.foo[].bar']['type']);
  115. }
  116. public static function supportedRules()
  117. {
  118. $description = 'A description';
  119. // Key is just an identifier
  120. // First array in each key is the validation ruleset,
  121. // Second is custom information (from bodyParameters() or comments)
  122. // Third is expected result
  123. yield 'string' => [
  124. ['string_param' => 'string'],
  125. ['string_param' => ['description' => $description]],
  126. [
  127. 'type' => 'string',
  128. 'description' => $description . ".",
  129. ],
  130. ];
  131. yield 'boolean' => [
  132. ['boolean_param' => 'boolean'],
  133. [],
  134. [
  135. 'type' => 'boolean',
  136. 'description' => "",
  137. ],
  138. ];
  139. yield 'integer' => [
  140. ['integer_param' => 'integer'],
  141. [],
  142. [
  143. 'type' => 'integer',
  144. 'description' => "",
  145. ],
  146. ];
  147. yield 'numeric' => [
  148. ['numeric_param' => 'numeric'],
  149. ['numeric_param' => ['description' => $description]],
  150. [
  151. 'type' => 'number',
  152. 'description' => $description . ".",
  153. ],
  154. ];
  155. yield 'file' => [
  156. ['file_param' => 'file|required'],
  157. ['file_param' => ['description' => $description]],
  158. [
  159. 'description' => "$description. Must be a file.",
  160. 'type' => 'file',
  161. ],
  162. ];
  163. yield 'image' => [
  164. ['image_param' => 'image|required'],
  165. [],
  166. [
  167. 'description' => "Must be an image.",
  168. 'type' => 'file',
  169. ],
  170. ];
  171. yield 'timezone' => [
  172. ['timezone_param' => 'timezone|required'],
  173. [],
  174. [
  175. 'description' => 'Must be a valid time zone, such as <code>Africa/Accra</code>.',
  176. 'type' => 'string',
  177. ],
  178. ];
  179. yield 'email' => [
  180. ['email_param' => 'email|required'],
  181. [],
  182. [
  183. 'description' => 'Must be a valid email address.',
  184. 'type' => 'string',
  185. ],
  186. ];
  187. yield 'url' => [
  188. ['url_param' => 'url|required'],
  189. ['url_param' => ['description' => $description]],
  190. [
  191. 'description' => "$description. Must be a valid URL.",
  192. 'type' => 'string',
  193. ],
  194. ];
  195. yield 'ip' => [
  196. ['ip_param' => 'ip|required'],
  197. ['ip_param' => ['description' => $description]],
  198. [
  199. 'description' => "$description. Must be a valid IP address.",
  200. 'type' => 'string',
  201. ],
  202. ];
  203. yield 'json' => [
  204. ['json_param' => 'json|required'],
  205. ['json_param' => []],
  206. [
  207. 'description' => 'Must be a valid JSON string.',
  208. 'type' => 'string',
  209. ],
  210. ];
  211. yield 'date' => [
  212. ['date_param' => 'date|required'],
  213. [],
  214. [
  215. 'description' => 'Must be a valid date.',
  216. 'type' => 'string',
  217. ],
  218. ];
  219. yield 'date_format' => [
  220. ['date_format_param' => 'date_format:Y-m-d|required'],
  221. ['date_format_param' => ['description' => $description]],
  222. [
  223. 'description' => "$description. Must be a valid date in the format <code>Y-m-d</code>.",
  224. 'type' => 'string',
  225. ],
  226. ];
  227. yield 'in' => [
  228. ['in_param' => 'in:3,5,6'],
  229. ['in_param' => ['description' => $description]],
  230. [
  231. 'description' => $description.".",
  232. 'type' => 'string',
  233. 'enumValues' => [3,5,6]
  234. ],
  235. ];
  236. yield 'not_in' => [
  237. ['not_param' => 'not_in:3,5,6'],
  238. [],
  239. [
  240. 'description' => "Must not be one of <code>3</code>, <code>5</code>, or <code>6</code>.",
  241. ],
  242. ];
  243. yield 'digits' => [
  244. ['digits_param' => 'digits:8'],
  245. [],
  246. [
  247. 'description' => "Must be 8 digits.",
  248. 'type' => 'string',
  249. ],
  250. ];
  251. yield 'digits_between' => [
  252. ['digits_between_param' => 'digits_between:2,8'],
  253. [],
  254. [
  255. 'description' => "Must be between 2 and 8 digits.",
  256. 'type' => 'string',
  257. ],
  258. ];
  259. yield 'alpha' => [
  260. ['alpha_param' => 'alpha'],
  261. [],
  262. [
  263. 'description' => "Must contain only letters.",
  264. 'type' => 'string',
  265. ],
  266. ];
  267. yield 'alpha_dash' => [
  268. ['alpha_dash_param' => 'alpha_dash'],
  269. [],
  270. [
  271. 'description' => "Must contain only letters, numbers, dashes and underscores.",
  272. 'type' => 'string',
  273. ],
  274. ];
  275. yield 'alpha_num' => [
  276. ['alpha_num_param' => 'alpha_num'],
  277. [],
  278. [
  279. 'description' => "Must contain only letters and numbers.",
  280. 'type' => 'string',
  281. ],
  282. ];
  283. yield 'ends_with' => [
  284. ['ends_with_param' => 'ends_with:go,ha'],
  285. [],
  286. [
  287. 'description' => "Must end with one of <code>go</code> or <code>ha</code>.",
  288. 'type' => 'string',
  289. ],
  290. ];
  291. yield 'starts_with' => [
  292. ['starts_with_param' => 'starts_with:go,ha'],
  293. [],
  294. [
  295. 'description' => "Must start with one of <code>go</code> or <code>ha</code>.",
  296. 'type' => 'string',
  297. ],
  298. ];
  299. yield 'uuid' => [
  300. ['uuid_param' => 'uuid'],
  301. [],
  302. [
  303. 'description' => "Must be a valid UUID.",
  304. 'type' => 'string',
  305. ],
  306. ];
  307. yield 'required_if' => [
  308. ['required_if_param' => 'required_if:another_field,a_value'],
  309. [],
  310. ['description' => "This field is required when <code>another_field</code> is <code>a_value</code>."],
  311. ];
  312. yield 'required_unless' => [
  313. ['required_unless_param' => 'string|required_unless:another_field,a_value'],
  314. [],
  315. ['description' => "This field is required unless <code>another_field</code> is in <code>a_value</code>."],
  316. ];
  317. yield 'required_with' => [
  318. ['required_with_param' => 'required_with:another_field,some_other_field'],
  319. [],
  320. ['description' => 'This field is required when <code>another_field</code> or <code>some_other_field</code> is present.'],
  321. ];
  322. yield 'required_with_all' => [
  323. ['required_with_all_param' => 'required_with_all:another_field,some_other_field'],
  324. [],
  325. ['description' => 'This field is required when <code>another_field</code> and <code>some_other_field</code> are present.'],
  326. ];
  327. yield 'required_without' => [
  328. ['required_without_param' => 'string|required_without:another_field,some_other_field'],
  329. [],
  330. ['description' => 'This field is required when <code>another_field</code> or <code>some_other_field</code> is not present.'],
  331. ];
  332. yield 'required_without_all' => [
  333. ['required_without_all_param' => 'string|required_without_all:another_field,some_other_field'],
  334. [],
  335. ['description' => 'This field is required when none of <code>another_field</code> and <code>some_other_field</code> are present.'],
  336. ];
  337. yield 'same' => [
  338. ['same_param' => 'same:other_field'],
  339. [],
  340. ['description' => "The value and <code>other_field</code> must match."],
  341. ];
  342. yield 'different' => [
  343. ['different_param' => 'string|different:other_field'],
  344. [],
  345. ['description' => "The value and <code>other_field</code> must be different."],
  346. ];
  347. yield 'after' => [
  348. ['after_param' => 'after:2020-02-12'],
  349. [],
  350. ['description' => "Must be a date after <code>2020-02-12</code>."],
  351. ];
  352. yield 'before_or_equal' => [
  353. ['before_or_equal_param' => 'before_or_equal:2020-02-12'],
  354. [],
  355. ['description' => "Must be a date before or equal to <code>2020-02-12</code>."],
  356. ];
  357. yield 'size (number)' => [
  358. ['size_param' => 'numeric|size:6'],
  359. [],
  360. ['description' => "Must be 6."],
  361. ];
  362. yield 'size (string)' => [
  363. ['size_param' => 'string|size:6'],
  364. [],
  365. ['description' => "Must be 6 characters."],
  366. ];
  367. yield 'size (file)' => [
  368. ['size_param' => 'file|size:6'],
  369. [],
  370. ['description' => "Must be a file. Must be 6 kilobytes."],
  371. ];
  372. yield 'max (number)' => [
  373. ['max_param' => 'numeric|max:6'],
  374. [],
  375. ['description' => "Must not be greater than 6."],
  376. ];
  377. yield 'max (string)' => [
  378. ['max_param' => 'string|max:6'],
  379. [],
  380. ['description' => "Must not be greater than 6 characters."],
  381. ];
  382. yield 'max (file)' => [
  383. ['max_param' => 'file|max:6'],
  384. [],
  385. ['description' => "Must be a file. Must not be greater than 6 kilobytes."],
  386. ];
  387. yield 'min (number)' => [
  388. ['min_param' => 'numeric|min:6'],
  389. [],
  390. ['description' => "Must be at least 6."],
  391. ];
  392. yield 'min (string)' => [
  393. ['min_param' => 'string|min:6'],
  394. [],
  395. ['description' => "Must be at least 6 characters."],
  396. ];
  397. yield 'min (file)' => [
  398. ['min_param' => 'file|min:6'],
  399. [],
  400. ['description' => "Must be a file. Must be at least 6 kilobytes."],
  401. ];
  402. yield 'between (number)' => [
  403. ['between_param' => 'numeric|between:1,2'],
  404. [],
  405. ['description' => "Must be between 1 and 2."],
  406. ];
  407. yield 'between (string)' => [
  408. ['between_param' => 'string|between:1,2'],
  409. [],
  410. ['description' => "Must be between 1 and 2 characters."],
  411. ];
  412. yield 'between (file)' => [
  413. ['between_param' => 'file|between:1,2'],
  414. [],
  415. ['description' => "Must be a file. Must be between 1 and 2 kilobytes."],
  416. ];
  417. yield 'regex' => [
  418. ['regex_param' => 'regex:/\d/'],
  419. [],
  420. ['description' => 'Must match the regex /\d/.'],
  421. ];
  422. yield 'accepted' => [
  423. ['accepted_param' => 'accepted'],
  424. [],
  425. [
  426. 'type' => 'boolean',
  427. 'description' => 'Must be accepted.',
  428. ],
  429. ];
  430. yield 'exists' => [
  431. ['exists_param' => 'exists:users,id'],
  432. [],
  433. [
  434. 'description' => 'The <code>id</code> of an existing record in the users table.',
  435. ],
  436. ];
  437. yield 'unsupported' => [
  438. ['unsupported_param' => [new DummyValidationRule, 'bail']],
  439. ['unsupported_param' => ['description' => $description]],
  440. ['description' => "$description."],
  441. ];
  442. yield 'accepted_if' => [
  443. ['accepted_if_param' => 'accepted_if:another_field,a_value'],
  444. [],
  445. [
  446. 'type' => 'boolean',
  447. 'description' => "Must be accepted when <code>another_field</code> is <code>a_value</code>.",
  448. ],
  449. ];
  450. }
  451. /** @test */
  452. public function child_does_not_overwrite_parent_status()
  453. {
  454. $ruleset = [
  455. 'array_param' => 'array|required',
  456. 'array_param.*' => 'array|required',
  457. 'array_param.*.an_item' => 'string|required',
  458. ];
  459. $results = $this->strategy->parse($ruleset);
  460. $this->assertCount(2, $results);
  461. $this->assertEquals(true, $results['array_param']['required']);
  462. }
  463. /** @test */
  464. public function can_parse_custom_closure_rules()
  465. {
  466. // Single line DocComment
  467. $ruleset = [
  468. 'closure' => [
  469. 'bail', 'required',
  470. /** This is a single line parsed closure rule. */
  471. function ($attribute, $value, $fail) {
  472. $fail('Always fail.');
  473. },
  474. ],
  475. ];
  476. $results = $this->strategy->parse($ruleset);
  477. $this->assertEquals(
  478. 'This is a single line parsed closure rule.',
  479. $results['closure']['description']
  480. );
  481. // Block DocComment
  482. $ruleset = [
  483. 'closure' => [
  484. 'bail', 'required',
  485. /**
  486. * This is a block DocComment
  487. * parsed on a closure rule.
  488. * Extra info.
  489. */
  490. function ($attribute, $value, $fail) {
  491. $fail('Always fail.');
  492. },
  493. ],
  494. ];
  495. $results = $this->strategy->parse($ruleset);
  496. $this->assertEquals(
  497. 'This is a block DocComment parsed on a closure rule. Extra info.',
  498. $results['closure']['description']
  499. );
  500. }
  501. /** @test */
  502. public function can_parse_custom_rule_classes()
  503. {
  504. $ruleset = [
  505. 'param1' => ['bail', 'required', new DummyWithDocsValidationRule],
  506. ];
  507. global $invokableRulesSupported;
  508. if ($invokableRulesSupported) {
  509. $ruleset['param2'] = [new DummyInvokableValidationRule];
  510. }
  511. global $laravel10Rules;
  512. if ($laravel10Rules) {
  513. $ruleset['param3'] = [new DummyL10ValidationRule];
  514. }
  515. $results = $this->strategy->parse($ruleset);
  516. $this->assertEquals(true, $results['param1']['required']);
  517. $this->assertEquals('This is a dummy test rule.', $results['param1']['description']);
  518. if (isset($results['param2'])) $this->assertEquals('This rule is invokable.', $results['param2']['description']);
  519. if (isset($results['param3'])) $this->assertEquals('This is a custom rule.', $results['param3']['description']);
  520. }
  521. /** @test */
  522. public function can_parse_enum_rules()
  523. {
  524. $results = $this->strategy->parse([
  525. 'enum' => [
  526. 'required',
  527. Rule::enum(Fixtures\TestStringBackedEnum::class)
  528. ],
  529. ]);
  530. $this->assertEquals('string', $results['enum']['type']);
  531. $this->assertEquals(
  532. ['red', 'green', 'blue'],
  533. $results['enum']['enumValues']
  534. );
  535. $this->assertTrue(in_array(
  536. $results['enum']['example'],
  537. array_map(fn ($case) => $case->value, Fixtures\TestStringBackedEnum::cases())
  538. ));
  539. $results = $this->strategy->parse([
  540. 'enum' => [
  541. 'required',
  542. new \Illuminate\Validation\Rules\Enum(Fixtures\TestIntegerBackedEnum::class),
  543. // Not supported in Laravel 8
  544. // Rule::enum(Fixtures\TestIntegerBackedEnum::class)
  545. ],
  546. ]);
  547. $this->assertEquals('integer', $results['enum']['type']);
  548. $this->assertEquals(
  549. [1, 2, 3],
  550. $results['enum']['enumValues']
  551. );
  552. $this->assertTrue(in_array(
  553. $results['enum']['example'],
  554. array_map(fn ($case) => $case->value, Fixtures\TestIntegerBackedEnum::cases())
  555. ));
  556. $results = $this->strategy->parse([
  557. 'enum' => [
  558. 'required',
  559. new \Illuminate\Validation\Rules\Enum(Fixtures\TestStringBackedEnum::class),
  560. // Not supported in Laravel 8
  561. // Rule::enum(Fixtures\TestStringBackedEnum::class),
  562. ],
  563. ], [
  564. 'enum' => ['description' => 'A description'],
  565. ]);
  566. $this->assertEquals('string', $results['enum']['type']);
  567. $this->assertEquals(
  568. 'A description.',
  569. $results['enum']['description']
  570. );
  571. $this->assertTrue(in_array(
  572. $results['enum']['example'],
  573. array_map(fn ($case) => $case->value, Fixtures\TestStringBackedEnum::cases())
  574. ));
  575. }
  576. /** @test */
  577. public function can_translate_validation_rules_with_types_with_translator_without_array_support()
  578. {
  579. // Single line DocComment
  580. $ruleset = [
  581. 'nested' => [
  582. 'string', 'max:20',
  583. ],
  584. ];
  585. $results = $this->strategy->parse($ruleset);
  586. $this->assertEquals('Must not be greater than 20 characters.', $results['nested']['description']);
  587. $this->app->extend('translator', function ($command, $app) {
  588. $loader = $app['translation.loader'];
  589. $locale = $app['config']['app.locale'];
  590. return new DummyTranslator($loader, $locale);
  591. });
  592. $results = $this->strategy->parse($ruleset);
  593. $this->assertEquals('successfully translated by concatenated string.', $results['nested']['description']);
  594. }
  595. /** @test */
  596. public function can_valid_parse_nullable_rules()
  597. {
  598. $ruleset = [
  599. 'nullable_param' => 'nullable|string',
  600. ];
  601. $results = $this->strategy->parse($ruleset);
  602. $this->assertEquals(true, $results['nullable_param']['nullable']);
  603. $ruleset = [
  604. 'nullable_param' => 'string',
  605. ];
  606. $results = $this->strategy->parse($ruleset);
  607. $this->assertEquals(false, $results['nullable_param']['nullable']);
  608. $ruleset = [
  609. 'required_param' => 'required|nullable|string',
  610. ];
  611. $results = $this->strategy->parse($ruleset);
  612. $this->assertEquals(false, $results['required_param']['nullable']);
  613. $ruleset = [
  614. 'array_param' => 'array',
  615. 'array_param.*.field' => 'nullable|string',
  616. ];
  617. $results = $this->strategy->parse($ruleset);
  618. $this->assertEquals(false, $results['array_param']['nullable']);
  619. $this->assertEquals(true, $results['array_param[].field']['nullable']);
  620. $ruleset = [
  621. 'object' => 'array',
  622. 'object.field1' => 'string',
  623. 'object.field2' => 'nullable|string',
  624. ];
  625. $results = $this->strategy->parse($ruleset);
  626. $this->assertEquals(false, $results['object']['nullable']);
  627. $this->assertEquals(false, $results['object.field1']['nullable']);
  628. $this->assertEquals(true, $results['object.field2']['nullable']);
  629. }
  630. }
  631. class DummyValidationRule implements \Illuminate\Contracts\Validation\Rule
  632. {
  633. public function passes($attribute, $value)
  634. {
  635. return true;
  636. }
  637. public function message()
  638. {
  639. return '.';
  640. }
  641. }
  642. class DummyWithDocsValidationRule implements \Illuminate\Contracts\Validation\Rule
  643. {
  644. public function passes($attribute, $value)
  645. {
  646. return true;
  647. }
  648. public function message()
  649. {
  650. return '.';
  651. }
  652. public static function docs()
  653. {
  654. return [
  655. 'description' => 'This is a dummy test rule.',
  656. 'example' => 'Default example, only added if none other give.',
  657. ];
  658. }
  659. }
  660. if ($invokableRulesSupported) {
  661. class DummyInvokableValidationRule implements \Illuminate\Contracts\Validation\InvokableRule
  662. {
  663. public function __invoke($attribute, $value, $fail)
  664. {
  665. if (strtoupper($value) !== $value) {
  666. $fail(':attribute must be uppercase.');
  667. }
  668. }
  669. public function docs()
  670. {
  671. return [
  672. 'description' => 'This rule is invokable.',
  673. ];
  674. }
  675. }
  676. }
  677. if ($laravel10Rules) {
  678. // Laravel 10 deprecated the previous Rule and InvokableRule classes for a single interface
  679. // (https://github.com/laravel/framework/pull/45954)
  680. class DummyL10ValidationRule implements \Illuminate\Contracts\Validation\ValidationRule
  681. {
  682. public function validate(string $attribute, mixed $value, \Closure $fail): void
  683. {
  684. if (strtoupper($value) !== $value) {
  685. $fail('The :attribute must be an attribute.');
  686. }
  687. }
  688. public static function docs()
  689. {
  690. return [
  691. 'description' => 'This is a custom rule.',
  692. ];
  693. }
  694. }
  695. }
  696. class DummyTranslator extends Translator
  697. {
  698. public function get($key, array $replace = [], $locale = null, $fallback = true)
  699. {
  700. if ($key === 'validation.max.string') {
  701. return 'successfully translated by concatenated string';
  702. }
  703. return $key;
  704. }
  705. }