Request Validation API: Validating Request Parameters
This API validates client request based on value of parameters and request method it came with by matching it to an XML file where validators are setup.
How does it work?
To better understand how it works, one has to imagine an interplay of following components working for the common goal of request validation:
- request: This is the payload sent by client to server in order to retrieve a resource owned by latter (eg: page). Following request components are relevant for validation:
- url: location of resource (page) on server.
- method: HTTP method used by client in resource request (eg: GET). Must use capitals!
- parameters: parameters that came along with url (eg: POST), making resource request specific.
- XML: a file where validators are defined in <routes> tag for each route based on parameter name and request method
- ParameterValidator: an abstract class holding recipe for parameter value validation, saving validation result into a Result object
- Result: object that encapsulates result of a validation along with status as ResultStatus
- Validator: a class that glues everything together by parsing XML and running all ParameterValidator instances found matching request.
- ResultsList: a list of Result objects obtained after ParameterValidator instances were ran, available via Validator's getResults() method.
All classes inside API belong to namespace Lucinda\RequestValidator!
How should developers deal with validation results?
Using components above, this API aims at functioning like a black box that expects request and XML file location being fed to Validator and latter to present global validation results.
API makes no assumption how your application should behave when validation fails for one or more parameters! Instead it only provides a platform where, based on methods of ResultsList, developers can:
- check if validation has failed for at least one parameter via hasPassed() method.
- get value that resulted from a successful parameter value validation (eg: a database ID) via get("PARAM_NAME") method.
Moreover, on each validation, ResultsList object is passed to next ParameterValidator object constructor and become available to validate("PARAM_VALUE") method as $this->pendingResults. This feature is useful to maintain efficiency when parameters depend of each other!
How can I use it?
To learn how to install and use this API, follow this step-by-step guide!