riley19280/fluent-json-schema 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

riley19280/fluent-json-schema

最新稳定版本:v1.2.1

Composer 安装命令:

composer require riley19280/fluent-json-schema

包简介

Create json schemas with ease

README 文档

README

Create json schema with ease

Installation

composer require riley19280/fluent-json-schema

Basic Usage

use FluentJsonSchema\FluentSchema;

$schema = FluentSchema::make()
    ->type()->object()
    ->property('name', FluentSchema::make()
        ->type()->string()
    )
    ->return()
    ->compile();
/* Results in
{
    "type": "object",
    "properties": {
        "name": {
            "type": "string"
        }
    }
}
*/

More advanced usage can be found here. The entire meta-schema spec has been implemented using this package.

Contexts

There are several different "contexts" that you can be in while constructing json schema objects. These are the main data type contexts

  • array
  • integer
  • number
  • object
  • string

Each of them have different methods that set specific properties related to that data type. If at any time you need to return to the "global" context, you can call the return method.

Boolean Schemas

In some cases, a schema that evaluates to true or false is needed, you can pass FluentSchema::make()->true() or FluentSchema::make()->false()

FluentSchema::make()
    ->type()->object()
    ->additionalProperties(FluentSchema::make()->false())

Converting to JSON

When done constructing your schema object, call the compile method on it. This will return a php array that you can then serialize to json.

By default, properties will be serialized in the order they are added to an object, i.e.

$schema = FluentSchema::make()->schema('schema')->id('id')->compile();
// Will be
// { "$schema": "schema", "$id": "id" }
$schema = FluentSchema::make()->id('id')->schema('schema')->compile();
// Will be
// { "$id": "id", "$schema": "schema" }

This can be changed by calling $schema->getSchemaDTO()->setKeyOrder(['$id', '$schema']). This will override the serialization order. Any unlisted keys will be added to the end.

Validation

Validation is done using the justinrainbow/json-schema package.

The following methods are available on the FluentSchema object to aid in schema validation:

  • getSchemaStorage(): SchemaStorage
  • setSchemaStorage(SchemaStorage $schemaStorage): static
  • validate(mixed &$data, int $checkMode = null): Validator
  • addValidationSchema(object|array $schema, string $id = null): static
$isValid = FluentSchema::make()
    ->type()->object()
    ->property('name', FluentSchema::make()
        ->type()->string()
    )
    ->return()
    ->validate((object)[
        'name' => 'validated!',
    ])
    ->isValid();

Please see the package documentation justinrainbow/json-schema for more detailed information on validation.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-29

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固