承接 hunomina/data-validator 相关项目开发

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

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

hunomina/data-validator

Composer 安装命令:

composer require hunomina/data-validator

包简介

Library for json schemas validation

README 文档

README

Build Status codecov

Description : Library for data validation based on data schemas

This project is licensed under the terms of the MIT license.

Interfaces and classes

DataType

Allows to encapsulate the data into an object and format it in order to be validated using a DataSchema.

JsonData

JsonData implements DataType.

JsonData::format() uses json_decode() to format json string into php array.

DataRule

Allows to validate DataType based on rule specifications (is the data optional ?, is the data allowed to be...).

JsonRule

JsonRule implement DataRule.

JsonRule can validate :

Type/Check Null Length Pattern Min/Max Enum Date format Empty
String
Character
Number
Integer
Float
Boolean
Typed List

DataSchema

DataSchema is the library main class. It allows to validate DataType based on sub-schemas and DataRule.

DataSchema::validate() method allows this validation. If DataSchema::validate() does not validate the DataType it throws an InvalidDataException.

JsonSchema

JsonSchema implements DataSchema and validates JsonData using JsonRule.

How it works

See tests for examples

A JsonSchema has a type : object or list.

Objects are composed of rules and "child" schemas if needed.

This is a schema definition :

use hunomina\Validator\Json\Schema\Json\JsonSchema;

$schema = new JsonSchema([
    'success' => ['type' => 'bool'],
    'error' => ['type' => 'string', 'null' => true],
    'user' => ['type' => 'object', 'null' => true, 'optional' => true, 'schema' => [
        'name' => ['type' => 'string'],
        'age' => ['type' => 'int']
    ]]
]);

Schemas are just php arrays passe to JsonSchema::setSchema() method.

This schema is composed of 3 elements :

  • a rule success which :

    • is a boolean
    • can not be null
    • is not optional
  • a rule error which :

    • is a string
    • can be null
    • is not optional
  • a "child" schema user which :

    • is an object and therefor is represented by a schema which contains 2 elements : a name (string) and an age (integer)
    • can be null
    • is optional

When a data unit is being validated using this schema by calling the JsonSchema::validate() method, the schema will check recursively if the data respects the rules and the "child" schemas.

If the data has :

  • a boolean element success
  • a null or string element error
  • an optionally, null or object element user which must have :
    • a string element name
    • an integer element age

This data is valid :

use hunomina\Validator\Json\Data\Json\JsonData;

$data = new JsonData([
    'success' => true,
    'error' => null,
    'user' => [
        'name' => 'test',
        'age' => 10
    ]
]);

This one is not :

use hunomina\Validator\Json\Data\Json\JsonData;

$data = new JsonData([
    'success' => true,
    'error' => null,
    'user' => 'test'
]);

As said earlier, rules can be used to validate length or data pattern.

This schema uses the pattern validation on the name element and the length validation on the geolocation element :

use hunomina\Validator\Json\Schema\Json\JsonSchema;

$schema = new JsonSchema([
    'name' => ['type' => 'string', 'pattern' => '/^[a-z]+$/'],
    'geolocation' => ['type' => 'integer-list', 'length' => 2]
]);

When calling the JsonSchema::validate() method, the schema will recursively check all the rule set and "child" schemas. If one rule or one "child" schema is invalid, JsonSchema::validate() returns false.

The "first level" schema is an object typed schema. It could be changed but is not meant to.

Finally, if a "child" schema is typed as an object, the schema will validate it as described above. If it's typed as a list, the schema will simply check each element of the data as an object type using the given "child" schema.

hunomina/data-validator 适用场景与选型建议

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

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

围绕 hunomina/data-validator 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: proprietary
  • 更新时间: 2020-01-04