aszenz/php-typed-schema
Composer 安装命令:
composer require aszenz/php-typed-schema
包简介
A php library to parse mixed data into proper types
关键字:
README 文档
README
PHP Typed Schema is a php library to parse unknown data (often as mixed type) into properly typed data
Typed Schema has built-in validators's for common use cases and a simple way to compose them to validate any form of data
Install
composer require aszenz/php-typed-schema
Use cases
Parsing array into typed dto
Let's say we have a well typed class Order
/** * @psalm-immutable */ final class Order { public function __construct( public int $id, public float $qty, public \DateTimeImmutable $date ) { } }
And we want to create it's object from a mixed data type like an array safely
// Assume this array comes from some external data source, and hence it's type is mixed. /** @var mixed **/ $orderInfo = ['id' => 1, 'quantity' => '123.20', 'order_date' => '02-02-2021'];
To convert this array to an object we write a schema for the expected array shape so that we can validate it.
The library provides a function Decoder::arrayKey('foo', Decoder::int()) which defines an array key foo of type int.
Essentially we want to validate that the array contains three keys of different value type's.
To represent this schema the library provides several functions like map2, map3, map4 that compose different decoders.
The library also provides built-in decoders/validators that parse numeric and date strings into their respective types.
Using them we can define our Order object's decoder/validator
// The type of this decoder is Decoder<Order> $orderDecoder = Decoder::map3( Decoder::arrayKey('id', Decoder::int()), // Notice how array key/value's don't have to match the object properties Decoder::arrayKey('quantity', Decoder::numeric()), Decoder::arrayKey('order_date', Decoder::dateString('d-m-Y')), fn (int $id, float $qty, \DateTimeImmutable $date) => new Order($id, $qty, $date) );
After defining it we can run it on our array and get the dto or an error if the schema didn't match the data.
Running the decoder gives us a Result type which can be either an Ok or Error object, we can unwrap it (throw's exception in case of error) to get our dto.
// Psalm/phpstan will correctly infer the result as Order $dto = $orderDecoder->run($orderInfo)->unwrap();
Parsing mixed array's to list of dto's
We can easily convert a list of array's to a list of dto's
Following from our previous example:
/** @var mixed */ $ordersInfo = [ ['id' => 1, 'quantity' => '123.20', 'order_date' => '02-02-2021'], ['id' => 2, 'quantity' => '3.20', 'order_date' => '03-04-2021'], ];
To convert this into a list of dto's we can reuse our existing decoder defined above:
// Psalm/phpstan will correctly infer it as list<Order> $listOfDtos = Decoder::listOf($orderDecoder)->run($ordersInfo)->unwrap();
Parsing json to dto
To validate json safely without any coupling between php objects and json fields, we can define the expected shape of the json and decode it for our use.
$jsonData = <<<JSON { "name": "Foo", "age": 21, "hobbies": ["writing", "travelling"] } JSON; $decoderForJson = Decoder::jsonDecode( Decoder::map3( Decoder::arrayKey('name', Decoder::nonEmptyString()), Decoder::arrayKey('age', Decoder::positiveInt()), Decoder::arrayKey('hobbies', Decoder::nonEmptyListOf(Decoder::string())), /** @psalm-pure */ function (string $name, int $age, array $hobbies) { // One can convert these variables into an object or // use them directly to perform some transformation } ) ); $result = $decoderForJson->run($jsonData); if ($result->isErr()) { echo 'Bad json data'; } $data = $result->unwrap();
Comparison with other php validation libraries
Other libraries in the php ecosystem are focused on validation or hydration of data into objects.
Typed schema separates concerns of parsing data and creating objects.
It leaves creating objects (or typed information) to the user, only performing the necessary checks to validate data conforms to the schema.
This approach is explicit and doesn't couple object's properties/constructors with the data source.
Credits
-
Inspired by Elm's Json Decode library
aszenz/php-typed-schema 适用场景与选型建议
aszenz/php-typed-schema 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 10 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 aszenz/php-typed-schema 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 aszenz/php-typed-schema 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 aszenz/php-typed-schema 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Adds request-parameter validation to the SLIM 3.x PHP framework
A jQuery augmented PHP library for creating and validating HTML forms
A Laravel validator for delimiter-separated list of emails.
A CakePHP behavior to validate foreign keys
PHP Validator for stuff.
Stand-alone PHP Class for Data Sanitization and Validation
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-19