Skip to content

Commit

Permalink
Add slug rule
Browse files Browse the repository at this point in the history
  • Loading branch information
natanfelles committed Oct 15, 2023
1 parent b7e1d5a commit 19ba012
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 0 deletions.
10 changes: 10 additions & 0 deletions guide/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ The available rules are:
- `optional`_
- `regex`_
- `required`_
- `slug`_
- `specialChar`_
- `string`_
- `timezone`_
Expand Down Expand Up @@ -775,6 +776,15 @@ The field is required.
required
slug
####

The field requires a valid slug.

.. code-block::
slug
specialChar
###########

Expand Down
1 change: 1 addition & 0 deletions src/Languages/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@
'int' => 'The {field} field requires an integer.',
'object' => 'The {field} field requires an object.',
'string' => 'The {field} field requires a string.',
'slug' => 'The {field} field requires a valid slug.',
];
1 change: 1 addition & 0 deletions src/Languages/es/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@
'int' => 'El campo {field} requiere un número entero.',
'object' => 'El campo {field} requiere un objeto.',
'string' => 'El campo {field} requiere un texto.',
'slug' => 'El campo {field} requiere una slug válida.',
];
1 change: 1 addition & 0 deletions src/Languages/pt-br/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@
'int' => 'O campo {field} requer um número inteiro.',
'object' => 'O campo {field} requer um objeto.',
'string' => 'O campo {field} requer uma string.',
'slug' => 'O campo {field} requer uma slug válida.',
];
13 changes: 13 additions & 0 deletions src/Traits/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,19 @@ public function string() : static
return $this;
}

/**
* Validates slug.
*
* @since 2.6
*
* @return static
*/
public function slug() : static
{
$this->rules[] = 'slug';
return $this;
}

/**
* Validates special characters.
*
Expand Down
16 changes: 16 additions & 0 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,22 @@ public static function string(string $field, array $data) : bool
return \is_string(ArraySimple::value($field, $data));
}

/**
* Validates slug.
*
* @param string $field
* @param array<string,mixed> $data
*
* @since 2.6
*
* @return bool
*/
public static function slug(string $field, array $data) : bool
{
$data = static::getData($field, $data);
return $data !== null && \preg_match('/^([a-z0-9_-]+)$/', $data) === 1;
}

/**
* Validates special characters.
*
Expand Down
5 changes: 5 additions & 0 deletions tests/RulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,11 @@ public function testRuleString() : void
self::assertRule('string', $this->rules->string());
}

public function testRuleSlug() : void
{
self::assertRule('slug', $this->rules->slug());
}

protected static function assertRule(string $rule, Rules $rules) : void
{
self::assertSame($rule, (string) $rules);
Expand Down
23 changes: 23 additions & 0 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,29 @@ public function testString() : void
self::assertTrue(Validator::string('string', $this->array));
}

public function testSlug() : void
{
$data = [
'a' => 'abc-123',
'b' => '123-abc',
'c' => '--__',
'd' => ' ',
'e' => 'coração',
'f' => 'foo@bar',
'g' => 'a 1',
'h' => '',
];
self::assertTrue(Validator::slug('a', $data));
self::assertTrue(Validator::slug('b', $data));
self::assertTrue(Validator::slug('c', $data));
self::assertFalse(Validator::slug('d', $data));
self::assertFalse(Validator::slug('e', $data));
self::assertFalse(Validator::slug('f', $data));
self::assertFalse(Validator::slug('g', $data));
self::assertFalse(Validator::slug('h', $data));
self::assertFalse(Validator::slug('undefined', $data));
}

public function testSpecialChar() : void
{
$data = [
Expand Down

0 comments on commit 19ba012

Please sign in to comment.