Skip to content

Commit

Permalink
fix: the require rule will accept null values (BREAKING CHANGES)
Browse files Browse the repository at this point in the history
feat: add notNull rule

before this version, a field that was present but with a null value caught the attention of the 'required' rule.
Now with this fix the 'required' rule will only check if a certain key is present in the params or payload.
  • Loading branch information
lefuturiste committed May 24, 2020
1 parent dc4c5d7 commit 9fdb238
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 26 deletions.
40 changes: 34 additions & 6 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,39 @@ public function __construct($params)

/**
* Verify if many fields exists
* Note, since version 1.4 a null field is considered as present and thus pass this test.
* BREAKING CHANGE:
* For version < 1.4 A 'null' field which was, before 1.4 considered as a error is now accepted
* For version >= 1.4: a 'null' field is present, to require this field as non null use the notNull() method
*
* @param array $keys
* @return $this
*/
public function required(string ...$keys): self
{
foreach ($keys as $key) {
try {
$this->getValue($key, true);
} catch (\Exception $e) {
$this->addError($key, 'required');
}
}

return $this;
}

/**
* Verify if a present field has a non null value
*
* @param array $keys
* @return $this
*/
public function notNull(string ...$keys): self
{
foreach ($keys as $key) {
$value = $this->getValue($key);
if (is_null($value)) {
$this->addError($key, 'required');
$this->addError($key, 'notNull');
}
}

Expand Down Expand Up @@ -416,20 +439,25 @@ public function isValid(): bool
* @param string $key
* @return mixed|null
*/
public function getValue(string $key)
public function getValue(string $key, bool $throwExceptionIfNotExists = false)
{
if (empty($this->params)) {
return null;
}
// if (empty($this->params)) {
// return null;
// }
$keys = array_map(
fn($str) => substr($str, -1) === "]" ? substr($str, 0, strlen($str) - 1) : $str,
function($str) {
return substr($str, -1) === "]" ? substr($str, 0, strlen($str) - 1) : $str;
},
explode('[', $key)
);
$explored = $this->params;
foreach ($keys as $nestedKey) {
if (array_key_exists($nestedKey, $explored)) {
$explored = $explored[$nestedKey];
} else {
if ($throwExceptionIfNotExists) {
throw new \Exception("Key not found");
}
return null;
}
}
Expand Down
31 changes: 16 additions & 15 deletions src/langs/en.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
<?php
return [
'required' => 'The field %s is required',
'empty' => 'The field %s cannot be empty',
'slug' => 'The field %s is not a valid slug',
'minLength' => 'The field %s must contain more than %d characters',
'maxLength' => 'The field %s must contain less than %d characters',
'betweenLength' => 'The field %s must contain between %d and %d characters',
'datetime' => 'The field %s must be a valid date (%s)',
'notEqual' => 'The field %s must be equal to %s',
'email' => 'The field %s must be a valid email address',
'integer' => 'The field %s must be a valid number',
'required' => 'The field %s is required',
'empty' => 'The field %s cannot be empty',
'slug' => 'The field %s is not a valid slug',
'minLength' => 'The field %s must contain more than %d characters',
'maxLength' => 'The field %s must contain less than %d characters',
'betweenLength' => 'The field %s must contain between %d and %d characters',
'datetime' => 'The field %s must be a valid date (%s)',
'notEqual' => 'The field %s must be equal to %s',
'email' => 'The field %s must be a valid email address',
'integer' => 'The field %s must be a valid number',
'float' => 'The field %s must be a valid float',
'url' => 'The field %s must be a valid URL',
'match' => 'The field %s must be equal to %s',
'between' => "The field %s must be between %d and %d",
'betweenStrict' => "The field %s must be strict between %d and %d",
'url' => 'The field %s must be a valid URL',
'match' => 'The field %s must be equal to %s',
'between' => "The field %s must be between %d and %d",
'betweenStrict' => "The field %s must be strict between %d and %d",
'array' => "Thr field %s must be an array",
'boolean' => "The field %s must be a boolean",
'patternMatch' => "The field %s must follow the pattern",
'alphaNumerical' => "The field %s must be composed of alpha numerical characters"
'alphaNumerical' => "The field %s must be composed of alpha numerical characters",
'notNull' => 'The field%s cannot be null'
];
11 changes: 6 additions & 5 deletions src/langs/fr.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
'notEqual' => "Le champs %s doit être égal au champs %s",
'email' => "Le champs %s doit être un email valide",
'integer' => "Le champs %s doit être un nombre valide",
'float' => 'Le champs %s doit être un nombre à virgule valide',
'float' => 'Le champs %s doit être un nombre à virgule valide',
'url' => "Le champs %s doit être une url valide",
'match' => "Le champs %s doit être égal à %s",
'between' => "Le champs %s doit être compris entre %d et %d",
'betweenStrict' => "Le champs %s doit être compris entre %d and %d",
'array' => "Le champs %s doit être un tableau",
'boolean' => "Le champs %s doit être un boolean",
'patternMatch' => "Le champs %s doit respecter le pattern",
'alphaNumerical' => "Le champs %s doit être composé de caractères alphanumériques"
'array' => "Le champs %s doit être un tableau",
'boolean' => "Le champs %s doit être un boolean",
'patternMatch' => "Le champs %s doit respecter le pattern",
'alphaNumerical' => "Le champs %s doit être composé de caractères alphanumériques",
'notNull' => "Le champs %s ne peut être null"
];
18 changes: 18 additions & 0 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ public function testRequiredIfFail()
$this->assertEquals(['Le champs content est requis'], $errors);
}

public function testRequiredWithNullField()
{
$errors = $this->makeValidator(['name' => null])
->required('name', 'content')
->getErrors();
$this->assertCount(1, $errors);
$this->assertEquals(['Le champs content est requis'], $errors);
}

public function testNotNull()
{
$errors = $this->makeValidator(['name' => null, 'content' => 'dsqdssd'])
->notNull('name', 'content')
->getErrors();
$this->assertCount(1, $errors);
$this->assertEquals(['Le champs name ne peut être null'], $errors);
}

public function testNotEmpty()
{
$errors = $this->makeValidator(['name' => 'joe', 'content' => ''])
Expand Down

0 comments on commit 9fdb238

Please sign in to comment.