Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复验证器工具类对小数的验证 #705

Merged
merged 6 commits into from
Jul 31, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions doc/components/validation/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ class TestValidate

整数验证

> 能通过整数验证的数据类型为 `int | float | string`
>
> 对于 `string` 类型可参考[数字字符串](https://www.php.net/manual/zh/language.types.numeric-strings.php)
>
> 例如,类似 `5` 、 `5.0` 、 `'5'` 均属于整数
>
> 特殊的,类似 `'5.0'` 这种**带小数点的数字字符串**不属于整数

验证必须为整数:

`@Integer`
Expand All @@ -203,6 +211,14 @@ class TestValidate

小数验证

> 能通过小数验证的数据类型为 `float | string`
>
> 对于 `string` 类型可参考[数字字符串](https://www.php.net/manual/zh/language.types.numeric-strings.php)
>
> 例如,类似 `5.1` 、 `'5.1'` 、 `'5.0'` 均属于小数
>
> 特殊的,类似 `5.0` 这种**小数部分为0的 `float` 值**不属于小数
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5.0 算小数吧

Copy link
Member Author

@Eno-CN Eno-CN Jul 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5.0 算小数吧

我记得应该是5.0的float值转string后直接就丢失小数点了,然后第二个条件不满足。在整数那里恰好也能过,所以它满足上面提到的整数,而不满足小数的条件。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我觉得这算bug了,应该修复下代码

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

整数和小数这两个的判断都有涉及这个问题,我觉得如果能严格判断数据类型就更好了,比如再给个严格模式的选项。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

严格模式不如直接用 php 自带的函数去验证类型,这里的代码需要修复一下,你有空看下吗

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

严格模式不如直接用 php 自带的函数去验证类型,这里的代码需要修复一下,你有空看下吗

@Yurunsoft 可以的,我这周内提个PR吧

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Yurunsoft
已完成以下修复

  1. 修复小数、整数验证函数对浮点数类型的检查
  2. 修复数值验证函数对整数类型或小数部分为0的浮点数类型的小数精度检查


验证必须为小数:

`@Decimal`
Expand All @@ -227,21 +243,25 @@ class TestValidate

数值验证,允许是整数或者小数

> 能通过数值验证的数据类型为 `int | float | string`
>
> 对于 `string` 类型可参考[数字字符串](https://www.php.net/manual/zh/language.types.numeric-strings.php)

验证必须为数值:

`@Decimal`
`@Number`

验证必须为>=10.24的数值:

`@Decimal(min=10.24)`
`@Number(min=10.24)`

验证必须为<=10.24的数值:

`@Decimal(max=10.24)`
`@Number(max=10.24)`

验证必须为>=1 && <=10.24的数值:

`@Decimal(min=1, max=10.24)`
`@Number(min=1, max=10.24)`

> 传入`1`,结果为`true`
>
Expand Down
Loading