applySchema()
Apply schema defaults and remove properties not declared in the schema.
Signature
php
JsonFast::applySchema(
string $json,
string $schemaJson,
?int $output = OUTPUT_ARRAY
): mixedParameters
| Name | Type | Default | Description |
|---|---|---|---|
$json | string | — | Document to normalize |
$schemaJson | string | — | Schema as JSON string |
$output | ?int | OUTPUT_ARRAY | Output mode |
Returns
Normalized document in the requested output form.
Typical effects:
- Fill missing fields from schema
defaultvalues - Strip properties not listed under
properties(when schema defines a closed shape)
Examples
Normalize user input before save:
php
$clean = JsonFast::applySchema(
$request->getContent(),
$userSchema,
JsonFast::OUTPUT_STRING
);
$db->save($clean);Pipeline: validate then apply:
php
$check = JsonFast::validateSchema($raw, $schema);
if ($check['valid']) {
$stored = JsonFast::applySchema($raw, $schema, JsonFast::OUTPUT_STRING);
} else {
// handle errors from $check['errors']
}Related
validateSchema()— check without mutatinggetSchema()— generate schema from samples
