Validating a document
The Validator catches what the type system cannot: a duplicate operationId, a path parameter left undeclared, a
$ref to nothing. It writes the document for a version, then checks it against the official JSON schema of that
version and against ten semantic rules.
Validating
use Ark4ne\OpenApiWriter\Enums\Version;
use Ark4ne\OpenApiWriter\Validation\OpisJsonSchemaValidator;
use Ark4ne\OpenApiWriter\Validation\Validator;
$validator = Validator::create(new OpisJsonSchemaValidator());
$violations = $validator->validate($document, Version::V3_1_2);
if (!$violations->isValid()) {
foreach ($violations->errors() as $violation) {
echo $violation, "\n";
}
}
OpisJsonSchemaValidatorchecks the official schema; it needsopis/json-schema. Without it,Validator::create()runs the semantic rules only. Build the validator once and reuse it.- The version defaults to the one declared with
OpenApi::openapi(), else 3.1.2. - What the version cannot express is reported, not thrown: the validator writes in
lenient mode, and each dropped feature is an error of the
rule
Serializer. The validation stops there: fix these first, and the schema and the rules run on the next call.
Reading the violations
Violations is iterable and countable:
| Method | Returns |
|---|---|
all() | every Violation |
errors() | the violations of severity error |
warnings() | the violations of severity warning |
isValid() | true when there is no error; warnings alone keep a document valid |
Each Violation has public pointer, rule, message and severity properties, and casts to one line:
$broken = OpenApi::create()
->info(Info::create()->title('Pets')->version('1.0.0'))
->paths(
PathItem::create('/pets/{id}')->operations(
Operation::get()
->operationId('show')
->tags('pets')
->responses(Response::ok()->content(
MediaType::json()->schema(Schema::ref('Pet')),
)),
),
PathItem::create('/pets')->operations(
Operation::get()
->operationId('show')
->responses(Response::ok()),
),
);
$messages = array_map('strval', $validator->validate($broken)->all());
[error] /paths/~1pets/get/operationId: operationId "show" is already used
at /paths/~1pets~1{id}/get (UniqueOperationIdRule)
[error] /paths/~1pets~1{id}/get: /paths/~1pets~1{id}/get has no required
in: path parameter for {id} (PathParametersRule)
[error] /paths/~1pets~1{id}/get/responses/200/content/application~1json/schema:
$ref "#/components/schemas/Pet" points to nothing (LocalReferencesRule)
[warning] /paths/~1pets~1{id}/get/tags/0: tag "pets" is not declared in tags
(DeclaredTagsRule)
The pointer is a JSON Pointer into the written document: ~1 stands for /. The rules are listed in
Validation rules.
In a test
Validate every version you publish, in your test suite:
use Ark4ne\OpenApiWriter\Enums\Version;
use Ark4ne\OpenApiWriter\Validation\OpisJsonSchemaValidator;
use Ark4ne\OpenApiWriter\Validation\Validator;
use PHPUnit\Framework\TestCase;
final class OpenApiTest extends TestCase
{
public function test_the_document_is_valid(): void
{
$document = (new ApiDocument())->build();
$validator = Validator::create(new OpisJsonSchemaValidator());
foreach ([Version::V3_0_4, Version::V3_1_2] as $version) {
$violations = $validator->validate($document, $version);
self::assertSame(
[],
array_map('strval', $violations->all()),
);
}
}
}
ApiDocument stands for whatever builds your document. Asserting on all() fails on warnings too; assert on
errors() to accept them.
Choosing the rules
withoutRules() removes rules by class, withRules() adds any class implementing Rule, and rules() lists them.
Both return a new validator:
use Ark4ne\OpenApiWriter\Validation\Rules\DeclaredTagsRule;
use Ark4ne\OpenApiWriter\Validation\Rules\NonEmptyDocumentRule;
$relaxed = $validator->withoutRules(
NonEmptyDocumentRule::class,
DeclaredTagsRule::class,
);
To write your own, see Custom rules.
Redoc extensions
A document written with Redoc extensions is validated with
the same option, so that the rules read x-webhooks like webhooks:
use Ark4ne\OpenApiWriter\Enums\VendorExtensions;
$validator->validate($document, '3.0.4', VendorExtensions::Redoc);