gilsegura / psr-validator
This is a project providing infrastructure for validating PSR HTTP messages.
Requires
- php: ^8.4
- opis/json-schema: ^2.3
- psr/http-message: ^2.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.95
- nyholm/psr7: ^1.8
- phpstan/phpstan: ^2.1
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^13.1
- rector/rector: ^2.4
README
Framework-agnostic validation of PSR-7 messages against JSON Schema. It validates
requests, responses and server requests, composes validators into a chain, and
loads schemas from files or raw strings. The gilsegura/psr-messages-bundle
package uses it as the validation middleware in a controller pipeline.
Installation
composer require gilsegura/psr-validator
Message validators
Each validator is an invokable that takes a PSR-7 message, validates it and
returns it (or throws a ValidationExceptionInterface on failure):
RequestValidator— validates an outgoingRequestInterface.ResponseValidator— validates aResponseInterface.ServerRequestValidator— validates an incomingServerRequestInterface.ValidatorChain— aMessageValidatorInterfacethat runs several validators in sequence, so request and response checks compose into one.
MessageValidatorInterface is the common contract (__invoke(MessageInterface): MessageInterface).
Schema validation
SchemaValidatorInterface/Schema\SchemaValidator— validate decoded data against a schema object, returning the list of violations;ValidatorExceptionsurfaces a schema-level failure.Exception\ValidationExceptionInterface— the marker validation failures implement, so callers catch a single type regardless of which part failed.
Schema factories
A schema factory builds the schema object the validators check against:
SchemaFactoryInterface— the contract; an invokable returning the schema.SchemaFactory\FileFactory— loads a JSON Schema from a.jsonfile.SchemaFactory\RawFactory— builds a schema from a raw string.SchemaFactory\SchemaFactoryException— raised when a schema cannot be built.
Middleware (PSR-15)
Middleware\ValidationMiddleware validates a message as part of a PSR-15
pipeline, so validation runs inline with the request flow rather than being
called by hand.
License
MIT. See LICENSE.