Validate

EasySwoole 提供了自带基础的验证类,默认在控制器中带有一个validate方法,如果希望用其他的方法或者是工具去做检验,可以在子类控制器中重写该方法,从而实现用其他工具进行校验

::: warning 验证器类: EasySwoole\Validate\Validate :::

基础使用

  1. <?php
  2. use EasySwoole\Validate\Validate;
  3. $data = [
  4. 'name' => 'blank',
  5. 'age' => 25
  6. ];
  7. $valitor = new Validate();
  8. $valitor->addColumn('name', '名字不为空')->required('名字不为空')->lengthMin(10,'最小长度不小于10位');
  9. $bool = $valitor->validate($data);
  10. var_dump($bool?"true":$valitor->getError()->__toString());
  11. /* 结果:
  12. string(26) "最小长度不小于10位"
  13. */

控制器中封装使用

  1. namespace App\HttpController;
  2. use EasySwoole\Http\Message\Status;
  3. use EasySwoole\Validate\Validate;
  4. use EasySwoole\Http\AbstractInterface\Controller;
  5. class BaseController extends Controller
  6. {
  7. protected function onRequest(?string $action): ?bool
  8. {
  9. $ret = parent::onRequest($action);
  10. if($ret === false){
  11. return false;
  12. }
  13. $v = $this->validateRule($action);
  14. if($v){
  15. $ret = $this->validate($v);
  16. if($ret == false){
  17. $this->writeJson(Status::CODE_BAD_REQUEST,null,"{$v->getError()->getField()}@{$v->getError()->getFieldAlias()}:{$v->getError()->getErrorRuleMsg()}");
  18. return false;
  19. }
  20. }
  21. return true;
  22. }
  23. protected function validateRule(?string $action):?Validate
  24. {
  25. }
  26. }

::: warning 我们定义了一个带有validateRule方法的基础控制器。 :::

  1. namespace App\HttpController;
  2. use App\HttpController\Api\BaseController;
  3. use EasySwoole\Validate\Validate;
  4. class Common extends BaseController
  5. {
  6. function sms()
  7. {
  8. $phone = $this->request()->getRequestParam('phone');
  9. }
  10. protected function validateRule(?string $action): ?Validate
  11. {
  12. $v = new Validate();
  13. switch ($action){
  14. case 'sms':{
  15. $v->addColumn('phone','手机号')->required('不能为空')->length(11,'长度错误');
  16. $v->addColumn('verifyCode','验证码')->required('不能为空')->length(4,'长度错误');
  17. break;
  18. }
  19. }
  20. return $v;
  21. }
  22. }

::: warning 在需要验证的控制器方法中,我们给对应的action添加对应的校验规则,即可实现自动校验,这样控制器方法即可安心实现逻辑。 :::

方法列表

获取Error:

  1. function getError():?EasySwoole\Validate\Error

给字段添加规则:

1.1.9版本到目前

  • string name 字段key
  • string alias 别名
  • string reset 重置规则
  1. public function addColumn(string $name, ?string $alias = null,bool $reset = false):EasySwoole\Validate\Rule

1.1.0版本到1.1.8版本

  • string name 字段key
  • string alias 别名
  1. public function addColumn(string $name, ?string $alias = null):EasySwoole\Validate\Rule

1.0.1版本

  • string name 字段key
  • string errorMsg 错误信息
  • string alias 别名
  1. public function addColumn(string $name,?string $errorMsg = null,?string $alias = null):EasySwoole\Validate\Rule

1.0.0版本

  • string name 字段key
  • string alias 别名
  • string errorMsg 错误信息
  1. public function addColumn(string $name,?string $alias = null,?string $errorMsg = null):EasySwoole\Validate\Rule

返回一个Rule对象可以添加自定义规则。

数据验证:

  • array data 数据
  1. function validate(array $data)

验证规则类

