Swaggest JSON-Schema
JSON Schema validation and typed PHP class structures for Draft 4, 6, and 7.
Repository Health
Technical Analysis
Swaggest JSON-Schema is a high-fidelity PHP implementation of the JSON Schema specification, supporting Draft 4, 6, and 7. It validates JSON data against a schema and can also map that data onto strongly typed PHP class structures, so you get both runtime validation and IDE-friendly, autocompletable objects from the same schema definition.
Beyond validation, the library can import and export schemas, resolve local and remote $ref references, and round-trip between JSON documents and PHP objects, making it a foundation for API tooling, code generation, and data contracts.
What You Get
- Full validation against JSON Schema Draft 4, 6, and 7
- Mapping of JSON data onto typed PHP
ClassStructureobjects - Local and remote
$refresolution with a pluggable ref provider - Schema import and export between JSON and PHP representations
- Detailed exceptions pinpointing where and why validation failed
Common Use Cases
- Validating API request and response payloads against a schema
- Generating typed PHP structures from JSON Schema definitions
- Enforcing data contracts between services
- Building OpenAPI/Swagger tooling on top of JSON Schema
Under The Hood
Architecture - The core is src/Schema.php implementing SchemaContract, which walks a schema tree of Constraint objects to validate data within a Context. Reference handling is delegated to RefResolver and the RemoteRef provider, while Structure/ClassStructure and the MagicMap traits map validated data onto typed PHP objects. A SchemaExporter and DataPreProcessor handle the reverse direction and normalization, and dedicated Exception classes (InvalidValue, InvalidRef) report failures precisely.
Tech Stack - Pure PHP supporting PHP >= 7.1, depending only on swaggest/json-diff, phplang/scope-exit, and the mbstring polyfill, plus ext-json. This lean dependency set keeps it easy to embed anywhere.
Code Quality - The project has broad test coverage (codecov-tracked) exercising the official JSON Schema test suites across drafts, and its structure separates constraints, references, and mapping cleanly. Recent development activity has slowed, but the code is stable and battle-tested across many downstream tools.
API Design - Two ergonomic entry points serve different needs: Schema::import() for raw validation and ClassStructure subclasses for typed mapping with IDE autocompletion. The README’s worked examples make the common validation path quick to adopt, though the typed-structure and ref-resolution features have a steeper learning curve.