承接 sgpinkus/jsonschema 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

sgpinkus/jsonschema

Composer 安装命令:

composer require sgpinkus/jsonschema

包简介

A PHP JSON Schema validator

README 文档

README

Draft v6 compliant JSON Schema validator for PHP:

  • Modular design.
  • Simple interface for validation.
  • JsonRef dereferencing is handled by an external PHP library JsonRef. You can easily replace it with a different one.
  • Easily extensible with custom constraints.
  • Draft v4 compatible.

Installation

composer install sgpinkus/jsonschema

Test

git clone ... && cd JsonSchema
git submodule update --init
composer test

Usage

In the simplest case, where you have a standalone JSON schema with no $refs:

<?php
require_once './vendor/autoload.php';
use JsonSchema\JsonSchema;
use JsonRef\JsonDocs;

$json = '{
  "users": [
    {
     "comment": "valid",
     "firstName": "John",
     "lastName": "Doe",
     "email": "john.doe@nowhere.com",
     "_id": 1
    },
    {
     "comment": "invalid",
     "firstName": "John",
     "lastName": "Doe",
     "email": "john.doe.nowhere.com",
     "_id": 2
    }
  ]
}';
$schema = '{
  "type": "object",
  "properties": {
    "firstName": { "type": "string", "minLength": 2 },
    "lastName": { "type": "string", "minLength": 2 },
    "email": { "type": "string", "format": "email" },
    "_id": { "type": "integer" }
  },
  "required": ["firstName", "lastName", "email", "_id"]
}';

$schema = new JsonSchema($schema);
$doc = json_decode($json);

// Validate whole doc.
$valid = $schema->validate($doc);
if($valid === true)
  print "OK\n";
else
  print $valid;

// Addendum: use JsonDocs::getPointer() to get a sub doc then validate it.
$valid = $schema->validate(JsonDocs::getPointer($doc, '/users/1'));
if($valid === true)
  print "OK\n";
else
  print "$valid";

If you have any $refs in your JSON schema, you need to use the JsonRef wrapper class to load and dereference the JSON schema documents:

<?php
require_once './vendor/autoload.php';
use JsonRef\JsonDocs;
use JsonSchema\JsonSchema;

$json = '{
  "comment": "valid",
  "firstName": "John",
  "lastName": "Doe",
  "email": "john.doe@nowhere.com",
  "_id": 1
}';
$schema = '{
  "id": "file:///tmp/jsonschema/user",
  "type": "object",
  "definitions" : {
    "_id" : { "type": "integer", "minimum": 0, "exclusiveMinimum": true },
    "commonName" : { "type": "string", "minLength": 2 }
  },
  "properties": {
    "firstName": { "$ref": "#/definitions/commonName" },
    "lastName": { "$ref": "#/definitions/commonName" },
    "email": { "type": "string", "format": "email" },
    "_id": { "$ref": "#/definitions/_id" }
  },
  "required": ["firstName", "lastName", "email", "_id"]
}';

// JsonDocs does the dereferencing, and also caches any loaded JSON docs. Without a loader it wont
// try to loader external resources though.
$jsonDocs = new JsonDocs();
$schema = new JsonSchema($jsonDocs->loadDocStr($schema, 'file:///tmp/some-unique-fake-uri'));
$valid = $schema->validate(json_decode($json));
if($valid === true)
  print "OK\n";
else
  print $valid;

To implement custom constraints extend the Constraint class and implement abstract methods, then register the constraint when creating the JsonSchema instance:

<?php
require_once './vendor/autoload.php';
use JsonSchema\JsonSchema;
use JsonSchema\Constraint\Constraint;
use JsonSchema\Constraint\Exception\ConstraintParseException;
use JsonSchema\Constraint\ValidationError;

class ModuloConstraint extends Constraint
{
  private $modulo;

  private function __construct(int $modulo) {
    $this->modulo = $modulo;
  }

  public static function getName() {
    return 'modulo';
  }

  public function validate($doc, $context) {
    if(is_int($doc) && $doc % $this->modulo !== 0) {
      return new ValidationError($this, "$doc is not modulo {$this->modulo}", $context);
    }
    return true;
  }

  public static function build($context) {
    if(!is_int($context->modulo)) {
      throw new ConstraintParseException("The value of 'modulo' MUST be an integer.");
    }

    return new static($context->modulo);
  }
}

$doc = 7;
$schema = '{
  "type": "integer",
  "modulo": 2
}';
$schema = new JsonSchema($schema, ['ModuloConstraint']);
$valid = $schema->validate($doc);
if($valid === true)
  print "OK\n";
else
  print $valid;

Also see cli-validator.php for example code.

TODO

See TODO.

CONFORMANCE

See conformance notes.

sgpinkus/jsonschema 适用场景与选型建议

sgpinkus/jsonschema 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 4, 最近一次更新时间为 2021 年 05 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 sgpinkus/jsonschema 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sgpinkus/jsonschema 我们能提供哪些服务?
定制开发 / 二次开发

基于 sgpinkus/jsonschema 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 4
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0
  • 更新时间: 2021-05-01