Skip to content

Commit

Permalink
Added StringValidator.doesNotContainWhitespace()
Browse files Browse the repository at this point in the history
  • Loading branch information
cowwoc committed Aug 31, 2024
1 parent f4f04e4 commit f1ba29a
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 52 deletions.
1 change: 1 addition & 0 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ https://github.com/cowwoc/requirements.java/commits/master for a full list.
design for this yet.
* New features:
* Added `validationFailed()` and `getValueOrDefault()` to all validators.
* Added `StringValidator.doesNotContainWhitespace()`.
* Added `StringValidator.matches(regex)`.
* Replaced `ClassValidator.isSupertypeOf()`, `isSubtypeOf()` with `Type.isSupertypeOf()`.
* Improvements
Expand Down
2 changes: 2 additions & 0 deletions src/internal/internal.mts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ import {
stringDoesNotEndWith,
stringContains,
stringDoesNotContain,
stringDoesNotContainWhitespace,
stringMatches
} from "./message/StringMessages.mjs";
import {
Expand Down Expand Up @@ -356,6 +357,7 @@ export
stringDoesNotEndWith,
stringContains,
stringDoesNotContain,
stringDoesNotContainWhitespace,
stringMatches,
Difference,
ObjectAndSize,
Expand Down
16 changes: 16 additions & 0 deletions src/internal/message/StringMessages.mts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,21 @@ function stringDoesNotContain(validator: AbstractValidator<unknown, string>, unw
return messageBuilder;
}

/**
* @param validator - the validator
* @returns a message for the validation failure
*/
function stringDoesNotContainWhitespace(validator: AbstractValidator<unknown, string>)
{
const name = validator.getName();
const messageBuilder = new MessageBuilder(validator,
`${MessageBuilder.quoteName(name)} may not contain whitespace.`);
const value = validator.getValueOrDefault(null);
if (value !== null)
messageBuilder.withContext(value, name);
return messageBuilder;
}

/**
* @param validator - the validator
* @param regex - the regular expression
Expand Down Expand Up @@ -194,5 +209,6 @@ export
stringDoesNotEndWith,
stringContains,
stringDoesNotContain,
stringDoesNotContainWhitespace,
stringMatches
};
2 changes: 1 addition & 1 deletion src/internal/message/diff/ContextGenerator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ expectedName: ${expectedName}`);

/**
* @returns the difference between the expected and actual values
* @throws TypeError if `actualName` or `expectedName` are `null`
* @throws TypeError if `actualName` or `expectedName` are `undefined` or `null`
*/
private compareTypes(): MessageSection[]
{
Expand Down
2 changes: 1 addition & 1 deletion src/internal/message/diff/DiffResult.mts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DiffResult
* @param diffLines - the difference between the actual and expected values (empty list if omitted)
* @param expectedLines - the lines of the expected string
* @param equalLines - indicates if the actual and expected values are equal for each line
* @throws TypeError if any of the arguments are `null`
* @throws TypeError if any of the arguments are `undefined` or `null`
*/
constructor(actualLines: string[], diffLines: string[], expectedLines: string[], equalLines: boolean[])
{
Expand Down
2 changes: 1 addition & 1 deletion src/internal/scope/AbstractApplicationScope.mts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ abstract class AbstractApplicationScope implements ApplicationScope
*
* @param parent - the parent scope
* @param globalConfiguration - the global configuration
* @throws TypeError if any of the arguments are `null`
* @throws TypeError if any of the arguments are `undefined` or `null`
*/
protected constructor(parent: ProcessScope, globalConfiguration: GlobalConfiguration)
{
Expand Down
2 changes: 1 addition & 1 deletion src/internal/validator/AbstractValidators.mts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ abstract class AbstractValidators<S> implements Validators<S>
*
* @param scope - the application configuration
* @param configuration - the configuration to use for new validators
* @throws TypeError if any of the arguments are `null`
* @throws TypeError if any of the arguments are `undefined` or `null`
*/
protected constructor(scope: ApplicationScope, configuration: Configuration)
{
Expand Down
17 changes: 15 additions & 2 deletions src/internal/validator/StringValidatorImpl.mts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
stringMatches,
stringIsTrimmed,
objectIsEmpty,
objectIsNotEmpty
objectIsNotEmpty,
stringDoesNotContainWhitespace
} from "../internal.mjs";


Expand Down Expand Up @@ -73,7 +74,7 @@ class StringValidatorImpl extends AbstractValidator<StringValidator, string>
{
if (this.value.isNull())
this.onNull();
if (this.value.validationFailed(v => !/^\s|\s$/.test(v)))
if (this.value.validationFailed(v => v != null && !/^\s|\s$/.test(v)))
{
this.addRangeError(
stringIsTrimmed(this).toString());
Expand Down Expand Up @@ -153,6 +154,18 @@ class StringValidatorImpl extends AbstractValidator<StringValidator, string>
return this;
}

doesNotContainWhitespace(): StringValidator
{
if (this.value.isNull())
this.onNull();
if (this.value.validationFailed(v => v != null && !/\s/.test(v)))
{
this.addRangeError(
stringDoesNotContainWhitespace(this).toString());
}
return this;
}

matches(regex: RegExp): StringValidator
{
if (this.value.isNull())
Expand Down
7 changes: 5 additions & 2 deletions src/internal/validator/ValidationFailureImpl.mts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ class ValidationFailureImpl implements ValidationFailure
* @param configuration - the validator's configuration
* @param message - the failure message
* @param errorBuilder - returns the error associated with the failure message
* @throws AssertionError if any of the arguments are `null`. If the error message contains leading or
* trailing whitespace, or is empty.
* @throws AssertionError if:
* <ul>
* <li>Any of the arguments are `undefined` or `null`.</li>
* <li>The error message contains leading or trailing whitespace, or is empty.</li>
* </ul>
*/
public constructor(configuration: Configuration, message: string, errorBuilder: ErrorBuilder)
{
Expand Down
2 changes: 1 addition & 1 deletion src/validator/ArrayValidator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface ArrayValidator<E> extends
* @param comparator - a function that returns a negative number if `first` should come
* before `second`, zero or `NaN` if the two values are equal, or a positive number
* if `first` should come after `second`.
* @throws TypeError if the value or `comparator` are `null`
* @throws TypeError if the value or `comparator` are `undefined` or `null`
* @throws RangeError if the collection is not sorted
* @returns this
*/
Expand Down
21 changes: 15 additions & 6 deletions src/validator/StringValidator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface StringValidator extends ValidatorComponent<StringValidator, string>
*
* @param prefix - the value that the string must start with
* @returns this
* @throws TypeError if the value or `prefix` are `null`
* @throws TypeError if the value or `prefix` are `undefined` or `null`
* @throws RangeError if the value does not start with `prefix`
*/
startsWith(prefix: string): StringValidator;
Expand All @@ -23,7 +23,7 @@ interface StringValidator extends ValidatorComponent<StringValidator, string>
*
* @param prefix - the value that the string may not start with
* @returns this
* @throws TypeError if the value or `prefix` are `null`
* @throws TypeError if the value or `prefix` are `undefined` or `null`
* @throws RangeError if the value starts with `prefix`
*/
doesNotStartWith(prefix: string): StringValidator;
Expand All @@ -33,7 +33,7 @@ interface StringValidator extends ValidatorComponent<StringValidator, string>
*
* @param suffix - the value that the string must end with
* @returns this
* @throws TypeError if the value or `suffix` are `null`
* @throws TypeError if the value or `suffix` are `undefined` or `null`
* @throws RangeError if the value does not end with `suffix`
*/
endsWith(suffix: string): StringValidator;
Expand All @@ -43,7 +43,7 @@ interface StringValidator extends ValidatorComponent<StringValidator, string>
*
* @param suffix - the value that the string may not end with
* @returns this
* @throws TypeError if the value or `suffix` are `null`
* @throws TypeError if the value or `suffix` are `undefined` or `null`
* @throws RangeError if the value ends with `suffix`
*/
doesNotEndWith(suffix: string): StringValidator;
Expand All @@ -53,7 +53,7 @@ interface StringValidator extends ValidatorComponent<StringValidator, string>
*
* @param expected - the string that the value must contain
* @returns this
* @throws TypeError if the value or `expected` are `null`
* @throws TypeError if the value or `expected` are `undefined` or `null`
* @throws RangeError if the value does not contain `expected`
*/
contains(expected: string): StringValidator;
Expand All @@ -63,11 +63,20 @@ interface StringValidator extends ValidatorComponent<StringValidator, string>
*
* @param unwanted - the string that the value may not contain
* @returns this
* @throws TypeError if the value or `unwanted` are `null`
* @throws TypeError if the value or `unwanted` are `undefined` or `null`
* @throws RangeError if the value contains `unwanted`
*/
doesNotContain(unwanted: string): StringValidator;

/**
* Ensures that the value does not contain whitespace characters.
*
* @return this

Check warning on line 74 in src/validator/StringValidator.mts

View workflow job for this annotation

GitHub Actions / Build

tsdoc-undefined-tag: The TSDoc tag "@return" is not defined in this configuration
* @throws NullPointerException if the value is `undefined` or `null`
* @throws IllegalArgumentException if the value contains whitespace characters
*/
doesNotContainWhitespace(): StringValidator;

/**
* Ensures that the value matches a regular expression.
*
Expand Down
Loading

0 comments on commit f1ba29a

Please sign in to comment.