ValidationRuleParsingTest.php 24 KB

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