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

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

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

wamesk/laravel-validator

最新稳定版本:0.1.0

Composer 安装命令:

composer require wamesk/laravel-validator

包简介

Laravel extended validator.

README 文档

README

Laravel package that extends default laravel validator

Installation

composer require wamesk/laravel-validator

Publish translations

php artisan vendor:publish --provider="Wame\Validator\LaravelValidatorServiceProvider" --tag="translations"

Validator

Validator used by default in this package. It works by chaining functions and getting response. Response is generated by wamesk/laravel-api-response package functions. To better understand how response works checkout documentation for response package here

Validate function

This function is final function. Always last. It requires data and rules for validation. Documentation for rules click here

Usage example:

$data = ['email' => 'example@gmail.com', 'password' => 'password'];

$validator = Validator::validate($data, [
    'email' => 'email|required|max:255',
    'password' => 'required|string'
]);
if ($validator) return $validator;

In case of validation error it will return

{
    "data": null,
    "code": null,
    "errors": {
        "email": [
            "validation.required"
        ]
    },
    "message": null
}

Code function

This function is add internal code in response. You can pass second parameter that changes prefix for message translation.

Usage example:

$data = ['email' => 'example@gmail.com', 'password' => 'password'];

$validator = Validator::code('1.2')->validate($data, [
    'email' => 'email|required|max:255',
    'password' => 'required|string'
]);
if ($validator) return $validator;

In case of validation error it will return

{
    "data": "1.2",
    "code": null,
    "errors": {
        "email": [
            "The email field is required"
        ]
    },
    "message": "api.1.2"
}

Messages function

This function adds custom response for validation. You need to pass objects of which key is field, and it's validation. As value, you pass your custom message as shown in example.

Usage example:

$data = ['email' => 'example@gmail.com', 'password' => 'password'];

$validator = Validator::code('1.2')
    ->messages([
        'email.required' => 'Email is required'
    ])
    ->validate($data, [
        'email' => 'email|required|max:255',
        'password' => 'required|string'
    ]);
if ($validator) return $validator;

In case of validation error it will return

{
    "data": "1.2",
    "code": null,
    "errors": {
        "email": [
            "Email is required"
        ]
    },
    "message": "api.1.2"
}

Status Code function

This function doesn't change response visually but changes status code of response. Default status code is 400 (Bad Request). If you want to chain all functions it can look like this. Status code is always integer.

Validator::statusCode($statusCode)->code($code)->messages($messages)->validate($data, $rules);

Rules

This package also provides you with these custom rules for your project.

Usage:

Validator::code($code)->validate($data, [
    'id' => [
        new Exists(User::class),
    ]
]);

Exists rule

This rule validates if entity exists. It requires model class in construct. Firstly it validates if there is entity with this parameter in database. Secondly it checks if it wasn't deleted, if it was it returns validation error. You can pass second (optional) parameter column name.

new \Wame\Validator\Rules\Exists(User::class, 'id')

IsInteger rule

This rule validates if attribute is integer. You can pass additional data (min, max) in construct to create range of acceptable integers.

new \Wame\Validator\Rules\IsInteger(min: 10, max: 100)

IsString rule

This rule validates if attribute is string. You can pass additional data (min, max) to validate length of string.

new \Wame\Validator\Rules\IsString(min: 10, max: 100)

IsArray rule

This rule validates if attribute is array. You can pass additional data (min, max) to validate length of array.

new \Wame\Validator\Rules\IsString(min: 10, max: 100)

IsEmail rule

Validates email format, checks domain existence, and optionally blocks temporary email domains in Laravel. You can pass additional data (true, false) to enable some functions.

new \Wame\Validator\Rules\IsEmail(domainMustExist: false, disableTempMail: false)

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-02-15

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固