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 documents against JSON Schema in PHP. Supports Draft-3, Draft-4, Draft-6 and Draft-7 (coverage varies). Install via Composer and use JsonSchema\Validator to validate data with local file $ref schemas and inspect validation errors.

View on GitHub
Deep Wiki
Context7

A library to validate a json schema.

Frequently asked questions about Json Schema
How do I integrate justinrainbow/json-schema into a Laravel FormRequest for API validation?
Use the validator in your FormRequest’s `rules()` method or override `validate()` to manually validate JSON payloads. For example, decode the request data, validate against a schema, and throw a `ValidationException` if invalid. This works alongside Laravel’s built-in validation for hybrid approaches.
Which JSON Schema draft versions does this package support, and how do I check compliance?
The package supports Draft-3, Draft-4, Draft-6, and Draft-7. Compliance is tracked via Bowtie reports, accessible via badges on the GitHub repo. Draft-7 has the most features but may have edge-case limitations. Always verify the latest compliance before production use.
Can I use this package to validate API responses (e.g., JSON:API or GraphQL) in Laravel?
Yes. Decode the response JSON and validate it against a schema in your resolver or controller. For GraphQL, integrate it into field resolvers or middleware. This ensures responses adhere to contracts, improving API reliability and client-side parsing.
How do I handle remote schemas (e.g., HTTP or file:// references) in Laravel?
Use `$ref` with `file://` or `http://` URIs in your schema. For remote schemas, cache the fetched JSON locally to avoid repeated network calls. Laravel’s filesystem or HTTP clients (e.g., Guzzle) can pre-fetch schemas during boot or on-demand.
Does this package work with Laravel’s built-in Validator facade, or is it a standalone solution?
It’s standalone but can be integrated with Laravel’s Validator facade via a custom extension. Create a wrapper class to convert JSON Schema errors into Laravel’s `ValidationException` format. This bridges the gap between the two validation systems seamlessly.
What’s the performance impact of using JSON Schema validation in high-traffic Laravel APIs?
Schema validation adds overhead, especially for complex schemas. Benchmark against Laravel’s built-in validator for your specific use case. For high-throughput APIs (e.g., 10K+ requests/sec), consider caching compiled schemas or limiting validation to critical endpoints.
How do I test JSON Schema validation in Laravel’s PHPUnit tests?
Mock the validator or use real schemas in your tests. Validate sample data against schemas and assert `isValid()` or error messages. For edge cases, test malformed JSON or unsupported draft features. Integrate with Laravel’s testing helpers for request validation.
Can I use this package to validate database-stored JSON fields (e.g., Laravel’s json type)?
Yes. Retrieve the JSON data from the database, decode it, and validate against your schema. This is useful for enforcing structure on dynamic or polymorphic data stored in JSON columns, ensuring consistency across your application.
What are the alternatives to justinrainbow/json-schema for Laravel, and when should I choose them?
Alternatives include `spatie/laravel-validation-rules` (for simple rules) or `webonyx/graphql-php` (for GraphQL-specific validation). Use this package when you need full JSON Schema compliance, `$ref` support, or complex nested validation. For lightweight needs, Laravel’s built-in validator may suffice.
How do I handle validation errors in Laravel to match the default ValidationException format?
Create a wrapper class that converts JSON Schema errors into Laravel’s `ValidationException` format. Map error paths and messages to match Laravel’s expected structure. This ensures consistency with Laravel’s error handling and localization features.
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.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai