Skip to content

Validator

WP-FastEndpoints uses opis/json-schema for JSON schema validation.

The reason we don't use the default WordPress JSON schema validation functionality is because it's quite outdated: it only partially supports JSON schema draft 4. opis/json-schema on the other side, does support the latest JSON schema drafts.

Customising validator

One of the coolest features of opis/json-schema is that is super flexible, and supports:

These, can be super useful when ever you need some custom functionality in your JSON schemas.

Available hooks

There are three WordPress filter hooks that you can use to customise the JSON schema validators used in WP-FastEndpoints:

  1. fastendpoints_validator - Triggered by both middlewares
  2. fastendpoints_schema_validator - Only triggered for Schema middlewares validators
  3. fastendpoints_response_validator - Only triggered for Response middlewares validators

Example

Imagine we only want to accept even numbers. To solve this issue, we might want to create a new custom format for integers, called even, which checks if a given number is even, like:

use Opis\JsonSchema\Validator;

/**
 * Adds custom format resolvers to all JSON validators: request payload schema and response.
 *
 * @see fastendpoints_schema_validator - To update only the request payload schema validator, or
 * @see fastendpoints_response_validator - To update only the response validator
 */
add_filter('fastendpoints_validator', function (Validator $validator): Validator {
    $formatsResolver = $validator->parser()->getFormatResolver();
    $formatsResolver->registerCallable('integer', 'even', function (int $value): bool {
        return $value % 2 === 0;
    });

    return $validator;
});

Here is an example of a JSON schema using our custom even format:

{
  "type": "integer",
  "format": "even"
}

More examples can be found in Custom Formats docs ยป