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

[RFC] Multiple validation messages #1

Merged

Conversation

przemyslaw-przylucki
Copy link
Contributor

@przemyslaw-przylucki przemyslaw-przylucki commented May 5, 2024

Addresses: tempestphp/tempest-framework#266

This PR introduces changes to the return type for Rules - now they can return either a string or an array of strings.

When array is returned, messages will be joined using naturalLangJoin on a new class - StringHelper, alternatively, this could be a join method on LangHelper - lemme know which one do you prefer.

That method converts

        $messages = [
            "Value should contain at least {$this->min} characters",
            "at least one number",
            'at least one uppercase and one lowercase letter'
        ];

to

Value should contain at least 10 characters, at least one number and at least one uppercase and one lowercase letter.

When string is returned, the message won't be modified.

Some things to consider:

  1. Now that the Rule can return either array of strings, or just string, we could either:
  • rename the message method to messages and only allow arrays to be returned, then join them
  • keep the message method as is (went this route because it's the least breaking change, I don't have a strong preference)
  1. Most of rules start with Value should - maybe we can add that prefix in the exception itself? It'd be especially useful if we went the "messages + array" route, because adding that to the first element would be awkward

@coveralls
Copy link

coveralls commented May 24, 2024

Pull Request Test Coverage Report for Build 9308577741

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 7 of 7 (100.0%) changed or added relevant lines in 3 files are covered.
  • 28 unchanged lines in 6 files lost coverage.
  • Overall coverage increased (+1.7%) to 86.489%

Files with Coverage Reduction New Missed Lines %
src/Discovery/InitializerDiscovery.php 1 94.44%
src/Container/Exceptions/CannotInstantiateDependencyException.php 3 0.0%
src/Container/GenericContainer.php 5 96.55%
src/Environment.php 5 0.0%
src/Container/Dependency.php 7 89.06%
src/functions.php 7 58.82%
Totals Coverage Status
Change from base Build 8923671835: 1.7%
Covered Lines: 1069
Relevant Lines: 1236

💛 - Coveralls


namespace Tempest\Support;

final class StringHelper
Copy link
Member

Choose a reason for hiding this comment

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

I actually think I like the idea of LanguageHelper::join more.

@brendt
Copy link
Member

brendt commented May 30, 2024

I know this has been open for a while, but I like it 👍 One small remark, afterwards we can merge this :)

@przemyslaw-przylucki
Copy link
Contributor Author

Oke doke, should be all good now!

@brendt brendt merged commit 83288d8 into tempestphp:main May 31, 2024
4 checks passed
@brendt
Copy link
Member

brendt commented May 31, 2024

There we go :)

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

Successfully merging this pull request may close these issues.

3 participants