Skip to content

Commit

Permalink
Merge 4c709ae into 9c6b332
Browse files Browse the repository at this point in the history
  • Loading branch information
lalobo committed Dec 13, 2017
2 parents 9c6b332 + 4c709ae commit 79db2cc
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,10 @@ Each type also has additional properties.
```
The string type validates the item as a string. it also has the following properties:
* **constraints** [*array*] : Exta constraints are:
* **length** [*array*] : the minimum and maximum length of the string as a numeric array in the format [min, max]. If you don't want to set a value set it to null. So [1,null] will be a string with a minimum of one character but no maximum set.
* **enum** [*array*] : the possible values of the string. so [FOO, BAR] means that the string can only be one of those values and nothing else
* **length** [*array*] : the minimum and maximum length of the string as a numeric array in the format [min, max]. If you don't want to set a value set it to null. So [1,null] will be a string with a minimum of one character but no maximum set.
* **regex** [*string*] : A regular expression to match against. This needs to be a Perl Compatible Regular Expression (i.e `/foo/`)


- **number**

Expand Down
48 changes: 48 additions & 0 deletions src/TypeValidator/StringValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,24 @@ public function validate(array $constraints, $data, $prettyName = 'This value')
$this->validateEnum($data, $constraints['enum'], $prettyName);
}

if (isset($constraints['regex'])) {
if (empty($constraints['regex'])) {
throw new InvalidRuleException(sprintf('The regex property of %s needs to be set', $prettyName));
}
$this->validateRegex($data, $constraints['regex'], $prettyName);
}

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

/**
* @param $data
* @param null $min
* @param null $max
* @param string $prettyName
* @return bool
* @throws DataValidationException
*/
public function validateLength($data, $min = null, $max = null, $prettyName = 'This value')
{
$length = strlen($data);
Expand All @@ -69,6 +84,13 @@ public function validateLength($data, $min = null, $max = null, $prettyName = 'T
return true;
}

/**
* @param $data
* @param array $enum
* @param string $prettyName
* @return bool
* @throws DataValidationException
*/
public function validateEnum($data, array $enum, $prettyName = 'This value')
{
if (in_array($data, $enum, true) == false) {
Expand All @@ -84,4 +106,30 @@ public function validateEnum($data, array $enum, $prettyName = 'This value')

return true;
}

/**
* @param $data
* @param $regex
* @param $prettyName
* @return bool
* @throws DataValidationException
* @throws InvalidRuleException
*/
public function validateRegex($data, $regex, $prettyName = 'This value')
{
$match = @preg_match($regex, $data);
if ($match === 0) {
throw new DataValidationException(
sprintf(
'%s is not an allowed value for %s. It does not match the regex: %s',
$data,
$prettyName,
$regex
)
);
} elseif ($match === false) {
throw new InvalidRuleException(sprintf('An error occured. The regex for %s may be invalid', $prettyName));
}
return true;
}
}
64 changes: 64 additions & 0 deletions tests/phpunit/src/Unit/TypeValidator/StringValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,68 @@ public function validateEnumlThrowsDataValidationException()

$typeValidator->validateEnum($data, $enum);
}

/**
* @test
* @expectedException \Mooti\Validator\Exception\InvalidRuleException
* @expectedExceptionMessage The regex property of This value needs to be set
*/
public function validateRegexThrowsInvalidRuleException()
{
$constraints = [
'regex' => ''
];

$data = 'foobar';

$typeValidator = new StringValidator;

$typeValidator->validate($constraints, $data);
}

/**
* @test
* @expectedException \Mooti\Validator\Exception\DataValidationException
* @expectedExceptionMessage bar is not an allowed value for This value. It does not match the regex: /foo/
*/
public function validateRegexThrowsDataValidationException()
{
$regex = '/foo/';

$data = 'bar';

$typeValidator = new StringValidator;

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

/**
* @test
* @expectedException \Mooti\Validator\Exception\InvalidRuleException
* @expectedExceptionMessage An error occured. The regex for This value may be invalid
*/
public function validateRegexThrowsInvalidRuleExceptionForInvalidRegex()
{
$regex = '/foo';

$data = 'bar';

$typeValidator = new StringValidator;

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

/**
* @test
*/
public function validateRegexSucceeds()
{
$regex = '/foo/';

$data = 'foo';

$typeValidator = new StringValidator;

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

0 comments on commit 79db2cc

Please sign in to comment.