horat1us/yii2-model-schema
Composer 安装命令:
composer require horat1us/yii2-model-schema
包简介
JSON Schema for Yii2 Model using validation rules
README 文档
README
Create JSON Schema from Yii2 Model using validation rules and other public methods.
Installation
Using composer:
composer require horat1us/yii2-model-schema:^2.0
Usage
base\Model extensions
Additional interfaces that will be used for generating JsonSchema, when they are implemented in model.
AttributesExamples
Will be used to generate property examples
See AttributesExamplesTrait for implementation Since 1.1.0
<?php declare(strict_types=1); namespace App; use Horat1us\Yii\Model; use yii\base; $model = new class extends base\Model implements Model\AttributesExamples { use Model\AttributesExamplesTrait; public function attributesExamples(): array { return [ 'a' => [1,2], 'b' => [], ]; } }; echo $model->getAttributeExamples('a'); // [1,2] echo $model->getAttributeExamples('b'); // null echo $model->getAttributeExamples('c'); // null echo $model->getAttributeExample('a'); // 1 echo $model->getAttributeExample('b'); // null echo $model->getAttributeExample('c'); // null
Conditional Required Fields
When a RequiredValidator has a when callable, JsonSchema evaluates it
against the model at schema-generation time. Only attributes whose when
returns true (or have no when) appear in the required array.
$model = new class extends base\Model { public string $passport_type = 'idcard'; public string $passport_number = ''; public string $idcard_number = ''; public function rules(): array { return [ [['passport_number'], 'required', 'when' => fn(base\Model $m): bool => $m->passport_type === 'legacy'], [['idcard_number'], 'required', 'when' => fn(base\Model $m): bool => $m->passport_type === 'idcard'], ]; } }; // passport_type is 'idcard' → only idcard_number in required $schema = (new JsonSchema($model))->jsonSerialize(); // $schema['required'] === ['idcard_number']
TODO
Write docs:
Contributors
License
统计信息
- 总下载量: 942
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 4
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-02-14