瀏覽代碼

Support more validation rules

shalvah 4 年之前
父節點
當前提交
fb0acf343b
共有 2 個文件被更改,包括 119 次插入2 次删除
  1. 42 2
      src/Extracting/ParsesValidationRules.php
  2. 77 0
      tests/Unit/ValidationRuleParsingTest.php

+ 42 - 2
src/Extracting/ParsesValidationRules.php

@@ -167,6 +167,24 @@ trait ParsesValidationRules
             /**
              * Special string types
              */
+            case 'alpha':
+                $parameterData['description'] .= "The value must contain only letters. ";
+                $parameterData['setter'] = function () {
+                    return $this->getFaker()->lexify('??????');
+                };
+                break;
+            case 'alpha_dash':
+                $parameterData['description'] .= "The value must contain only letters, numbers, dashes and underscores. ";
+                $parameterData['setter'] = function () {
+                    return $this->getFaker()->lexify('???-???_?');
+                };
+                break;
+            case 'alpha_num':
+                $parameterData['description'] .= "The value must contain only letters and numbers. ";
+                $parameterData['setter'] = function () {
+                    return $this->getFaker()->bothify('#?#???#');
+                };
+                break;
             case 'timezone':
                 // Laravel's message merely says "The value must be a valid zone"
                 $parameterData['description'] .= "The value must be a valid time zone, such as <code>Africa/Accra</code>. ";
@@ -218,10 +236,33 @@ trait ParsesValidationRules
                     return date($arguments[0], time());
                 };
                 break;
+            case 'starts_with':
+                $parameterData['description'] .= 'The value must start with one of ' . w::getListOfValuesAsFriendlyHtmlString($arguments).' ';
+                $parameterData['setter'] = fn() => $this->getFaker()->lexify("{$arguments[0]}????");;
+                break;
+            case 'ends_with':
+                $parameterData['description'] .= 'The value must end with one of ' . w::getListOfValuesAsFriendlyHtmlString($arguments).' ';
+                $parameterData['setter'] = fn() => $this->getFaker()->lexify("????{$arguments[0]}");;
+                break;
+            case 'uuid':
+                $parameterData['description'] .= $this->getDescription($rule).' ';
+                $parameterData['setter'] = fn() => $this->getFaker()->uuid();;
+                break;
 
             /**
              * Special number types. Some rules here may apply to other types, but we treat them as being numeric.
              */
+            case 'digits':
+                $parameterData['description'] .= $this->getDescription($rule, [':digits' => $arguments[0]]) . ' ';
+                $parameterData['setter'] = fn() => $this->getFaker()->randomNumber($arguments[0], true);
+                $parameterData['type'] = 'number';
+                break;
+
+            case 'digits_between':
+                $parameterData['description'] .= $this->getDescription($rule, [':min' => $arguments[0], ':max' => $arguments[1]]) . ' ';
+                $parameterData['setter'] = fn() => $this->getFaker()->randomNumber($this->getFaker()->numberBetween(...$arguments), true);
+                $parameterData['type'] = 'number';
+                break;
             /*
              * min, max and between not supported until we can figure out a proper way
              *  to make them compatible with multiple types (string, number, file)
@@ -258,8 +299,7 @@ trait ParsesValidationRules
              */
             case 'in':
                 // Not using the rule description here because it only says "The attribute is invalid"
-                $description = 'The value must be one of ' . w::getListOfValuesAsFriendlyHtmlString($arguments);
-                $parameterData['description'] .= $description . ' ';
+                $parameterData['description'] .= 'The value must be one of ' . w::getListOfValuesAsFriendlyHtmlString($arguments) . ' ';
                 $parameterData['setter'] = function () use ($arguments) {
                     return Arr::random($arguments);
                 };

+ 77 - 0
tests/Unit/ValidationRuleParsingTest.php

@@ -2,6 +2,8 @@
 
 namespace Knuckles\Scribe\Tests\Unit;
 
+use Illuminate\Support\Facades\Validator;
+use Illuminate\Validation\ValidationException;
 use Knuckles\Scribe\Extracting\ParsesValidationRules;
 use Knuckles\Scribe\Tests\BaseLaravelTest;
 use Knuckles\Scribe\Tools\DocumentationConfig;
@@ -34,9 +36,20 @@ class ValidationRuleParsingTest extends BaseLaravelTest
         $results = $this->strategy->parse($ruleset, $customInfo);
 
         $parameterName = array_keys($ruleset)[0];
+        ray($parameterName, $results[$parameterName]);
 
         $this->assertEquals($expected['type'], $results[$parameterName]['type']);
         $this->assertStringEndsWith($expected['description'], $results[$parameterName]['description']);
+
+        // Validate that the generated values actually pass validation
+        $validator = Validator::make([$parameterName => $results[$parameterName]['example']], $ruleset);
+        try {
+            $validator->validate();
+        } catch (ValidationException $e) {
+            dump('Value: ', $results[$parameterName]['example']);
+            dump($e->errors());
+            throw $e;
+        }
     }
 
 
@@ -198,5 +211,69 @@ class ValidationRuleParsingTest extends BaseLaravelTest
                 'type' => 'string',
             ],
         ];
+        yield 'digits' => [
+            ['digits_param' => 'digits:8'],
+            [],
+            [
+                'description' => "The value must be 8 digits.",
+                'type' => 'number',
+            ],
+        ];
+        yield 'digits_between' => [
+            ['digits_between_param' => 'digits_between:2,8'],
+            [],
+            [
+                'description' => "The value must be between 2 and 8 digits.",
+                'type' => 'number',
+            ],
+        ];
+        yield 'alpha' => [
+            ['alpha_param' => 'alpha'],
+            [],
+            [
+                'description' => "The value must contain only letters.",
+                'type' => 'string',
+            ],
+        ];
+        yield 'alpha_dash' => [
+            ['alpha_dash_param' => 'alpha_dash'],
+            [],
+            [
+                'description' => "The value must contain only letters, numbers, dashes and underscores.",
+                'type' => 'string',
+            ],
+        ];
+        yield 'alpha_num' => [
+            ['alpha_num_param' => 'alpha_num'],
+            [],
+            [
+                'description' => "The value must contain only letters and numbers.",
+                'type' => 'string',
+            ],
+        ];
+        yield 'ends_with' => [
+            ['ends_with_param' => 'ends_with:go,ha'],
+            [],
+            [
+                'description' => "The value must end with one of <code>go</code> or <code>ha</code>.",
+                'type' => 'string',
+            ],
+        ];
+        yield 'starts_with' => [
+            ['starts_with_param' => 'starts_with:go,ha'],
+            [],
+            [
+                'description' => "The value must start with one of <code>go</code> or <code>ha</code>.",
+                'type' => 'string',
+            ],
+        ];
+        yield 'uuid' => [
+            ['uuid_param' => 'uuid'],
+            [],
+            [
+                'description' => "The value must be a valid UUID.",
+                'type' => 'string',
+            ],
+        ];
     }
 }