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.
FormRequest, ApiResource). Can replace or augment Laravel’s built-in validation for complex JSON payloads (e.g., API requests, nested data structures)."true" to boolean true).Request objects (via json()->all() or input()).Validator facade via custom rules or service providers.Validating events).file://), enabling dynamic schema loading (e.g., from a config service or database).$ref references (e.g., for reusable sub-schemas).contentMediaType). Validate against your schema’s draft requirements.Constraint::CHECK_MODE_STRICT for Draft-6 if strict compliance is needed.CHECK_MODE_COERCE_TYPES modifies input data, which may conflict with Laravel’s immutable request handling (e.g., Illuminate\Http\Request).coerce() sparingly.property.x is required vs. property.x: "must be string").Validator::setErrorFormatter() or map errors to Laravel’s Validator exceptions.return_type_declaration and strict_types if enabled.resources/schemas/) or dynamic (e.g., fetched from a config service)?$ref resolution handle remote schemas (caching, retries, auth)?validateRequest()), middleware level (e.g., API gateway), or service level (e.g., domain logic)?Validator (e.g., custom error bags, API responses)?SchemaStorage) be cached?createJsonTest())?rules() to return schema objects or use a custom SchemaValidator trait.
use JsonSchema\Validator;
use JsonSchema\Constraints\Constraint;
class StoreRequest extends FormRequest {
public function rules(): array {
$validator = new Validator();
$validator->validate(
$this->json()->all(),
(object)['type' => 'object', 'properties' => [...]],
Constraint::CHECK_MODE_COERCE_TYPES
);
return []; // Skip Laravel's validator
}
}
SchemaValidator facade or bind the package to Laravel’s container.
$this->app->singleton(Validator::class, function () {
return new Validator(new Factory(new SchemaStorage()));
});
public function handle(Request $request, Closure $next) {
$validator = app(Validator::class);
$validator->validate($request->json()->all(), $this->schema);
return $next($request);
}
Illuminate\Validation\Validating events to inject schema validation.Illuminate\Bus\DispatchesJobs to queue validation for async processing.required|string) with schema validation for complex payloads.FormRequest::rules()).Validator entirely for API contracts (if performance allows).json-schema for complex schemas.$validator = Validator::make($request->all(), [
'nested.data' => ['json_schema' => file_get_contents('schema.json')],
]);
idn_to_ascii deprecation).Validator and json-schema for the same payload to prevent double-validation.json-schema as the source of truth and map errors to Laravel’s format.spatie/laravel-api-resources).laravel/pint, phpunit, and pestphp for schema-driven tests.resources/schemas/ or a database (e.g., for dynamic APIs).SchemaValidator service to centralize validation logic.class SchemaValidator {
public function __construct(private Validator $validator) {}
public function validate(array $data, string|object $schema): void {
$this->validator->validate($data, $schema);
if (!$this->validator->isValid()) {
throw new ValidationException($this->validator->getErrors());
}
}
}
ValidationException that formats json-schema errors for Laravel’s Validator.throw ValidationException::withMessages([
'property.x' => collect($validator->getErrors())
->map(fn($e) => $e['message'])
->unique()
->values()
->toArray(),
]);
JsonTestCase.public function test_schema_validation() {
$validator = new Validator();
$validator->validate(['data' => 'test'], (object)['type' => 'object', 'properties' => [...]]);
$this->assertTrue($validator->isValid());
}
SchemaStorage).How can I help you explore Laravel packages today?