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(): SchemaStoragesetSchemaStorage(SchemaStorage $schemaStorage): staticvalidate(mixed &$data, int $checkMode = null): ValidatoraddValidationSchema(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.
统计信息
- 总下载量: 242
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-05-29