knplabs / php-json-schema
PHP json-schema实现
v0.1.0
2022-10-31 10:01 UTC
Requires
- php: >=8.0
This package is auto-updated.
Last update: 2024-09-08 10:10:22 UTC
README
这是一个PHP的JSON Schema实现。这个库允许你创建JSON Schema对象,并验证JSON数据是否符合这些对象。
安装
使用以下命令安装最新版本
composer require knplabs/php-json-schema
基本用法
JsonSchema必须实现KnpLabs\JsonSchema\JsonSchemaInterface
(它也扩展了JsonSerializable
)。
默认JsonSchema
已经有一个名为KnpLabs\JsonSchema\JsonSchema
的默认JsonSchemaInterface
实现,它是一个抽象类。这个类提供了一些静态方法来创建一些常见的JSON Schema标量或对象。
标量
JsonSchema::string()
use KnpLabs\JsonSchema\JsonSchema; $schema = JsonSchema::create( 'firstName', // The name of the property 'Hold the first name of the user', // The description of the property ['John', 'Georges'], // Some examples of possible values JsonSchema::string() // The type of the property );
JsonSchema::text()
use KnpLabs\JsonSchema\JsonSchema; $schema = JsonSchema::create( 'content', // The name of the property 'The content of the article', // The description of the property ['Lorem ipsum...'], // Some examples of possible values JsonSchema::text() // The type of the property );
JsonSchema::integer()
use KnpLabs\JsonSchema\JsonSchema; $schema = JsonSchema::create( 'age', // The name of the property 'Hold the age of the user', // The description of the property [25, 30], // Some examples of possible values JsonSchema::integer() // The type of the property );
JsonSchema::positiveInteger()
use KnpLabs\JsonSchema\JsonSchema; $schema = JsonSchema::create( 'age', // The name of the property 'Hold the age of the user', // The description of the property [25, 30], // Some examples of possible values JsonSchema::positiveInteger() // The type of the property );
JsonSchema::number()
use KnpLabs\JsonSchema\JsonSchema; $schema = JsonSchema::create( 'price', // The name of the property 'The price in dollars', // The description of the property [10.8, 30.0], // Some examples of possible values JsonSchema::number() // The type of the property );
JsonSchema::boolean()
use KnpLabs\JsonSchema\JsonSchema; $schema = JsonSchema::create( 'isAdult', // The name of the property 'Hold if the user is an adult', // The description of the property [true, false], // Some examples of possible values JsonSchema::boolean() // The type of the property );
JsonSchema::date()
use KnpLabs\JsonSchema\JsonSchema; $schema = JsonSchema::create( 'createdAt', // The name of the property 'The date of creation', // The description of the property ['2015-01-01', '2015-01-02'], // Some examples of possible values JsonSchema::date() // The type of the property );
枚举
枚举是标量的特殊类型,它是一系列可能的值。可以通过扩展KnpLabs\JsonSchema\EnumSchema
来创建枚举。
<?php namespace Acme; use KnpLabs\JsonSchema\EnumSchema; class RoleEnum extends EnumSchema { const ROLE_ADMIN = 'ROLE_ADMIN'; const ROLE_USER = 'ROLE_USER'; public function getTitle(): string { return 'Role enum'; } public function getDescription(): string { return 'Enum of the possible roles'; } public static function getEnum() { yield self::ROLE_ADMIN; yield self::ROLE_USER; } }
对象
你可以通过扩展KnpLabs\JsonSchema\ObjectSchema
类来创建对象模式。
<?php namespace Acme; use KnpLabs\JsonSchema\ObjectSchema; /** * @extends ObjectSchema<array{ * firstName: string, * lastName: string, * role: string, * }> */ class PersonSchema extends ObjectSchema { public function __construct() { $this->addProperty( 'firstName', JsonSchema::create( 'firstName', 'Hold the first name of the user', ['John', 'Georges'], JsonSchema::string() ) ); $this->addProperty( 'lastName', JsonSchema::create( 'lastName', 'Hold the last name of the user', ['Doe', 'Smith'], JsonSchema::string() ) ); $this->addProperty('role', new RoleEnum()); } }
集合
你可以通过扩展KnpLabs\JsonSchema\CollectionSchema
类来创建集合模式。
<?php namespace Acme; use KnpLabs\JsonSchema\CollectionSchema; class PersonCollectionSchema extends CollectionSchema { public function __construct() { parent::__construct(new PersonSchema()); } public function getDescription(): string { return 'The list of all the persons'; } }