Use regular expressions for validation

This commit is contained in:
Sebastian Meyer 2024-01-10 11:45:39 +01:00
parent a0f4f0546f
commit e6a4d5d508
4 changed files with 47 additions and 32 deletions

View File

@ -35,25 +35,44 @@ use Symfony\Component\Validator\Validation;
abstract class Entity
{
/**
* Check if a string does not contain any whitespaces.
* Check if a string is a valid URL.
*
* @param string $url The URL
*
* @return string The validated URL
*
* @throws ValidationFailedException
*/
protected function validateUrl(string $url): string
{
$url = trim($url);
$validator = Validation::createValidator();
$violations = $validator->validate($url, new Assert\Url());
if ($violations->count() > 0) {
throw new ValidationFailedException(null, $violations);
}
return $url;
}
/**
* Check if a string matches a given regular expression.
*
* @param string $string The string
* @param string $regEx The regular expression
*
* @return string The validated string
*
* @throws ValidationFailedException
*/
protected function validateNoWhitespace(string $string): string
protected function validateRegEx(string $string, string $regEx): string
{
$string = trim($string);
$validator = Validation::createValidator();
$violations = $validator->validate(
$string,
[
new Assert\Regex([
'pattern' => '/\s/',
'match' => false,
'message' => 'This value contains whitespaces.'
'pattern' => $regEx,
'message' => 'This value does not match the regular expression "{{ pattern }}".'
]),
new Assert\NotBlank()
]
@ -64,26 +83,6 @@ abstract class Entity
return $string;
}
/**
* Check if a string is a valid URI.
*
* @param string $uri The URI
*
* @return string The validated URI
*
* @throws ValidationFailedException
*/
protected function validateUri(string $uri): string
{
$uri = trim($uri);
$validator = Validation::createValidator();
$violations = $validator->validate($uri, new Assert\Url());
if ($violations->count() > 0) {
throw new ValidationFailedException(null, $violations);
}
return $uri;
}
/**
* Check if a string is well-formed XML.
*

View File

@ -97,7 +97,7 @@ class Format extends Entity
public function setNamespace(string $namespace): void
{
try {
$this->namespace = $this->validateUri($namespace);
$this->namespace = $this->validateUrl($namespace);
} catch (ValidationFailedException $exception) {
throw $exception;
}
@ -115,7 +115,7 @@ class Format extends Entity
public function setSchema(string $schema): void
{
try {
$this->xmlSchema = $this->validateUri($schema);
$this->xmlSchema = $this->validateUrl($schema);
} catch (ValidationFailedException $exception) {
throw $exception;
}
@ -133,7 +133,7 @@ class Format extends Entity
public function __construct(string $prefix, string $namespace, string $schema)
{
try {
$this->prefix = $this->validateNoWhitespace($prefix);
$this->prefix = $this->validateRegEx($prefix, '/^[A-Za-z0-9\-_\.!~\*\'\(\)]+$/');
$this->setNamespace($namespace);
$this->setSchema($schema);
} catch (ValidationFailedException $exception) {

View File

@ -245,7 +245,11 @@ class Record extends Entity
public function __construct(string $identifier, Format $format, ?string $data = null, ?DateTime $lastChanged = null)
{
try {
$this->identifier = $this->validateNoWhitespace($identifier);
$this->identifier = $this->validateRegEx(
$identifier,
// xs:anyURI
'/^(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)?\/{0,2}[0-9a-zA-Z;\/?:@&=+$\\.\\-_!~*\'()%]+)?(#[0-9a-zA-Z;\/?:@&=+$\\.\\-_!~*\'()%]+)?$/'
);
$this->setFormat($format);
$this->setContent($data);
$this->setLastChanged($lastChanged);

View File

@ -163,6 +163,18 @@ class Set extends Entity
}
}
/**
* Set the name for this set.
*
* @param string $name The name
*
* @return void
*/
public function setName(string $name): void
{
$this->name = trim($name);
}
/**
* Get new entity of set.
*
@ -175,8 +187,8 @@ class Set extends Entity
public function __construct(string $spec, string $name, string $description = '')
{
try {
$this->spec = $this->validateNoWhitespace($spec);
$this->name = trim($name);
$this->spec = $this->validateRegEx($spec, '/^([A-Za-z0-9\-_\.!~\*\'\(\)])+(:[A-Za-z0-9\-_\.!~\*\'\(\)]+)*$/');
$this->setName($name);
$this->setDescription($description);
$this->records = new ArrayCollection();
} catch (ValidationFailedException $exception) {