ValidationRuleParsingTest.php 26 KB

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