承接 michaelcozzolino/symfony-validation-enhancements-bundle 相关项目开发

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

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

michaelcozzolino/symfony-validation-enhancements-bundle

Composer 安装命令:

composer require michaelcozzolino/symfony-validation-enhancements-bundle

包简介

A set of symfony validations to enhance the experience with the Symfony validator

README 文档

README

The Symfony Validation Enhancements Bundle is a Symfony bundle designed to extend and enhance the default validation capabilities provided by the Symfony framework. It introduces additional constraints and validation features to facilitate more robust data validation within Symfony applications.

Key Features

  • Additional Validators: Custom validators that complement Symfony's native validation constraints for more specific and tailored validation rules.
  • Enhanced Validation Logic: Advanced validation mechanisms to implement complex scenarios with ease.

Installation

Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.

Applications that use Symfony Flex

Open a command console, enter your project directory and execute:

$ composer require michaelcozzolino\SymfonyValidationEnhancementsBundle

Applications that don't use Symfony Flex

Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles in the config/bundles.php file of your project:

// config/bundles.php

return [
    // ...
    MichaelCozzolino\SymfonyValidationEnhancementsBundle\SymfonyValidationEnhancementsBundle::class => ['all' => true],
];

Docs

The bundle automatically register the following event listeners:

RequestPayloadTrimmerListener

The RequestPayloadTrimmerListener is an event listener designed to trim whitespace from the request payload during the Symfony kernel request event. This ensures clean and sanitized data is passed through the application.

When used together with the NonEmptyString constraint, it will cause validation to fail for requests containing only whitespace or empty strings, such as:

{
    "name": "     ",
    "surname": ""
}

Usage

max is used to specify the maximum length of the string, if null or omitted, the max length is +inf.

use MichaelCozzolino\SymfonyValidationEnhancementsBundle\Validator\Constraint\NonEmptyString;

class MyRequest {
    public function __construct(
        #[NonEmptyString(max: 1000)]
        public readonly string $name,
        
        #[NonEmptyString(max: null)]
        public readonly string $surname
    ) {
    }
}

if working with MySql, more specific constraints can be used:

use MichaelCozzolino\SymfonyValidationEnhancementsBundle\Validator\Constraint\NonEmptyMySqlText;
use MichaelCozzolino\SymfonyValidationEnhancementsBundle\Validator\Constraint\NonEmptyMySqlVarcharDefault;

class PersonRequest {
    public function __construct(
        #[NonEmptyMySqlText]
        public readonly string $name,
        
        #[NonEmptyMySqlVarcharDefault]
        public readonly string $surname
    ) {
    }
}

ValidationErrorListener

The ValidationErrorListener automatically standardizes the obtained response after one or more validation failure. Let's suppose your request object is the following one:

use MichaelCozzolino\SymfonyValidationEnhancementsBundle\Validator\Constraint\NonEmptyString;
use Symfony\Component\Validator\Constraints as Assert;

class AddressRequest {
    public function __construct(
        #[NonEmptyString]
        public readonly string $street,
        
        #[Assert\Positive]
        public readonly int $number
    ) {
    }
}

class UserRequest {
    public function __construct(
        #[Assert\Positive]
        public readonly int $userId,
        
        #[Assert\All([
            new Assert\Positive
        ])]
        public readonly array $productIds,
        
        #[Assert\Valid]
        public readonly AddressRequest $address
    ) {
    
    }
}

and an invalid payload could be:

{
    "userId": -19,
    "productIds": [
        -1,
        2,
        0
    ],
    "address": {
        "street": "",
        "number": -1
    }
}

the listener above will return a json response structured as:

{
    "userId": [
        "validation error for -19"
    ],
    "productIds": {
        "0": [
            "validation error for -1"
        ],
        "2": [
            "validation error for 0"
        ]
    },
    "address": {
        "street": [
            "validation error for street"
        ],
        "number": [
            "validation error for number"
        ]
    }
}

So, the final json response is the same object from the request whose values are an array (in case of multiple constraint) of errors.

Constraints

In addition to the constraints mentioned before, we also have:

EntityExists

It checks that an entity exists, meaning that there exists one row in the database using the specified identifier.

Usage

use MichaelCozzolino\SymfonyValidationEnhancementsBundle\Validator\Constraint\EntityExists;class EntityRequest {
    public function __construct(
        #[EntityExists(entityClass: MyEntity::Class, validateExistence:true, entityProperty: 'id', entityName: 'my entity')]
        public readonly int $entityId
    ) {
    }
}

Parameters

entityClass: The class-string of the entity.

validateExistence: If set to true the validation will fail if the entity already exists, if set to false the validation will fail if the entity does not exist. The last one is useful for example when you want to store some data whose id is for example a non auto generated one but decided by the code. Default: true

entityProperty: The name of the column used as primary key to retrieve the entity. Default: 'id'

entityName: The name of the entity that will be used in the validation message. Default: null. In case the default value is used the validator will try to guess the short name of the entity class.

Message

type: string default: The requested `{entityName}` does not exist.

michaelcozzolino/symfony-validation-enhancements-bundle 适用场景与选型建议

michaelcozzolino/symfony-validation-enhancements-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 11 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 michaelcozzolino/symfony-validation-enhancements-bundle 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-08