Skip to content

Repository files navigation

PHPNomad JSON Schema

Platform-agnostic JSON Schema validation contract for PHPNomad. Your code depends on a small interface; the concrete validator implementation is swappable without touching consumers.

This package ships interfaces only. Pair it with an implementation package — for example, a wrapper around opis/json-schema, or roll your own against any library that exposes per-violation metadata.

Requirements

PHP 8.2 or newer.

Installation

composer require phpnomad/json-schema

You will also need a concrete implementation package. The canonical one is phpnomad/opis-json-schema-integration.

Usage

usePHPNomad\JsonSchema\Interfaces\JsonSchemaValidatorStrategy;
usePHPNomad\JsonSchema\Exceptions\ValidationError;
/** @var JsonSchemaValidatorStrategy $validator */try {
$validator->validate($data, '/path/to/schema.json');
// Validation passed.
} catch (ValidationError$e) {
foreach ($e->failuresas$failure) {
// $failure->path, $failure->message, $failure->keyword
}
}

validate() returns true on success and throws ValidationError on failure. The exception carries the full collection of failures — one entry per violation — so callers can report every problem in a single pass rather than a fail-fast single message.

Contract

JsonSchemaValidatorStrategy

The single validation entry point. Implementations resolve $schemaUri (a file path, URL, or registered schema identifier) to a JSON Schema document and validate $data against it.

interface JsonSchemaValidatorStrategy
{
/** * @throws ValidationError When validation fails. */publicfunctionvalidate(array$data, string$schemaUri): bool;
}

ValidationError

Thrown on validation failure. Exposes a readonly collection of ValidationFailure.

finalclass ValidationError extends \Exception
{
/** @var ValidationFailure[] */publicreadonlyarray$failures;
}

ValidationFailure

A single violation. All fields are readonly promoted properties.

finalclass ValidationFailure
{
publicreadonlystring$path; // pointer to the offending location, e.g. "programs.gold.incentiveType"publicreadonlystring$message; // human-readable descriptionpublicreadonlystring$keyword; // JSON Schema keyword that failed — "type", "required", "enum", etc.
}

Why an abstraction

Recipe importers, configuration validators, and schema-driven APIs are natural places to want JSON Schema validation, but picking a specific library locks that choice into every consumer. This package lets your code depend on the idea of validating against a JSON Schema without caring which library actually does the work — swap implementations for performance, draft support, or licensing reasons without touching downstream code.

License

MIT

About

Platform-agnostic JSON Schema validation contract for PHPNomad. Interface-only abstraction — pair with an implementation package.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages