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

add notBlank and notEmpty as string validators #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

marlon-sousa
Copy link

In this pr we create two validators fopr strings

  • notBlank will make sure that string is not empty "" and that it contains at least one non white space character
  • notEmpty will make sure that string is not empty "".

Why

Because it is kit common when validating request schemas that a given field, if it exists, cannot be blank or empty.

@dominikzogg
Copy link
Member

I want to keep this package as lean as possible. Both of your suggestion can be done by the comments i provide.
Feel free to adapt the PR to add example in the readme instead.

@marlon-sousa
Copy link
Author

Hello,

I might have failed to find your comments.

Where are they listed?

As of today, I needed to write the post parsers by hand. While this can for sure be done, blank and empty operations are common enough to be included, the same way the email validator could easily be remobed and the match validator with a email regexp could be used instead.

Although you have of course full freedom to decide what stays in or what stays out, empty and blank validators are far from being a corner user case, and they are present in other libraries.

It was a good exercise anyways. Feel free to accept or reject it at your disposal, but the needs are clear.

Also, respect validation might not be always available for use with this package.

Thanks for the great work!

@dominikzogg
Copy link
Member

dominikzogg commented Oct 8, 2024

Hi @marlon-sousa

You could do the following in your app:

$notBlankSchema = $schema->match('/^[\s]*$/');
$notEmptySchema = $schema->minLength(1);

Best,
Dominik

@marlon-sousa
Copy link
Author

As far as I understand

$notBlankSchema = $schema->match('/^[\s]*$/');

will parse only blank strings, which is definitely not what a NotBlancSchema should be doing.

It seems though that the correct way of handling this would be

$notBlankSchema = $schema->trim()->minSize(1);

Anyways, People coming here with same questions will be tricked into something they likely don't want.

I personally am going with another regex assuring that one or more characters are present and that none of them is a space like character. This fortunately works for me, but won't work for people accepting strings which can contain spaces, but which cannot be blank or empty.

I still think these two patterns are common enough in the validation field to be included, but wanted to document how to follow consistently from here if they are not included.

I would suggest you to write these typs on documentation, because this need is very common.

@dominikzogg
Copy link
Member

@marlon-sousa i copied the wrong regex from your PR, it was only mean't to describe how it already could be done as similar as you suggested.

$notBlankSchema = $schema->trim()->minSize(1);

is much better, i agree.

@dominikzogg
Copy link
Member

dominikzogg commented Oct 31, 2024

@marlon-sousa it would be great if you change your PR to update your example to the readme and drop the code changes.

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.

2 participants