amirhs712/rule-builder
Composer 安装命令:
composer require amirhs712/rule-builder
包简介
Validation Rule Builder for Laravel
README 文档
README
Nope is a validation rule builder for laravel inspired by Yup for javascript.
With Nope you can generate rules more fluently, I've also copied rules description from laravel official website and hard coded them into Nope,
so you can have access to documentations easily.
Some functionalities have been extended for validation rules, for example you can pass Carbon instances to date validation rules like after.
Installation
Require this package with composer using the following command:
composer require amirhs712/rule-builder
Usage
I recommend using nope() global helper for warning-free and complete ide-inspection.
Also you can use Amirhs712\RuleBuilder\Nope class instead.
$rules = [ 'username' => nope()->required()->string()->max(30)->get(), //[required,string,max:30] 'password' => Nope::required()->stringOf(30)->confirmed()->get() //[required,string,confirmed,max:30] ];
We use get() to get output as an array, alternatively you can use toString()
to return the output as a pipe separated string.
toStringandgetsupport a required status ranging from-2to1.- 1 => call
requiredrule - 0 => do nothing (default)
- -1 => call
sometimesrule - -2 => call
sometimes&nullablerules
- 1 => call
$rules = [ 'username' => nope()->stringOf(30)->toString(1), //string|max:30|required 'password' => nope()->stringOf(30)->toString(-2) //string|max:30|sometimes|nullable ];
Date Rules with Carbon
You can pass a carbon instance to the following date rules: after, afterOrEqual, dateEquals,before, beforeOrEqual
$rules = [ 'date' => nope()->after('today')->get(), 'another_date' => nope()->afterOrEqual(now()->addMonth())->get(), ];
Min / Max Helpers
These methods allow you to set min and / or max arguments manually.
Rule::activeUrl($max) ->alpha($min, $max) ->alphaDash($min, $max) ->alphaNum($min, $max) ->array($min, $max) ->file($max) ->image($max) ->integer($min, $max) ->json($max) ->numeric($min, $max) ->string($min, $max) ->url($max);
Raw rules
You can use raw(string|array) to add raw string rules or validation objects.
$rules=[ 'field1' => nope()->raw('string|max:30')->get(), 'field2' => nope()->raw(['string', 'max:30'])->get(), 'field3' => nope()->raw(new ValidationObject)->get(), 'field4' => nope()->raw([new ValidationObject, new AnotherObject])->get(), 'field5' => nope()->raw('string')->raw('max')->get(), ];
- Note: You cannot call
toStringmethod if you have validation objects. Doing so will result in an error.
Conditional rules
You can use when method to add rules conditionally.
nope()->when($conditionIsMet, function(Nope $nope){ $nope->max(100); });
Laravel constraint builders
in, notIn, dimensions, exists, unique methods support both their relative constraint builders and,
their default values.
$rules = [ 'image1' => nope()->dimensions(['width' => 300, 'height' => 300])->get(), 'image2' => nope()->dimensions(Rule::dimensions()->width(300)->height(300))->get(), ];
Templates
You can build and define your own rule templates.
Coming soon!
Undefined methods
Right now there are no limitations for method calls (I could not find a reason to do so), for example you can do
nope()->myCustomExtendedRule($arg1, $arg2,...)->get(); //Results in: ["my_custom_extended_rule:$arg1, $arg2,..."]
Alternatives
You can check this package as an alternative, I've implemented some ideas from this package and merged them into my own solution.
Feedback
Please feel free to open up an issue, or a Pull Request if you have any suggestions.
amirhs712/rule-builder 适用场景与选型建议
amirhs712/rule-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 336 次下载、GitHub Stars 达 3, 最近一次更新时间为 2020 年 10 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validation」 「laravel」 「rule」 「rule-builder」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 amirhs712/rule-builder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 amirhs712/rule-builder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 amirhs712/rule-builder 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
ContentElement for Contao to generate hr. Simply choose element type "divider" and save.
Magento 2 Discount Amount Limiter
Business rules engine tools.
Adds request-parameter validation to the SLIM 3.x PHP framework
A jQuery augmented PHP library for creating and validating HTML forms
Validation rules and an Eloquent Model attribute cast to value object encapsulating a South African government-issued personal identification number, for Laravel.
统计信息
- 总下载量: 336
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-10-26