定制 phpnomad/json-schema 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

phpnomad/json-schema

最新稳定版本:1.0.1

Composer 安装命令:

composer require phpnomad/json-schema

包简介

Platform-agnostic JSON Schema validation contract for PHPNomad. Ships interfaces only — pair with a concrete integration package (e.g. opis-json-schema-integration).

README 文档

README

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

use PHPNomad\JsonSchema\Interfaces\JsonSchemaValidatorStrategy;
use PHPNomad\JsonSchema\Exceptions\ValidationError;

/** @var JsonSchemaValidatorStrategy $validator */
try {
    $validator->validate($data, '/path/to/schema.json');
    // Validation passed.
} catch (ValidationError $e) {
    foreach ($e->failures as $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.
     */
    public function validate(array $data, string $schemaUri): bool;
}

ValidationError

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

final class ValidationError extends \Exception
{
    /** @var ValidationFailure[] */
    public readonly array $failures;
}

ValidationFailure

A single violation. All fields are readonly promoted properties.

final class ValidationFailure
{
    public readonly string $path;     // pointer to the offending location, e.g. "programs.gold.incentiveType"
    public readonly string $message;  // human-readable description
    public readonly string $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

统计信息

  • 总下载量: 1.26k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-19

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固