Download the PHP package duyler/openapi without Composer
On this page you can find all versions of the php package duyler/openapi. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package openapi
Duyler OpenAPI Validator
OpenAPI 3.2 validator for PHP 8.4+
Features
- Full OpenAPI 3.2 Support - Complete implementation of OpenAPI 3.2 specification
- JSON Schema Validation - Full JSON Schema draft 2020-12 validation with 25+ validators
- PSR-7 Integration - Works with any PSR-7 HTTP message implementation
- Request Validation - Validate path parameters, query parameters, headers, cookies, and request body
- Response Validation - Validate status codes, headers, and response bodies
- Multiple Content Types - Support for JSON, form-data, multipart, text, and XML
- Built-in Format Validators - 12+ built-in validators (email, UUID, date-time, URI, IPv4/IPv6, etc.)
- Custom Format Validators - Easily register custom format validators
- Discriminator Support - Full support for polymorphic schemas with discriminators
- Type Coercion - Optional automatic type conversion
- PSR-6 Caching - Cache parsed OpenAPI documents for better performance
- PSR-14 Events - Subscribe to validation lifecycle events
- Error Formatting - Multiple error formatters (simple, detailed, JSON)
- Webhooks Support - Validate incoming webhook requests
- Schema Registry - Manage multiple schema versions
- Validator Compilation - Generate optimized validator code
Installation
Quick Start
Basic Usage
Usage
Loading OpenAPI Specifications
PSR-7 Integration
The validator works with any PSR-7 implementation:
Caching
Enable PSR-6 caching for improved performance:
Events
Subscribe to validation events using PSR-14:
Webhooks
Validate webhook requests:
Advanced Usage
Custom Format Validators
Register custom format validators for domain-specific validation:
Type Coercion
Enable automatic type conversion for query parameters and request body:
Error Formatters
Choose from built-in error formatters or create your own:
Discriminator Validation
Validate polymorphic schemas with discriminators:
Event-Driven Validation
Subscribe to validation lifecycle events:
Schema Registry
Manage multiple API versions:
Validator Pool
The validator pool uses WeakMap to reuse validator instances:
Validator Compilation
Generate optimized validator code:
Configuration Options
Builder Methods
| Method | Description | Default |
|---|---|---|
fromYamlFile(string $path) |
Load spec from YAML file | - |
fromJsonFile(string $path) |
Load spec from JSON file | - |
fromYamlString(string $content) |
Load spec from YAML string | - |
fromJsonString(string $content) |
Load spec from JSON string | - |
withCache(SchemaCache $cache) |
Enable PSR-6 caching | null |
withEventDispatcher(EventDispatcherInterface $dispatcher) |
Set PSR-14 event dispatcher | null |
withErrorFormatter(ErrorFormatterInterface $formatter) |
Set error formatter | SimpleFormatter |
withFormat(string $type, string $format, FormatValidatorInterface $validator) |
Register custom format | - |
withValidatorPool(ValidatorPool $pool) |
Set custom validator pool | new ValidatorPool() |
withLogger(object $logger) |
Set PSR-3 logger | null |
withEmptyArrayStrategy(EmptyArrayStrategy $strategy) |
Set empty array validation strategy | AllowBoth |
enableCoercion() |
Enable type coercion | false |
enableNullableAsType() |
Enable nullable validation (default: true) | true |
disableNullableAsType() |
Disable nullable validation | false |
Example Configuration
Supported JSON Schema Keywords
The validator supports the following JSON Schema draft 2020-12 keywords:
Type Validation
type- String, number, integer, boolean, array, object, nullenum- Enumerated valuesconst- Constant valuenullable- Allows null values (default: enabled)
Nullable Validation
By default, the nullable: true schema keyword allows null values for a property:
This behavior is enabled by default. To disable nullable validation and treat nullable: true as not allowing null values:
String Validation
minLength/maxLength- String length constraintspattern- Regular expression patternformat- Format validation (email, uri, uuid, date-time, etc.)
Pattern Validation
All regular expressions in schemas are validated during schema parsing. If a pattern is invalid, an InvalidPatternException is thrown.
Supported Pattern Fields
pattern- Regular expression for string validationpatternProperties- Object with patterns for property keyspropertyNames- Pattern for property name validation
Pattern Delimiters
The library automatically adds delimiters (/) to patterns without them. You can specify patterns with or without delimiters:
Both variants work identically.
Pattern Validation Errors
Invalid patterns are detected early and throw descriptive errors:
Numeric Validation
minimum/maximum- Range constraintsexclusiveMinimum/exclusiveMaximum- Exclusive rangesmultipleOf- Numeric division
Array Validation
items/prefixItems- Array item validationminItems/maxItems- Array length constraintsuniqueItems- Unique item requirementcontains/minContains/maxContains- Item presence validation
Object Validation
properties- Property definitionsrequired- Required propertiesadditionalProperties- Additional property rulesminProperties/maxProperties- Property count constraintspatternProperties- Pattern-based property validationpropertyNames- Property name validationdependentSchemas- Conditional schema application
Composition Keywords
allOf- Must match all schemasanyOf- Must match at least one schemaoneOf- Must match exactly one schemanot- Must not match schemaif/then/else- Conditional validation
Advanced Keywords
$ref- Schema referencesdiscriminator- Polymorphic schemasunevaluatedProperties/unevaluatedItems- Dynamic evaluation
Error Handling
Validation Exceptions
All validation errors throw ValidationException which contains detailed error information:
Common Validation Errors
| Error Type | Description |
|---|---|
TypeMismatchError |
Data type doesn't match schema type |
RequiredError |
Required property is missing |
MinLengthError / MaxLengthError |
String length constraint violation |
MinimumError / MaximumError |
Numeric range constraint violation |
PatternMismatchError |
Regular expression pattern violation |
InvalidFormatException |
Format validation failed (email, URI, etc.) |
OneOfError / AnyOfError |
Composition constraint violation |
EnumError |
Value not in allowed enum |
MissingParameterException |
Required parameter is missing |
UnsupportedMediaTypeException |
Content-Type not supported |
Error Formatters
Choose the appropriate error formatter for your use case:
Built-in Format Validators
The following format validators are included:
String Formats
| Format | Description | Example |
|---|---|---|
date-time |
ISO 8601 date-time | 2026-01-15T10:30:00Z |
date |
ISO 8601 date | 2026-01-15 |
time |
ISO 8601 time | 10:30:00Z |
email |
Email address | [email protected] |
uri |
URI | https://example.com |
uuid |
UUID | 550e8400-e29b-41d4-a716-446655440000 |
hostname |
Hostname | example.com |
ipv4 |
IPv4 address | 192.168.1.1 |
ipv6 |
IPv6 address | 2001:db8::1 |
byte |
Base64-encoded data | SGVsbG8gd29ybGQ= |
duration |
ISO 8601 duration | P3Y6M4DT12H30M5S |
json-pointer |
JSON Pointer | /path/to/value |
relative-json-pointer |
Relative JSON Pointer | 1/property |
Numeric Formats
| Format | Description | Example |
|---|---|---|
float |
Floating-point number | 3.14 |
double |
Double-precision number | 3.14159265359 |
Overriding Built-in Validators
Replace built-in validators with custom implementations:
Migration from league/openapi-psr7-validator
Key Differences
| Feature | league/openapi-psr7-validator | duyler/openapi |
|---|---|---|
| PHP Version | PHP 7.4+ | PHP 8.4+ |
| OpenAPI Version | 3.0 | 3.0, 3.1, 3.2 |
| JSON Schema | Draft 7 | Draft 2020-12 |
| Builder Pattern | Fluent builder | Fluent builder (immutable) |
| Type Coercion | Enabled by default | Opt-in |
| Error Formatting | Basic | Multiple formatters |
Migration Examples
Before (league/openapi-psr7-validator)
After (duyler/openapi)
Requirements
- PHP 8.4 or higher - Uses modern PHP features (readonly classes, match expressions, etc.)
- PSR-7 HTTP message -
psr/http-message ^2.0(e.g.,nyholm/psr7) - PSR-6 cache -
psr/cache ^3.0(e.g.,symfony/cache,cache/cache) - PSR-14 events -
psr/event-dispatcher ^1.0(e.g.,symfony/event-dispatcher)
Testing
License
MIT
All versions of openapi with dependencies
nyholm/psr7 Version ^1.8
psr/cache Version ^3.0
psr/event-dispatcher Version ^1.0
psr/http-message Version ^2.0
symfony/yaml Version ^7.0 || ^8.0