validateSchema()
Validate JSON against a JSON Schema.
Signature
php
JsonFast::validateSchema(
string $json,
string $schemaJson,
?int $output = OUTPUT_ARRAY
): mixedParameters
| Name | Type | Default | Description |
|---|---|---|---|
$json | string | — | Document to validate |
$schemaJson | string | — | Schema as JSON string |
$output | ?int | OUTPUT_ARRAY | Output mode |
Returns
php
[
'valid' => true,
]
// or when invalid:
[
'valid' => false,
'errors' => [
'$.id expected integer, got string',
'$.email is required',
],
]Error messages use JSON Pointer-style paths ($.field).
Schema support
Implements a focused subset: type, required, properties, items, default. Not a full JSON Schema draft validator — sufficient for many API contracts without Composer packages like Opis or JustinRainbow.
Examples
php
$schema = JsonFast::getSchema($knownGood, JsonFast::OUTPUT_STRING);
$result = JsonFast::validateSchema($incoming, $schema);
if (!$result['valid']) {
foreach ($result['errors'] as $err) {
logger()->warning($err);
}
}Request validation middleware:
php
$result = JsonFast::validateSchema($request->getContent(), $apiSchema);
if (!$result['valid']) {
return response()->json(['errors' => $result['errors']], 422);
}Performance
Project benchmarks show roughly 9× Opis and 18× JustinRainbow on a medium test document — see benchmarks.
Related
getSchema()— infer schemaapplySchema()— coerce / clean data
