ergebnis/json-schema-validator

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

1.0.0 2021-02-02 13:02 UTC

README

Integrate Merge Prune Release Renew

Code Coverage Type Coverage

Latest Stable Version Total 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

use Ergebnis\Json\SchemaValidator;
use JsonSchema\Validator;

$json = SchemaValidator\Json::fromFile('composer.json');

$schema = SchemaValidator\Schema::fromJson(SchemaValidator\Json::fromString(file_get_contents('https://getcomposer.org/schema.json')));

$schemaValidator = new SchemaValidator\SchemaValidator(
    new SchemaValidator\Decoder(),
    new Validator()
);

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

var_dump($result->isValid()); // bool
var_dump($result->errors());  // flat list of error messages

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.

License

This package is licensed using the MIT License.

Please have a look at LICENSE.md.

Curious what I am building?

📬 Subscribe to my list, and I will occasionally send you an email to let you know what I am working on.