目前验证器支持的规则如下

  1. namespace EasySwoole\Validate;
  2. /**
  3. * 校验规则
  4. * 请以首字母排序校验方法以便后期维护
  5. * Class Rule
  6. * @package EasySwoole\Validate
  7. */
  8. class Rule
  9. {
  10. protected $ruleMap = [];
  11. function getRuleMap(): array
  12. {
  13. return $this->ruleMap;
  14. }
  15. /**
  16. * 给定的URL是否可以成功通讯
  17. * @param null|string $msg
  18. * @return $this
  19. */
  20. function activeUrl($msg = null)
  21. {
  22. $this->ruleMap['activeUrl'] = [
  23. 'arg' => null,
  24. 'msg' => $msg
  25. ];
  26. return $this;
  27. }
  28. /**
  29. * 给定的参数是否是字母 即[a-zA-Z]
  30. * @param null|string $msg
  31. * @return $this
  32. */
  33. function alpha($msg = null)
  34. {
  35. $this->ruleMap['alpha'] = [
  36. 'arg' => null,
  37. 'msg' => $msg
  38. ];
  39. return $this;
  40. }
  41. function alphaNum($msg = null)
  42. {
  43. $this->ruleMap['alphaNum'] = [
  44. 'arg' => null,
  45. 'msg' => $msg
  46. ];
  47. return $this;
  48. }
  49. function alphaDash($msg = null)
  50. {
  51. $this->ruleMap['alphaDash'] = [
  52. 'arg' => null,
  53. 'msg' => $msg
  54. ];
  55. return $this;
  56. }
  57. /**
  58. * 给定的参数是否在 $min $max 之间
  59. * @param integer $min 最小值 不包含该值
  60. * @param integer $max 最大值 不包含该值
  61. * @param null|string $msg
  62. * @return $this
  63. */
  64. function between($min, $max, $msg = null)
  65. {
  66. $this->ruleMap['between'] = [
  67. 'msg' => $msg,
  68. 'arg' => [
  69. $min, $max
  70. ]
  71. ];
  72. return $this;
  73. }
  74. /**
  75. * 给定参数是否为布尔值
  76. * @param null|string $msg
  77. * @return $this
  78. */
  79. function bool($msg = null)
  80. {
  81. $this->ruleMap['bool'] = [
  82. 'msg' => $msg,
  83. 'arg' => null
  84. ];
  85. return $this;
  86. }
  87. /**
  88. * 给定参数是否为小数格式
  89. * @param null|integer $precision 规定小数点位数 null 为不规定
  90. * @param null $msg
  91. * @return $this
  92. */
  93. function decimal(?int $precision = null, $msg = null)
  94. {
  95. $this->ruleMap['decimal'] = [
  96. 'msg' => $msg,
  97. 'arg' => $precision
  98. ];
  99. return $this;
  100. }
  101. /**
  102. * 给定参数是否在某日期之前
  103. * @param null|string $date
  104. * @param null|string $msg
  105. * @return $this
  106. */
  107. function dateBefore(?string $date = null, $msg = null)
  108. {
  109. $this->ruleMap['dateBefore'] = [
  110. 'msg' => $msg,
  111. 'arg' => $date
  112. ];
  113. return $this;
  114. }
  115. /**
  116. * 给定参数是否在某日期之后
  117. * @param null|string $date
  118. * @param null|string $msg
  119. * @return $this
  120. */
  121. function dateAfter(?string $date = null, $msg = null)
  122. {
  123. $this->ruleMap['dateAfter'] = [
  124. 'msg' => $msg,
  125. 'arg' => $date
  126. ];
  127. return $this;
  128. }
  129. /**
  130. * 验证值是否相等
  131. * @param $compare
  132. * @param null|string $msg
  133. * @return $this
  134. */
  135. function equal($compare, $msg = null)
  136. {
  137. $this->ruleMap['equal'] = [
  138. 'msg' => $msg,
  139. 'arg' => $compare
  140. ];
  141. return $this;
  142. }
  143. /**
  144. * 验证值是否一个浮点数
  145. * @param null|string $msg
  146. * @return $this
  147. */
  148. function float($msg = null)
  149. {
  150. $this->ruleMap['float'] = [
  151. 'arg' => null,
  152. 'msg' => $msg
  153. ];
  154. return $this;
  155. }
  156. /**
  157. * 调用自定义的闭包验证
  158. * @param callable $func
  159. * @param null|string $msg
  160. * @return $this
  161. */
  162. function func(callable $func, $msg = null)
  163. {
  164. $this->ruleMap['func'] = [
  165. 'arg' => $func,
  166. 'msg' => $msg
  167. ];
  168. return $this;
  169. }
  170. /**
  171. * 值是否在数组中
  172. * @param array $array
  173. * @param bool $isStrict
  174. * @param null|string $msg
  175. * @return $this
  176. */
  177. function inArray(array $array, $isStrict = false, $msg = null)
  178. {
  179. $this->ruleMap['inArray'] = [
  180. 'arg' => [ $array, $isStrict ],
  181. 'msg' => $msg
  182. ];
  183. return $this;
  184. }
  185. /**
  186. * 是否一个整数值
  187. * @param null|string $msg
  188. * @return $this
  189. */
  190. function integer($msg = null)
  191. {
  192. $this->ruleMap['integer'] = [
  193. 'arg' => null,
  194. 'msg' => $msg
  195. ];
  196. return $this;
  197. }
  198. /**
  199. * 是否一个有效的IP
  200. * @param array $array
  201. * @param null $msg
  202. * @return $this
  203. */
  204. function isIp($msg = null)
  205. {
  206. $this->ruleMap['isIp'] = [
  207. 'arg' => null,
  208. 'msg' => $msg
  209. ];
  210. return $this;
  211. }
  212. /**
  213. * 是否不为空
  214. * @param null $msg
  215. * @return $this
  216. */
  217. function notEmpty($msg = null)
  218. {
  219. $this->ruleMap['notEmpty'] = [
  220. 'arg' => null,
  221. 'msg' => $msg
  222. ];
  223. return $this;
  224. }
  225. /**
  226. * 是否一个数字值
  227. * @param null $msg
  228. * @return $this
  229. */
  230. function numeric($msg = null)
  231. {
  232. $this->ruleMap['numeric'] = [
  233. 'arg' => null,
  234. 'msg' => $msg
  235. ];
  236. return $this;
  237. }
  238. /**
  239. * 不在数组中
  240. * @param array $array
  241. * @param bool $isStrict
  242. * @param null $msg
  243. * @return $this
  244. */
  245. function notInArray(array $array, $isStrict = false, $msg = null)
  246. {
  247. $this->ruleMap['notInArray'] = [
  248. 'arg' => [ $array, $isStrict ],
  249. 'msg' => $msg
  250. ];
  251. return $this;
  252. }
  253. /**
  254. * 验证数组或字符串的长度
  255. * @param int $len
  256. * @param null $msg
  257. * @return $this
  258. */
  259. function length(int $len, $msg = null)
  260. {
  261. $this->ruleMap['length'] = [
  262. 'msg' => $msg,
  263. 'arg' => $len
  264. ];
  265. return $this;
  266. }
  267. /**
  268. * 验证数组或字符串的长度是否超出
  269. * @param int $lengthMax
  270. * @param null $msg
  271. * @return $this
  272. */
  273. function lengthMax(int $lengthMax, $msg = null)
  274. {
  275. $this->ruleMap['lengthMax'] = [
  276. 'msg' => $msg,
  277. 'arg' => $lengthMax
  278. ];
  279. return $this;
  280. }
  281. /**
  282. * 验证数组或字符串的长度是否达到
  283. * @param int $lengthMin
  284. * @param null $msg
  285. * @return $this
  286. */
  287. function lengthMin(int $lengthMin, $msg = null)
  288. {
  289. $this->ruleMap['lengthMin'] = [
  290. 'msg' => $msg,
  291. 'arg' => $lengthMin
  292. ];
  293. return $this;
  294. }
  295. /**
  296. * 验证数组或字符串的长度是否在一个范围内
  297. * @param null $msg
  298. * @return $this
  299. */
  300. function betweenLen(int $min, int $max, $msg = null)
  301. {
  302. $this->ruleMap['betweenLen'] = [
  303. 'msg' => $msg,
  304. 'arg' => [
  305. $min,
  306. $max
  307. ]
  308. ];
  309. return $this;
  310. }
  311. /**
  312. * 验证值不大于(相等视为不通过)
  313. * @param int $max
  314. * @param null|string $msg
  315. * @return Rule
  316. */
  317. function max(int $max, ?string $msg = null): Rule
  318. {
  319. $this->ruleMap['max'] = [
  320. 'arg' => $max,
  321. 'msg' => $msg
  322. ];
  323. return $this;
  324. }
  325. /**
  326. * 验证值不小于(相等视为不通过)
  327. * @param int $min
  328. * @param null|string $msg
  329. * @return Rule
  330. */
  331. function min(int $min, ?string $msg = null): Rule
  332. {
  333. $this->ruleMap['min'] = [
  334. 'arg' => $min,
  335. 'msg' => $msg
  336. ];
  337. return $this;
  338. }
  339. /**
  340. * 验证值是合法的金额
  341. * 100 | 100.1 | 100.01
  342. * @param integer|null $precision 小数点位数
  343. * @param string|null $msg
  344. * @return Rule
  345. */
  346. function money(?int $precision = null, string $msg = null): Rule
  347. {
  348. $this->ruleMap['money'] = [
  349. 'arg' => $precision,
  350. 'msg' => $msg
  351. ];
  352. return $this;
  353. }
  354. /**
  355. * 设置值为可选参数
  356. * @return $this
  357. */
  358. function optional()
  359. {
  360. $this->ruleMap['optional'] = [
  361. 'arg' => null,
  362. 'msg' => null
  363. ];
  364. return $this;
  365. }
  366. /**
  367. * 正则表达式验证
  368. * @param $reg
  369. * @param null $msg
  370. * @return $this
  371. */
  372. function regex($reg, $msg = null)
  373. {
  374. $this->ruleMap['regex'] = [
  375. 'arg' => $reg,
  376. 'msg' => $msg
  377. ];
  378. return $this;
  379. }
  380. /**
  381. * 必须存在值
  382. * @param null $msg
  383. * @return $this
  384. */
  385. function required($msg = null)
  386. {
  387. $this->ruleMap['required'] = [
  388. 'arg' => null,
  389. 'msg' => $msg
  390. ];
  391. return $this;
  392. }
  393. /**
  394. * 值是一个合法的时间戳
  395. * @param null $msg
  396. * @return $this
  397. */
  398. function timestamp($msg = null)
  399. {
  400. $this->ruleMap['timestamp'] = [
  401. 'arg' => null,
  402. 'msg' => $msg
  403. ];
  404. return $this;
  405. }
  406. /**
  407. * 时间戳在某指定日期之前
  408. * @param string $date 传入任意可被strtotime解析的字符串
  409. * @param null $msg
  410. * @return $this
  411. */
  412. function timestampBeforeDate($date, $msg = null)
  413. {
  414. $this->ruleMap['timestampBeforeDate'] = [
  415. 'arg' => $date,
  416. 'msg' => $msg
  417. ];
  418. return $this;
  419. }
  420. /**
  421. * 时间戳在某指定日期之后
  422. * @param string $date 传入任意可被strtotime解析的字符串
  423. * @param null $msg
  424. * @return $this
  425. */
  426. function timestampAfterDate($date, $msg = null)
  427. {
  428. $this->ruleMap['timestampAfterDate'] = [
  429. 'arg' => $date,
  430. 'msg' => $msg
  431. ];
  432. return $this;
  433. }
  434. /**
  435. * 指定时间戳在某时间戳之前
  436. * @param string|integer $beforeTimestamp 在该时间戳之前
  437. * @param null $msg
  438. * @return $this
  439. */
  440. function timestampBefore($beforeTimestamp, $msg = null)
  441. {
  442. $this->ruleMap['timestampBefore'] = [
  443. 'arg' => $beforeTimestamp,
  444. 'msg' => $msg
  445. ];
  446. return $this;
  447. }
  448. /**
  449. * 指定时间戳在某时间戳之后
  450. * @param string|integer $afterTimestamp 在该时间戳之后
  451. * @param null $msg
  452. * @return $this
  453. */
  454. function timestampAfter($afterTimestamp, $msg = null)
  455. {
  456. $this->ruleMap['timestampAfter'] = [
  457. 'arg' => $afterTimestamp,
  458. 'msg' => $msg
  459. ];
  460. return $this;
  461. }
  462. /**
  463. * 值是一个合法的链接
  464. * @param null $msg
  465. * @return $this
  466. */
  467. function url($msg = null)
  468. {
  469. $this->ruleMap['url'] = [
  470. 'arg' => null,
  471. 'msg' => $msg
  472. ];
  473. return $this;
  474. }
  475. /**
  476. * 值是一个合法的链接
  477. * @param null $msg
  478. * @return $this
  479. */
  480. function allDigital($msg = null)
  481. {
  482. $this->ruleMap['allDigital'] = [
  483. 'arg' => null,
  484. 'msg' => $msg
  485. ];
  486. return $this;
  487. }
  488. }