getSchema()
Infer a JSON Schema from an example document.
Signature
php
JsonFast::getSchema(string $json, ?int $output = OUTPUT_ARRAY): mixedParameters
| Name | Type | Default | Description |
|---|---|---|---|
$json | string | — | Example JSON document |
$output | ?int | OUTPUT_ARRAY | Output mode |
Returns
Inferred schema as array, JSON string, or object depending on $output. Covers a focused subset of JSON Schema: types, required, properties, items, default — not the full draft spec.
Examples
Generate schema from production sample:
php
$sample = file_get_contents('samples/order.json');
$schema = JsonFast::getSchema($sample, JsonFast::OUTPUT_STRING);
file_put_contents('schemas/order.json', $schema);Bootstrap validation for a new endpoint:
php
$schema = JsonFast::getSchema($firstSuccessfulResponse);
$ok = JsonFast::validateSchema($nextResponse, $schema);Related
validateSchema()— check documents against schemaapplySchema()— defaults and strip extrasschemaDiff()— compare schema versions
