Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Json Schema

Json Schema Laravel Package

justinrainbow/json-schema

Validate JSON data against JSON Schema in PHP. Supports draft-3, draft-4, draft-6, and draft-7 with $ref resolution and detailed validation errors. Install via Composer and validate decoded JSON objects against local or remote schemas.

View on GitHub
Deep Wiki
Context7

justinrainbow/json-schema is a PHP library for validating JSON data against JSON Schema definitions. It supports common schema drafts (Draft-3, Draft-4, Draft-6, Draft-7) and is suited for enforcing payload contracts in APIs, configuration files, and data pipelines.

Use it to load JSON, reference schemas via $ref (including local files), and collect validation errors. Draft support varies—consult the Bowtie report for current compliance.

  • Validates JSON structures against JSON Schema
  • Supports multiple drafts: 3, 4, 6, 7
  • Handles schema references via $ref (e.g., file://...)
  • Provides detailed validation errors via a Validator API
  • Easy installation via Composer (composer require justinrainbow/json-schema)
Frequently asked questions about Json Schema
How do I integrate this package into Laravel’s FormRequest validation?
Override the `rules()` method in your `FormRequest` class to return a schema object. Use the `Validator` class to validate the JSON payload against your schema, then skip Laravel’s default validation by returning an empty array. This works well for complex nested JSON structures where Laravel’s built-in validation falls short.
Does this package support remote JSON Schema references (e.g., HTTP/HTTPS `$ref`)?
Yes, the package supports remote schemas via `$ref` (e.g., `https://example.com/schema.json`). Use the `SchemaStorage` class to cache and resolve references, but note that remote schemas may introduce latency. For production, consider implementing retry logic or caching mechanisms to handle failures gracefully.
Which JSON Schema drafts are fully supported, and how do I check compliance?
Draft-3, Draft-4, and Draft-6 are well-supported, while Draft-7 has partial compliance. Check the [Bowtie report](https://bowtie.report/#/implementations/php-justinrainbow-json-schema) for real-time compliance metrics. For strict Draft-6 compliance, use `Constraint::CHECK_MODE_STRICT` to enforce validation rules.
Can I use this package to validate API requests in Laravel middleware?
Yes, you can create middleware to validate incoming JSON payloads before they reach your controllers. Instantiate the `Validator` in middleware and validate the request data against your schema. If validation fails, return a `JsonResponse` with formatted errors, similar to Laravel’s default validation responses.
How do I customize error messages to match Laravel’s validation format?
Use the `Validator::setErrorFormatter()` method to customize error messages. Alternatively, map the package’s errors to Laravel’s `Validator` exceptions by catching validation failures and throwing `ValidationException` with formatted error bags. This ensures consistency with Laravel’s built-in validation language.
Will this package work with Laravel’s testing tools (e.g., `createJsonTest()`)?
Yes, you can integrate this package into Laravel’s testing suite. Use `createJsonTest()` to send JSON payloads and validate responses with the `Validator` class. For schema testing, leverage the package’s built-in test suite to verify compliance with your draft requirements.
How do I handle type coercion in validation (e.g., converting strings to booleans)?
Enable type coercion by passing `Constraint::CHECK_MODE_COERCE_TYPES` to the `validate()` method. However, be cautious—coercion modifies input data, which may conflict with Laravel’s immutable request handling. Clone the input data before validation or use coercion sparingly in non-critical paths.
What’s the best way to store and manage JSON schemas in a Laravel project?
Store schemas in a `resources/schemas/` directory for static schemas or fetch them dynamically from a config service/database. Use `SchemaStorage` to cache resolved schemas and handle `$ref` references efficiently. For remote schemas, implement caching (e.g., Redis) to avoid repeated HTTP requests.
Does this package support async validation for high-throughput APIs?
The package itself doesn’t support async validation natively, but you can queue validation jobs for heavy schemas using Laravel’s queue system. Dispatch a job to validate the payload against the schema and return a response immediately, then process the validation result later.
Are there alternatives to this package for JSON Schema validation in Laravel?
Yes, alternatives include `zendframework/zend-schema` (more feature-rich but heavier) and `webonyx/graphql-php` (if you’re using GraphQL). However, `justinrainbow/json-schema` is lightweight (~1MB) and integrates seamlessly with Laravel’s ecosystem, making it ideal for most use cases without unnecessary bloat.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport