-
Notifications
You must be signed in to change notification settings - Fork 774
Description
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 failsI'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.