Skip to content

Validator to detect untrimmed whitespace in stringsΒ #1696

@Niels-NTG

Description

@Niels-NTG

Is your feature request related to a problem?
Recently some users of the software I work on had a bug caused by dirty imported data, where some string values had trailing spaces. While the data did went to our Respect-powered validator system, we neglected to check for trailing spaces.

My estimate is that more often than not string values without leading and trailing whitespace is desired over strings that have it. Therefore it would be helpful if Respect Validation would offer a convenient validator function for it.

Describe the solution
A new validator function called HasNoUntrimmedSpaces() which passes if the input contains no whitespace characters on neither the start or end of the input string value. The definition of whitespace is the same as what the Spaced() validator uses (spaces, tabs, line breaks).

For example:

v::hasNoUntrimmedSpaces()->assert('username');
// Validation passes
v::hasNoUntrimmedSpaces()->assert('');
// Validation passes
v::hasNoUntrimmedSpaces()->assert('username ');
// Validation fails
v::hasNoUntrimmedSpaces()->assert(' ');
// Validation fails

I'm open for ideas for a better and snappier name than HasNoUnrimmedSpaces() πŸ˜‰ .

Alternatives
Use the existing Regex(string $regex) function like so:

v::regex('/^\S*$/')->assert($inputValue)

or a chain of negated StartsWith and EndsWith functions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions