Validation Errors, Custom Exception Messages, New Parsers (CSV, Url, Email)
What's new?
- Validation Errors
- Custom Inline Exception Messages
- CSV Parsers
- Url Parser
- Email Parser
- New Approach To Exceptions
- Other Minor changes
Validation Errors:
$this->queryParameter('id')->int()->required()
Calling this via GET /?id=not_an_integer
will now throw a exception with the message
Invalid value for parameter "id". Expected an integer, but got "not_an_integer"
Previously, Parameter id not found
was the default exception message. That was not very insightful, as does not indicate that the validation failed.
The exception class in this case is \MPScholten\RequestParser\InvalidValueException
if not specified otherwise. It's a subclass of \MPScholten\RequestParser\NotFoundException
for b.c. reasons.
If you're using a custom exception or a custom exception message, you need to migrate your code to use this feature. More on that below.
Custom Inline Exception Messages:
If you need to override the exception message thrown by the library just once or twice, you can do this by passing the exception messages as the first and second argument to ->required()
:
$this->queryParameter('id')->int()->required("invalid id", "id not found in the request");
- The above code will throw an exception with the message
invalid id
instead of the default message if an invalid id parameter is provided. - If no id parameter is provided at all, the message is
id not found in the request
instead of the default message.
If you're using a custom exception or a custom exception message, you need to migrate your code to use this feature. More on that below.
CSV Parsers:
The library now supports comma separated values:
$names = $this->queryParameter('names')->commaSeparated()->string()->required();
GET /?names=John,Oliver
As expected it also supports ->defaultsTo
.
There is also ->commaSeparated()->int()->
, ->commaSeparated()->float()->
and many more. Take a look at the documentation for a list of all supported parsers
Url Parser:
$url = $this->queryParameter('url')->string()->url()->required();
It uses php's filter_var
for validation.
Email Parser:
$email = $this->queryParameter('email')->string()->email()->required();
It uses php's filter_var
for validation.
New Approach To Exceptions:
To better support the new features, like validation errors, the internal handling of exceptions was changed. This is only interesting to you if you're using custom exceptions with the library.
The old approach worked as follows:
$this->initRequestParser($request, function ($parameter) {
return new CustomException("The parameter $parameter was not found");
});
If you're still using that approach, you need to migrate to support newer features like validation errors. Check Using Custom Exception Classes and Using Custom Exception Messages to learn how the new way of configuring custom exceptions works.
Keep in mind that the old callback-based approach still works (so you don't need to upgrade it now), but then you cannot take advantage of validation errors and inline exception messages.
Minor changes:
- The default not found message got reworded from
Parameter $name not found
toParameter "$name" not found
- The README now has a table of contents