Skip to content

Commit 19ba012

Browse files
committed
Add slug rule
1 parent b7e1d5a commit 19ba012

File tree

8 files changed

+70
-0
lines changed

8 files changed

+70
-0
lines changed

guide/index.rst

+10
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ The available rules are:
252252
- `optional`_
253253
- `regex`_
254254
- `required`_
255+
- `slug`_
255256
- `specialChar`_
256257
- `string`_
257258
- `timezone`_
@@ -775,6 +776,15 @@ The field is required.
775776
776777
required
777778
779+
slug
780+
####
781+
782+
The field requires a valid slug.
783+
784+
.. code-block::
785+
786+
slug
787+
778788
specialChar
779789
###########
780790

src/Languages/en/validation.php

+1
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,5 @@
5959
'int' => 'The {field} field requires an integer.',
6060
'object' => 'The {field} field requires an object.',
6161
'string' => 'The {field} field requires a string.',
62+
'slug' => 'The {field} field requires a valid slug.',
6263
];

src/Languages/es/validation.php

+1
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,5 @@
5959
'int' => 'El campo {field} requiere un número entero.',
6060
'object' => 'El campo {field} requiere un objeto.',
6161
'string' => 'El campo {field} requiere un texto.',
62+
'slug' => 'El campo {field} requiere una slug válida.',
6263
];

src/Languages/pt-br/validation.php

+1
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,5 @@
5959
'int' => 'O campo {field} requer um número inteiro.',
6060
'object' => 'O campo {field} requer um objeto.',
6161
'string' => 'O campo {field} requer uma string.',
62+
'slug' => 'O campo {field} requer uma slug válida.',
6263
];

src/Traits/Validator.php

+13
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,19 @@ public function string() : static
486486
return $this;
487487
}
488488

489+
/**
490+
* Validates slug.
491+
*
492+
* @since 2.6
493+
*
494+
* @return static
495+
*/
496+
public function slug() : static
497+
{
498+
$this->rules[] = 'slug';
499+
return $this;
500+
}
501+
489502
/**
490503
* Validates special characters.
491504
*

src/Validator.php

+16
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,22 @@ public static function string(string $field, array $data) : bool
688688
return \is_string(ArraySimple::value($field, $data));
689689
}
690690

691+
/**
692+
* Validates slug.
693+
*
694+
* @param string $field
695+
* @param array<string,mixed> $data
696+
*
697+
* @since 2.6
698+
*
699+
* @return bool
700+
*/
701+
public static function slug(string $field, array $data) : bool
702+
{
703+
$data = static::getData($field, $data);
704+
return $data !== null && \preg_match('/^([a-z0-9_-]+)$/', $data) === 1;
705+
}
706+
691707
/**
692708
* Validates special characters.
693709
*

tests/RulesTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,11 @@ public function testRuleString() : void
351351
self::assertRule('string', $this->rules->string());
352352
}
353353

354+
public function testRuleSlug() : void
355+
{
356+
self::assertRule('slug', $this->rules->slug());
357+
}
358+
354359
protected static function assertRule(string $rule, Rules $rules) : void
355360
{
356361
self::assertSame($rule, (string) $rules);

tests/ValidatorTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,29 @@ public function testString() : void
372372
self::assertTrue(Validator::string('string', $this->array));
373373
}
374374

375+
public function testSlug() : void
376+
{
377+
$data = [
378+
'a' => 'abc-123',
379+
'b' => '123-abc',
380+
'c' => '--__',
381+
'd' => ' ',
382+
'e' => 'coração',
383+
'f' => 'foo@bar',
384+
'g' => 'a 1',
385+
'h' => '',
386+
];
387+
self::assertTrue(Validator::slug('a', $data));
388+
self::assertTrue(Validator::slug('b', $data));
389+
self::assertTrue(Validator::slug('c', $data));
390+
self::assertFalse(Validator::slug('d', $data));
391+
self::assertFalse(Validator::slug('e', $data));
392+
self::assertFalse(Validator::slug('f', $data));
393+
self::assertFalse(Validator::slug('g', $data));
394+
self::assertFalse(Validator::slug('h', $data));
395+
self::assertFalse(Validator::slug('undefined', $data));
396+
}
397+
375398
public function testSpecialChar() : void
376399
{
377400
$data = [

0 commit comments

Comments
 (0)