承接 tzurbaev/laravel-extra-fields-validator 相关项目开发

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

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

tzurbaev/laravel-extra-fields-validator

Composer 安装命令:

composer require tzurbaev/laravel-extra-fields-validator

包简介

Extra fields validator for Laravel.

README 文档

README

GitHub Workflow Status StyleCI ScrutinizerCI Latest Version on Packagist Software License

Description

This package allows to perform validation of exta (redundant) fields that were sent with HTTP request.

All you need to do is create new Form Request by extending Laravel\ExtraFieldsValidator\ExtraFormRequest class (instead of Illuminate\Foundation\Http\FormRequest).

Since the ExtraFormRequest extends the original FormRequest class, you can work with this class just as with regular Form Request.

Behind the scenes ExtraFormRequest creates custom instance of Validator class (Laravel\ExtraFieldsValidator\Validator) that allows to register & run extra callbacks (after successful validation & after failed validation).

After successful validation ExtraFormRequest will try to find fields that are not described in rules list but present in actual HTTP request payload.

If there's any, response will finish with HTTP 422 Unprocessable Entity response and custom error message will be added to errors bag (ExtraFormRequest::getExtraFieldErrorMessage(string $field): string).

Requirements

This package requires PHP 7.4 or higher.

Installation

You can install the package via composer:

$ composer require tzurbaev/laravel-extra-fields-validator

Also you can publish package configuration:

php artisan vendor:publish --provider="Laravel\ExtraFieldsValidator\ExtraFieldsValidatorServiceProvider"

Usage

Let's say you have the following request:

<?php

namespace App\Http\Requests\Users;

use Laravel\ExtraFieldsValidator\ExtraFormRequest;

class StoreUserRequest extends ExtraFormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'first_name' => 'required|string|max:255',
            'last_name' => 'required|string|max:255',
            'age' => 'required|integer|min:0',
        ];
    }
}

If actual HTTP request payload contains any other field, validation will fail.

Using custom validator

If you're creating custom validator via FormRequest's validator method, you need to use result of parent::validator() method as your base validator.

<?php

namespace App\Http\Requests\Users;

use Laravel\ExtraFieldsValidator\ExtraFormRequest;

class StoreUserRequest extends ExtraFormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'first_name' => 'required|string|max:255',
            'last_name' => 'required|string|max:255',
            'age' => 'required|integer|min:0',
        ];
    }
    
    public function validator()
    {
        $validator = parent::validator();

        // Some custom logic.

        return $validator;
    }
}

Using custom form request

If you're using custom FormRequest class and can't extend from ExtraFormRequest, you can include Laravel\ExtraFieldsValidator\ProvidesExtraFieldsValidator trait into your base/child class.

Error message

You need to add message to your validation.php language file (under the custom.extra_field path). The extra field name will be passed as :attribute replacement.

You can also override ExtraFormRequest::getExtraFieldErrorMessage method and return any custom message.

Data source

By default validator uses Laravel's FormRequest::validationData() method to retrieve data that should be validated. If you have request with no validation rules or your route can accept optional query params, this might lead to unexpected validation errors, since optional query params might be missing from your rules array.

You have 2 options to deal with this:

  1. Describe optional params in your rules method. This will not break your requests with optional query params but if there's any other non-described param, you will face validation exception;
  2. Change the extra-validator.data_source (or ENV variable EXTRA_VALIDATOR_DATA_SOURCE) value from default to input_source. This will instruct validator only to use JSON/Request data without query params.

GET requests

In most cases you should not use ExtraFormRequest for your GET routes. If you really need it, describe all available params in your rules method.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email zurbaev@gmail.com instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

tzurbaev/laravel-extra-fields-validator 适用场景与选型建议

tzurbaev/laravel-extra-fields-validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.68k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 01 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-01-22