Skip to content

Commit

Permalink
Allowed params in GroupPath (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
borisvalo authored and f3l1x committed Nov 28, 2021
1 parent 49130e3 commit 1520fcb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
31 changes: 28 additions & 3 deletions src/Core/Schema/Validation/GroupPathValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,44 @@ protected function validateRegex(SchemaBuilder $builder): void
// -> a-z
// -> A-Z
// -> 0-9
// -> -_/
$match = Regex::match($path, '#([^a-zA-Z0-9\-_/]+)#');
// -> -_/{}
$match = Regex::match($path, '#([^a-zA-Z0-9\-_/{}]+)#');

if ($match !== null) {
throw new InvalidSchemaException(
sprintf(
'@Path "%s" in "%s" contains illegal characters "%s". Allowed characters are only [a-zA-Z0-9-_/].',
'@Path "%s" in "%s" contains illegal characters "%s". Allowed characters are only [a-zA-Z0-9-_/{}].',
$path,
$controller->getClass(),
$match[1]
)
);
}

// Allowed parameter characters:
// -> a-z
// -> A-Z
// -> 0-9
// -> -_
// @regex https://regex101.com/r/APckUJ/3
$matches = Regex::matchAll($path, '#\{(.+)\}#U');
if ($matches !== null) {
foreach ($matches as $item) {
$match = Regex::match($item[1], '#.*([^a-zA-Z0-9\-_]+).*#');

if ($match !== null) {
throw (new InvalidSchemaException(
sprintf(
'@Path "%s" in "%s" contains illegal characters "%s" in parameter. Allowed characters in parameter are only {[a-z-A-Z0-9-_]+}',
$path,
$controller->getClass(),
$match[1]
)
))
->withController($controller);
}
}
}
}
}
}
Expand Down
30 changes: 28 additions & 2 deletions tests/cases/Core/Schema/Validation/GroupPathValidation.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ test(function (): void {
});
});

// Validate: success with parameter
test(function (): void {
$validation = new GroupPathValidation();
$builder = new SchemaBuilder();

$c1 = $builder->addController('c1');
$c1->addGroupPath('/foo/{bar}');

Assert::noError(function () use ($validation, $builder): void {
$validation->validate($builder);
});
});

// Validate: only /
test(function (): void {
$validation = new GroupPathValidation();
Expand Down Expand Up @@ -69,9 +82,22 @@ test(function (): void {
$builder = new SchemaBuilder();

$c1 = $builder->addController('c1');
$c1->addGroupPath('/{foo$}');
$c1->addGroupPath('/foo$');

Assert::exception(function () use ($validation, $builder): void {
$validation->validate($builder);
}, InvalidSchemaException::class, '@Path "/foo$" in "c1" contains illegal characters "$". Allowed characters are only [a-zA-Z0-9-_/{}].');
});

// Validate: invalid parameter (contains)
test(function (): void {
$validation = new GroupPathValidation();
$builder = new SchemaBuilder();

$c1 = $builder->addController('c1');
$c1->addGroupPath('/{foo&&&bar}');

Assert::exception(function () use ($validation, $builder): void {
$validation->validate($builder);
}, InvalidSchemaException::class, '@Path "/{foo$}" in "c1" contains illegal characters "{". Allowed characters are only [a-zA-Z0-9-_/].');
}, InvalidSchemaException::class, '@Path "/{foo&&&bar}" in "c1" contains illegal characters "&&&". Allowed characters are only [a-zA-Z0-9-_/{}].');
});

0 comments on commit 1520fcb

Please sign in to comment.