承接 aneterial/laravel-data-validator 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

aneterial/laravel-data-validator

Composer 安装命令:

composer require aneterial/laravel-data-validator

包简介

Laravel package for convenient validation and hydration of a request into DTO structures

README 文档

README

Build Status License Packagist PHP Laravel

Contents

Prerequisites

  • PHP 8.2 or higher
  • Laravel 11

for dev

  • PHPUnit &11
  • phpstan ^1.11
  • php-cs-fixer ^3.58

Installation

Install package via composer

composer require aneterial/laravel-data-validator

Usage

You now have a class attribute DataValidator\Attributes\RequestProperty at your disposal. Add it to the properties of your DTO and set the necessary configuration fields

use DataValidator\Attributes\RequestProperty;

final readonly class ExampleDTO {
  #[RequestProperty(property: 'id', rules: 'required|integer|min:0')]
  public int $id;

  #[RequestProperty(property: 'email', rules: 'required|string|email')]
  public string $email;
}

Description of fields:

  • property: name of the request key that matches the property
  • rules: validation rules based on component semantics Laravel Validation, accepts only string value
  • requestDataType: to indicate where a field is expected - in the request body (default) const RequestProperty::BODY_TYPE or in query string const RequestProperty::QUERY_TYPE
  • listRules: if the value is an array (list) - set the validation rules for each element according to the semantics of Laravel Validation

Next, you need to get a DataValidator\DataManager instance in your controller from app DI container and pass the request essence to it, indicating the DTO class that you expect to receive after validation and filling with data.

$dataManager = app(\DataValidator\DataManager::class);

Next, the Laravel validator will check the request entity (instanse of \Illuminate\Http\Request), and if the data is incorrect, it will throw an \Illuminate\Validation\ValidationException. If the data is correct, the Manager will create and fill the DTO object with data, which you can use in your application

/** @var ExampleDTO $dto */
$dto = $dataManager->validateAndConvert(from: $request, to: ExampleDTO::class);

If your endpoint involves passing array of objects [{...}, {...}, {...}], you can use a method that will validate the request and return an array of DTOs

/** @var ExampleDTO[] $dtos */
$dtos = $dataManager->validateAndConvertList(from: $request, to: ExampleDTO::class);

Examples

Here are some examples of using validation by attributes

  1. DTO with lists
final readonly class ExampleDTO {
...

  /** @var string[] $emails */
  #[RequestProperty(property: 'emails', rules: 'required|list', listRules: 'string|email')]
  public array $emails;
  
  /** @var int[] $ids */
  #[RequestProperty(property: 'ids', rules: 'required|list', listRules: 'int|min:0')]
  public array $ids;

...
}
  1. DTO with non required properties, if it not required - it should be nullable, except array - it can be empty array
final readonly class ExampleDTO {
  #[RequestProperty(property: 'id', rules: 'integer|min:0')]
  public ?int $id;

  #[RequestProperty(property: 'email', rules: 'string|email')]
  public ?string $email;

  /** @var int[] $ids */
  #[RequestProperty(property: 'ids', rules: 'list', listRules: 'int|min:0')]
  public array $ids;
}
  1. DTO with nested object
final readonly class ExampleDTO {
...

  #[RequestProperty(property: 'child', rules: 'required|array')]
  public NestedDTO $child;

...
}

// NestedDTO should contain properties with attributes
final readonly class NestedDTO {
  #[RequestProperty(property: 'id', rules: 'required|integer|min:0')]
  public int $id;

  #[RequestProperty(property: 'email', rules: 'required|string|email')]
  public string $email;
}
  1. DTO with list of nested object
final readonly class ExampleDTO {
...

  /** @var NestedDTO[] $children */
  #[RequestProperty(property: 'children', rules: 'required|list', listRules: NestedDTO::class)]
  public array $children;

...
}
  1. DTO with enum of BackedEnum property, you should set enum type to property and no more rules are required except, if you want - indicate type of enum
final readonly class ExampleDTO {
...

  #[RequestProperty(property: 'enum', rules: 'required|string')]
  public AnApplicationEnum $enum;

...
}
  1. DTO with enums of BackedEnum property
final readonly class ExampleDTO {
...
  /** @var AnApplicationEnum[] $enums */
  #[RequestProperty(property: 'enums', rules: 'required|list', listRules: AnApplicationEnum::class)]
  public array $enums;

...
}

Restrictions

- Important note: for different requestDataType

if DTO has nested objects or arrays of nested objects, the requestDataType of these entities is ignored and taken from the parrent entity

So if you use

final readonly class ExampleDTO {
...

  #[RequestProperty(property: 'child', rules: 'required|array', requestDataType: RequestProperty::BODY_TYPE)]
  public NestedDTO $child;

...
}

// NestedDTO should contain properties with attributes
final readonly class NestedDTO {
  #[RequestProperty(property: 'id', rules: 'required|integer|min:0', requestDataType: RequestProperty::QUERY_TYPE)]
  public int $id;

  #[RequestProperty(property: 'email', rules: 'required|string|email', requestDataType: RequestProperty::QUERY_TYPE)]
  public string $email;
}

In nested object requestDataType will not work and it be RequestProperty::BODY_TYPE like in parrent entity

- Another one: for list validation

Method DataManager::validateAndConvertList can only work with data from request body, so all properties of entity will be force casted to type RequestProperty::BODY_TYPE in this usage

aneterial/laravel-data-validator 适用场景与选型建议

aneterial/laravel-data-validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 06 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「validation」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 aneterial/laravel-data-validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 aneterial/laravel-data-validator 我们能提供哪些服务?
定制开发 / 二次开发

基于 aneterial/laravel-data-validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-06-02