Skip to content

Commit

Permalink
Merge 651a8da into e53b185
Browse files Browse the repository at this point in the history
  • Loading branch information
iamthewit authored Oct 1, 2019
2 parents e53b185 + 651a8da commit ddde873
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/TypeValidator/StringValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public function validate(array $constraints, $data, $prettyName = 'This value')
$this->validateRegex($data, $constraints['regex'], $prettyName);
}

if (isset($constraints['email']) && $constraints['email'] === true) {
$this->validateEmailAddress($data);
}

parent::validate($constraints, $data, $prettyName);
}

Expand Down Expand Up @@ -132,4 +136,22 @@ public function validateRegex($data, $regex, $prettyName = 'This value')
}
return true;
}

/**
* @param string $emailAddress
*
* @return bool
* @throws DataValidationException
*/
private function validateEmailAddress(string $emailAddress): bool
{
if (filter_var($emailAddress, FILTER_VALIDATE_EMAIL)) {
return true;
}

throw new DataValidationException(
sprintf('%s is not a valid email address.', $emailAddress)
);
}

}
17 changes: 17 additions & 0 deletions tests/phpunit/src/Unit/TypeValidator/StringValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,21 @@ public function validateRegexSucceeds()

$typeValidator->validateRegex($data, $regex);
}

public function testValidateStringWithEmailConstraintSucceeds()
{
$stringValidator = new StringValidator();
$stringValidator->validate(['email'], 'user@host.tld');
}

/**
* @expectedException \Mooti\Validator\Exception\DataValidationException
* @expectedExceptionMessage invalidEmail is not a valid email address.
*/
public function testValidateEmailAddressThrowsInvalidRuleException()
{
$stringValidator = new StringValidator();
$stringValidator->validate(['email' => true], 'invalidEmail');
}

}

0 comments on commit ddde873

Please sign in to comment.