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

添加setDefault,用来设置默认值,当有些字段不需要可填可不填,或者该字段用于某个控制器的固定值时可使用->setDefault() #1

Merged
merged 105 commits into from Sep 9, 2023

Conversation

596868636
Copy link

@596868636 596868636 commented Sep 9, 2023

PR需求来源:https://www.workerman.net/q/11421

用法
$data = v::input($request->post(), [
'app_id' => v::alnum()->length(1, 64)->setName('用户名')->setDefault("toadmin"),
'app_id1' => v::alnum()->length(1, 64)->setName('用户名')->setDefault("toadmin",true),
'xxx' => v::optional(v::length(5, 64))->setName('其它'),
'xxx1' => v::length(5, 64)->setDefault('其它111')
]);

使用setDefault后,同时可代替v::optional用于验证一些字段可填可不填字段中,同时可以使用setDefaule第二个参数为true即可设置为强制默认值,而不被前端传输的参数替换,比如app_id1,我需要固定为“toadmin”,而不被前端传输app_id1为touser而覆盖。

而验证后的变量 $data 即可传输到service等层进行业务处理

同时合并了Respect/validation 最新的2.3版本
/
Validation

TheRespectPanda and others added 30 commits August 23, 2020 00:24
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit will also remove Travis and Scrutinizer and will configure
Codecov as a code coverage tool.

A few changes in the PHPUnit configuration already had to be made
before, but became more visible now. They're along with this commit.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
We already supported PHP 8.0 as our constrains in the "composer.json"
file was ">=7.3", but we were not testing it before.

Because of that, I found a bug on "EndsWith" which is fixed now.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
The third argument of "filter_var" must be either an integer or an
array. On PHP 8 this "FilterVar" rule fails because we always pass that
argument, even if it is null.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Although BCMath is necessary to execute "Ip" and "Iban" rules, we not
required that in the "composer.json" file. That's because if someone
wants to use the library, but doesn't want to use those rules, they
would not need to install that extension.

However, when executing the tests, they will break. This commit will
verify whether the extension exists to test "Ip" and "Iban" rules.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
After the refactoring on the Factory class [1], to throw exceptions of a
specific rule, it is necessary to add the exception namespace of that
rule. That change makes sense when someone wants to create rules from
the Validator class, but when using rules as classes, it's not as handy.

This commit will auto-resolve exception based on the rule namespace,
just as it used to be.

[1]: 1f217dd

Co-authored-by: Casey McLaughlin <caseyamcl@gmail.com>
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Most Validation errors are sent to Users/Visitors or Clients and as such
might not need to know it was a Key their inputs are being validated
upon.

Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
The PSR-7 has two interfaces that allow us to validate them as files.
This commit will allow some rules to validate those interfaces.

Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
One of the reasons for this change is to spare some space on the
Validator class, leaving only real code there. However, the biggest
reason is that IDEs can auto-complete rules after Validator creates the
first rule.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit will fix an issue but ignore two of them because it will
require some refactoring that won't be possible in a PATCH version.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
We have three workflows that automatically update the code. They are
using version 2.0 as a base to create their changes. We need to update
that because the last supported version is 2.1.

Since I do not think it is a good idea to change code every time a new
MINOR version is released, I moved that value to a repository secret.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Inside the "data/" directory, we have files with lists of subdivisions
that need to be updated. We have to update them manually, or we automate
that task with a script and GitHub actions.

The two options are very time consuming and also not ideal. We don't
want to deal with that problem and, thinking that the user of this
library may want to show the data that we validate, we should create a
whole library to make it more usable.

The "sokil/php-isocodes" is a simple library that, even supports
translations. It's frequently updated and has gone to major performance
updates.

I am not fond of the idea of requiring an external library to install
Validation, as I have seen that gone wrong before [1]. Ideally, that
would be an optional dependency for people who would like to use those
rules, but to make that happen, we need to release a MAJOR version.

[1]: d072b4d

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Since "sokil/php-isocodes" is a dependency of our repository already, it
makes sense to use it as a source of currency codes instead of keeping a
list of currencies ourselves.

