Skip to content

applySchema()

Apply schema defaults and remove properties not declared in the schema.

Signature

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

Parameters

NameTypeDefaultDescription
$jsonstringDocument to normalize
$schemaJsonstringSchema as JSON string
$output?intOUTPUT_ARRAYOutput mode

Returns

Normalized document in the requested output form.

Typical effects:

  • Fill missing fields from schema default values
  • 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']
}

Native tools, weird experiments, and practical performance work.