ergebnis/json-schema-validator

Provides a JSON schema validator, building on top of justinrainbow/json-schema.

4.0.0 2022-12-10 14:50 UTC

This package is auto-updated.

Last update: 2023-09-22 22:41:20 UTC


README

Integrate Merge Release Renew

Code Coverage Type Coverage

Latest Stable Version Total Downloads Monthly Downloads

Provides a JSON schema validator, building on top of justinrainbow/json-schema.

Installation

Run

composer require ergebnis/json-schema-validator

Usage

If you have used the validator from justinrainbow/json-schema before, you might have observed that it has a few flaws:

  • The validator is stateful.
  • The validator requires decoding JSON strings before validating them.
  • The validator returns an array of errors, where each error is an array.

This package delegates the validation to justinrainbow/json-schema and provides a friendlier interface.

<?php

declare(strict_types=1);

use Ergebnis\Json\Json;
use Ergebnis\Json\Pointer;
use Ergebnis\Json\SchemaValidator;

$json = Json::fromFile('composer.json');
$schema = Json::fromString(file_get_contents('https://getcomposer.org/schema.json'));
$jsonPointer = Pointer\JsonPointer::document();

$schemaValidator = new SchemaValidator\SchemaValidator();

$result = $schemaValidator->validate(
    $json,
    $schema,
    $jsonPointer
);

var_dump($result->isValid()); // bool
var_dump($result->errors());  // flat list of `ValidationError` value objects

Changelog

Please have a look at CHANGELOG.md.

Contributing

Please have a look at CONTRIBUTING.md.

Code of Conduct

Please have a look at CODE_OF_CONDUCT.md.

Security Policy

Please have a look at SECURITY.md.

License

This package is licensed using the MIT License.

Please have a look at LICENSE.md.

Curious what I am up to?

Follow me on Twitter!