By using that library, we can also validate currency codes using
different sets.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Since we already have that library as a dependency of our repository, it
makes sense to use it as a source of language codes instead of keeping a
list of currencies ourselves.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Since we already have that library as a dependency of our repository, it
makes sense to use it as a source of country codes instead of keeping a
list of currencies ourselves.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
We could simply add version 3.0 instead of replacing it, but I would
like to stimulate people to keep their libraries up-to-date.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
The functions from the mbstring can deal find with strings without
forcing an specific encoding. However, sometimes "mb_detect_encoding()"
cannot identify the encoding therefore the functions that expect a valid
encoding will trigger a PHP error.

This commit will remove the unnecessary use of "mb_detect_encoding()."

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
alganet and others added 28 commits February 19, 2023 00:19
 - List will be auto-updated from https://publicsuffix.org/list/public_suffix_list.dat
 - Updated AbstractSearcher rules to be case insensitive
 - Updated PR creator bots
 - Docs and tests
 - For this particular updater, a list of exceptions to the rules
   downloaded by geonames is included in POSTAL_CODES_EXTRA, for
   cases in which we seem to do better than geonames itself based
   on previous user reports.
 - Added an option to also validate formatting of the postal codes.
 - Combined multiple PR bots into a single one.
Co-authored-by: The Respect Panda <therespectpanda@gmail.com>
The "bic" rule has been removed, and two more rules were added.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
- Add test for null value in the Cnpj rule.
- Add UploadedFileInterface object test for Size rule.
- Add test for invalid values in Sorted rule.
Co-authored-by: The Respect Panda <therespectpanda@gmail.com>
When I created that rule, I barely spoke English.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Most exception messages in Validation use "must" and "must not" in their
templates, but a few rules don't.

I fixed most of them, but AlwaysValid and AlwaysInvalid remain because I
wonder if they will be better if I update them.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
The integration tests use the same pattern to test exception messages.
With my changes, we won't validate which exception we throw in those
tests, but matching the message is enough. I created three functions to
replace most of those tests.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
We had a method that returned the full path of the fixture directory,
and we frequently would concatenate that path with a file we needed. I
changed it to include the file's path inside the fixture directory. That
way, we avoid repeating the same patter over and over.

I made the method static because we use it in data providers, which need
to be static.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
When we write tests requiring those interfaces, we create mocks. Those
new stubs will make those tests easier to read and allow us to reduce
the number of mocks we write with PHPUnit, making the code in the tests
a bit less complex.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
With that, the tests will be more straightforward, and we won't need to
use the test class in the data providers. That will help us later
because, on PHPUnit 10, all data providers need to be static.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
With that, the tests will be more straightforward, and we won't need to
use the test class in the data providers. That will help us later
because, on PHPUnit 10, all data providers need to be static.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
That will help us later because, on PHPUnit 10, all data providers need
to be static.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
We can use the AlwaysValid and AlwaysInvalid rules in the tests instead
of mocking them. Those changes will help us later because we mainly use
the `createValidatableMock()` in the data providers and, as from
PHPUnit 10, all data providers need to be static.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
From PHPUnit 10, all data providers need to be static. This commit will
make migrating from version 9 to 10 a bit easier.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
It's essential to test our abstract classes because users might use
them. However, creating mocks when writing those tests make the code too
complicated.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
# Conflicts:
#	library/Exceptions/ExistsException.php
#	library/Exceptions/FalseValException.php
#	library/Exceptions/KeyException.php
#	library/Exceptions/KeySetException.php
#	library/Exceptions/NoException.php
#	library/Exceptions/PhoneException.php
#	library/Exceptions/SfException.php
#	library/Exceptions/TrueValException.php
#	library/Exceptions/YesException.php
#	library/Rules/AbstractRule.php
#	library/Validator.php
@walkor walkor merged commit 28316bf into walkor:master Sep 9, 2023
@walkor
Copy link
Owner

walkor commented Sep 9, 2023

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet