定制 phpnomad/opis-json-schema-integration 二次开发

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

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

phpnomad/opis-json-schema-integration

最新稳定版本:1.0.3

Composer 安装命令:

composer require phpnomad/opis-json-schema-integration

包简介

Concrete PHPNomad JsonSchemaValidatorStrategy backed by opis/json-schema. Drop-in implementation with draft 2020-12 support.

README 文档

README

Concrete JsonSchemaValidatorStrategy implementation backed by opis/json-schema. Drop in to satisfy phpnomad/json-schema with full draft 2020-12 support.

Requirements

PHP 8.2 or newer.

Installation

composer require phpnomad/opis-json-schema-integration

This pulls in both phpnomad/json-schema (the abstraction) and opis/json-schema (the concrete validator) automatically.

Usage

use PHPNomad\JsonSchema\Exceptions\ValidationError;
use PHPNomad\OpisJsonSchema\Integration\Strategies\OpisJsonSchemaValidator;

$validator = new OpisJsonSchemaValidator();

try {
    $validator->validate(
        ['name' => 'Alice', 'age' => 30],
        '/path/to/person.schema.json',
    );
} catch (ValidationError $e) {
    foreach ($e->failures as $failure) {
        printf(
            "%s: %s [%s]\n",
            $failure->path,
            $failure->message,
            $failure->keyword,
        );
    }
}

Schema resolution

The $schemaUri argument accepts:

  • A local file path — the file is read and parsed as JSON before validation.
  • A URI string — passed straight through to Opis's resolver. Register prefixes or IDs on the underlying Opis\JsonSchema\Validator beforehand if you need custom resolution.

Custom Opis configuration

Subclass OpisJsonSchemaValidator and override the protected buildValidator() method to customize keyword registration, resolver prefixes, or other Opis behavior:

use Opis\JsonSchema\Validator as OpisValidator;
use PHPNomad\OpisJsonSchema\Integration\Strategies\OpisJsonSchemaValidator;

final class MySiteJsonSchemaValidator extends OpisJsonSchemaValidator
{
    protected function buildValidator(): OpisValidator
    {
        $validator = parent::buildValidator();
        $validator->resolver()->registerPrefix('https://my-site.example/', '/path/to/schemas');

        return $validator;
    }
}

The no-arg constructor keeps the class safe to auto-wire through standard dependency injection containers.

How failures are reported

Every Opis leaf error becomes a PHPNomad\JsonSchema\ValidationFailure:

  • path — data pointer, dotted notation (e.g. programs.gold.incentiveType; empty string at the root).
  • message — Opis's error message with placeholder substitution applied.
  • keyword — the JSON Schema keyword that failed (required, type, enum, etc.).

Container errors (allOf, oneOf, anyOf) are unwrapped — only the leaf violations are surfaced, so callers see one entry per actual problem.

License

MIT

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固