danilopolani/laravel-json-validation-testing
Composer 安装命令:
composer require danilopolani/laravel-json-validation-testing
包简介
A better JSON validation errors testing
README 文档
README
A simple library to help testing JSON validation errors by rules.
The current way to test HTTP errors is broken: it tests that the validation fails or you have to manually specify the error message. With this package you'll just need to specify the validation rule that fails and it builds the error message to be sure at 100% that it fails and what fails. (Further reading here in the old merge request for Laravel).
Installation
You can install the package via composer:
composer require --dev danilopolani/laravel-json-validation-testing
Usage
The package provides an helper to retrieve a compiled error message:
use DaniloPolani\JsonValidation\JsonValidation; JsonValidation::getRuleErrorMessage('foo', 'required'); // => ["The foo field is required."]
However, if you need to test that your HTTP APIs, the package ships with a brand new assertJsonValidationErrorRule assertion to make your life easier:
it('throws validation error', function () { $this->postJson('/') ->assertJsonValidationErrorRule('foo', 'required'); });
It supports as well dynamic rules, such as between, size, max etc. You just need to specify the type of rule you want to apply:
it('throws validation error', function () { $this->postJson('/') ->assertJsonValidationErrorRule('foo', 'between.string:1,5') // The foo must be between 1 and 5 characters. ->assertJsonValidationErrorRule('foo', 'size.array:3'); // The foo must contain 3 items. });
You can even test multiple validation errors at once by providing an array of field => rule as argument:
use DaniloPolani\JsonValidation\JsonValidation; it('throws validation error', function () { $this->postJson('/') ->assertJsonValidationErrorRule([ 'foo' => 'required', 'bar' => 'required_array_keys:foo,baz', ]); });
Custom rules
If you want to test a custom rule, make sure it implements the interface DaniloPolani\JsonValidation\Contracts\HasRuleMessage, needed to extract the failing message to check against.
For example, a custom Rule would look like this:
<?php namespace App\Rules; use Closure; use DaniloPolani\JsonValidation\Contracts\HasRuleMessage; use Illuminate\Contracts\Validation\ValidationRule; class Uppercase implements ValidationRule, HasRuleMessage { /** * Run the validation rule. */ public function validate(string $attribute, mixed $value, Closure $fail): void { if (strtoupper($value) !== $value) { $fail($this->message()); } } public function message(): string { return 'The :attribute must be uppercase.'; } }
And then you can use it in your assert function:
Of course you can provide your own custom validation Rules:
it('throws validation error', function () { $this->postJson('/') ->assertJsonValidationErrorRule('foo', new Uppercase()); });
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
danilopolani/laravel-json-validation-testing 适用场景与选型建议
danilopolani/laravel-json-validation-testing 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.24k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2022 年 03 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「Danilo Polani」 「laravel-json-validation-testing」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 danilopolani/laravel-json-validation-testing 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 danilopolani/laravel-json-validation-testing 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 danilopolani/laravel-json-validation-testing 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Breadcrumbs in an uncomplicated way. This component facilitates the way to create breadcrumbs for your website, to favor a more accessible navigation. It was designed to work very well with the Bootstrap Framework. Besides being very easy to use, it has very interesting possibilities, such as adding
Alfabank REST API integration
Trigger is the absurdly easy way to communicate with the user of your application through highly customizable messages
Zipper is a facilitator for creating Zip Files in an uncomplicated way, with features of Download, Insert Files, Extraction and Creation of Zip with multiple in one!
A Laravel package designed to simplify the creation of structured and formatted API responses (JSON, XML) with custom status codes, messages, and data. XML support is currently under construction.
A Laravel package to automatically generate factories and seeders based on models and their relationships.
统计信息
- 总下载量: 17.24k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-02