Skip to content

validateSchema()

Validate JSON against a JSON Schema.

Signature

php
JsonFast::validateSchema(
    string $json,
    string $schemaJson,
    ?int $output = OUTPUT_ARRAY
): mixed

Parameters

NameTypeDefaultDescription
$jsonstringDocument to validate
$schemaJsonstringSchema as JSON string
$output?intOUTPUT_ARRAYOutput 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 Opis and 18× JustinRainbow on a medium test document — see benchmarks.

Native tools, weird experiments, and practical performance work.