Abstract
Protected
constructorthe application configuration
+the validator configuration
+the name of the value
+the value being validated
+the contextual information set by a parent validator or the user
+the list of validation failures
+Protected
Readonly
_configurationThe validator configuration.
+Protected
Readonly
contextThe contextual information of this validator.
+Protected
Readonly
failuresThe list of validation failures.
+Protected
Readonly
nameThe name of the value.
+Protected
Readonly
scopeThe application configuration.
+Readonly
valueThe value being validated.
+Adds a validation failure and throws an error if the validator is configured to throw an error on +failure.
+a message that explains what went wrong
+creates the error associated with this failure
+Protected
addFacilitates the validation of related properties. For example,
++```ts +requireThat(nameToFrequency, "nameToFrequency"). + and(m => m.size().isPositive()). + and(m => m.keySet().contains("John")); +``` +
+Any changes made during the validation process will impact this validator.
the nested validation
+this
+Returns the array of failed validations.
+an array of failed validations
+Protected
failReturns the contextual information for upcoming validations carried out by this validator. The contextual
+information is a map of key-value pairs that can provide more details about validation failures. For
+example, if the message is "Password may not be empty" and the map contains the key-value pair
+{"username": "john.smith"}
, the error message would be:
Password may not be empty
username: john.smith
+
+
+an unmodifiable map from each entry's name to its value
+the string to prepend to the name if the name is null
+the name of the value
+the string to prepend to the value if the name is null
+a value
+the prefixed name if it is defined; otherwise, the prefixed string representation of the value
+the application configuration
+Returns the value that is being validated.
+the fallback value to use if the value is invalid
+the validated value, or defaultValue
if the value is invalid (e.g. due to dereferencing a
+property of a null
object)
Returns the value that is being validated.
+the fallback value to use if the value is invalid
+the validated value, or defaultValue
if the value is invalid (e.g. due to dereferencing a
+property of a null
object)
Ensures that the object is an instance of a class.
+the desired class
+a validator for an object of the desired class
+Ensures that the object is not an instance of a class.
+this
+Ensures that the value is not null
.
+This method should be used to validate method arguments that are assigned to class fields but not +accessed right away (such as constructor and setter arguments). It should also be used to validate any +method arguments when the validator contains +additional contextual information.
this
+Ensures that the value is not undefined.
++This method should be used to validate method arguments that are assigned to class fields but not +accessed right away (such as constructor and setter arguments). It should also be used to validate any +method arguments when the validator contains +additional contextual information.
this
+Ensures that the value is null
.
this
+Ensures that the value has the specified type.
+the expected type of the value
+this
+Ensures that the value is undefined.
+this
+Protected
requireEnsures that a name does not conflict with other variable names already in use by the validator.
+the name of the parameter
+false
to allow the name to be used even if it conflicts with an
+existing name in the validator context
the internal validator of the name
+Sets the contextual information for upcoming validations.
++This method adds contextual information to error messages. The contextual information is stored as +key-value pairs in a map. Values set by this method override any values that are set using +Validators.withContext. +
+There is no way to remove contextual information from a validator. Thread-level contextual information is +removed automatically.
the value of the entry
+the name of an entry
+this
+Protected
Static
Readonly
VALUE_Abstract
Protected
constructorCreates a new instance.
+the application configuration
+the configuration to use for new validators
+Abstract
copyReturns a new factory instance with an independent configuration. This method is commonly used to inherit +and update contextual information from the original factory before passing it into a nested operation. +For example,
+JavascriptValidators copy = validators.copy();
copy.getContext().put(json.toString(), "json");
nestedOperation(copy);
+
+
+a copy of this factory
+Protected
getReturns the configuration for assertThat
factory methods.
the configuration for assertThat
factory methods
Protected
getReturns the configuration for checkIf
factory methods.
the configuration for checkIf
factory methods
Returns the contextual information inherited by validators created out by this factory. The contextual
+information is a map of key-value pairs that can provide more details about validation failures. For
+example, if the message is "Password may not be empty" and the map contains the key-value pair
+{"username": "john.smith"}
, the error message would be:
Password may not be empty
username: john.smith
+
+
+an unmodifiable map from each entry's name to its value
+Returns the global configuration shared by all validators.
++NOTE: Updating this configuration affects existing and new validators.
the global configuration updater
+the application configuration
+Abstract
removeSet the configuration used by new validators.
+the updated configuration
+Abstract
withSets the contextual information for validators created by this factory.
++This method adds contextual information to error messages. The contextual information is stored as +key-value pairs in a map. Values set by this method may be overridden by +ValidatorComponent.withContext.
the value of the entry
+the name of an entry
+this
+Thrown when a method is invoked at an illegal or inappropriate time.
+Creates a new error.
+an explanation of what went wrong
+The default implementation of JavascriptValidators.
+Creates a new instance of this validator with an independent configuration.
+the application configuration
+the configuration to use for new validators
+Creates a new instance of this validator with an independent configuration.
+the application configuration
+the factory to copy
+Protected
Readonly
contextProtected
Readonly
scopeStatic
Readonly
INTERNALA validator factory that creates validators to check the arguments of validation methods.
+Validates the state of an unknown value or a value that does not have a specialized validator.
++The returned validator captures exceptions on validation failure rather than throwing them immediately. +The exceptions are converted into an AssertionError and can be retrieved or thrown once the +validation completes. Exceptions unrelated to validation failures are thrown immediately.
the value
+Optional
name: stringthe name of the value
+validator for the value
+Validates the state of an array.
++The returned validator captures exceptions on validation failure rather than throwing them immediately. +The exceptions are converted into an AssertionError and can be retrieved or thrown once the +validation completes. Exceptions unrelated to validation failures are thrown immediately.
the type the value
+the type elements in the array
+the value
+Optional
name: stringthe name of the value
+validator for the value
+Validates the state of a boolean.
++The returned validator captures exceptions on validation failure rather than throwing them immediately. +The exceptions are converted into an AssertionError and can be retrieved or thrown once the +validation completes. Exceptions unrelated to validation failures are thrown immediately.
the value
+Optional
name: stringthe name of the value
+validator for the value
+Validates the state of a map.
++The returned validator captures exceptions on validation failure rather than throwing them immediately. +The exceptions are converted into an AssertionError and can be retrieved or thrown once the +validation completes. Exceptions unrelated to validation failures are thrown immediately.
the value
+Optional
name: stringthe name of the value
+validator for the value
+Validates the state of a number.
++The returned validator captures exceptions on validation failure rather than throwing them immediately. +The exceptions are converted into an AssertionError and can be retrieved or thrown once the +validation completes. Exceptions unrelated to validation failures are thrown immediately.
the value
+Optional
name: stringthe name of the value
+validator for the value
+Validates the state of a set.
++The returned validator captures exceptions on validation failure rather than throwing them immediately. +The exceptions are converted into an AssertionError and can be retrieved or thrown once the +validation completes. Exceptions unrelated to validation failures are thrown immediately.
the value
+Optional
name: stringthe name of the value
+validator for the value
+Validates the state of a string.
++The returned validator captures exceptions on validation failure rather than throwing them immediately. +The exceptions are converted into an AssertionError and can be retrieved or thrown once the +validation completes. Exceptions unrelated to validation failures are thrown immediately.
the value
+Optional
name: stringthe name of the value
+validator for the value
+Validates the state of an unknown value or a value that does not have a specialized validator.
++The returned validator throws an error immediately if a validation fails.
the value
+Optional
name: stringthe name of the value
+validator for the value
+Validates the state of an array.
++The returned validator throws an error immediately if a validation fails.
the type the value
+the type elements in the array
+the value
+Optional
name: stringthe name of the value
+validator for the value
+Validates the state of a boolean.
++The returned validator throws an error immediately if a validation fails.
the value
+Optional
name: stringthe name of the value
+validator for the value
+Validates the state of a map.
++The returned validator throws an error immediately if a validation fails.
the value
+Optional
name: stringthe name of the value
+validator for the value
+Validates the state of a number.
++The returned validator throws an error immediately if a validation fails.
the value
+Optional
name: stringthe name of the value
+validator for the value
+Validates the state of a set.
++The returned validator throws an error immediately if a validation fails.
the value
+Optional
name: stringthe name of the value
+validator for the value
+Validates the state of a string.
++The returned validator throws an error immediately if a validation fails.
the value
+Optional
name: stringthe name of the value
+validator for the value
+Returns a new factory instance with an independent configuration. This method is commonly used to inherit +and update contextual information from the original factory before passing it into a nested operation. +For example,
+JavascriptValidators copy = validators.copy();
copy.getContext().put(json.toString(), "json");
nestedOperation(copy);
+
+
+a copy of this factory
+Protected
getReturns the configuration for assertThat
factory methods.
the configuration for assertThat
factory methods
Protected
getReturns the configuration for checkIf
factory methods.
the configuration for checkIf
factory methods
Returns the contextual information inherited by validators created out by this factory. The contextual
+information is a map of key-value pairs that can provide more details about validation failures. For
+example, if the message is "Password may not be empty" and the map contains the key-value pair
+{"username": "john.smith"}
, the error message would be:
Password may not be empty
username: john.smith
+
+
+an unmodifiable map from each entry's name to its value
+Returns the global configuration shared by all validators.
++NOTE: Updating this configuration affects existing and new validators.
the global configuration updater
+the application configuration
+Removes the contextual information of validators created by this factory.
+the parameter name
+this
+Validates the state of an unknown value or a value that does not have a specialized validator.
++The returned validator throws an error immediately if a validation fails.
the value
+the name of the value
+a verifier
+Validates the state of an array.
++The returned validator throws an error immediately if a validation fails.
the type the value
+the type elements in the array
+the value
+the name of the value
+a verifier
+Validates the state of a boolean.
++The returned validator throws an error immediately if a validation fails.
the value
+the name of the value
+a verifier
+Validates the state of a number.
++The returned validator throws an error immediately if a validation fails.
the value
+the name of the value
+a verifier
+Validates the state of a set.
++The returned validator throws an error immediately if a validation fails.
the value
+the name of the value
+a verifier
+Validates the state of a string.
++The returned validator throws an error immediately if a validation fails.
the value
+the name of the value
+a verifier
+Set the configuration used by new validators.
+the updated configuration
+Sets the contextual information for validators created by this factory.
++This method adds contextual information to error messages. The contextual information is stored as +key-value pairs in a map. Values set by this method may be overridden by +ValidatorComponent.withContext.
the value of the entry
+the name of an entry
+this
+The terminal associated with the process.
+the encoding that the terminal should use (defaults to the best available encoding)
+the encodings supported by the terminal
+Indicates the type of encoding that the terminal should use.
++This feature can be used to force the use of colors even when their support is not detected.
the type of encoding that the terminal should use
+Represents a value that is being validated.
+This class is not intended to replace undefined
or null
references but to record additional
+information alongside them.
Instead of throwing an error when an undefined
or null
value is accessed, the system
+marks it as invalid and continues to record validation failures.
Creates a value that may be invalid.
+true
if the value is valid, or false
if invalid
the value
+Applies a function to the value if it is valid. If the value is invalid, no action is taken.
+this
if the value is invalid; otherwise, a MaybeInvalid
instance wrapping the
+result of applying the mapper to the value
Returns the valid value, or a default value if invalid. A value of null
does not hold any
+special significance. It does not imply that the value is invalid.
a value
+the valid value, or defaultValue
if the value is invalid
Converts an undefined
or null
value to an invalid value. If the value is invalid or non-null
, no
+action is taken.
an invalid value if the original value was undefined
or null
; otherwise, returns this
+with T
excluding undefined
and null
Evaluates a condition on the value.
+the condition to evaluate
+true
if the value is invalid, undefined
, null
or if the condition
returns false
;
+otherwise, returns false
Static
invalidReturns an invalid value.
+an invalid value
+Static
validReturns a valid value.
+a value
+a valid value
+Abstract
Creates validators for the Javascript API with an independent configuration.
++A factory that creates different types of validators. +
+There are three kinds of validators: +
Abstract
assertValidates the state of an unknown value or a value that does not have a specialized validator.
++The returned validator throws an error immediately if a validation fails. This error is then +converted into an AssertionError. Errors unrelated to validation failures are not converted.
the value
+Optional
name: stringthe name of the value
+a validator for the value
+Abstract
assertValidates the state of an array.
++The returned validator throws an error immediately if a validation fails. This error is then +converted into an AssertionError. Errors unrelated to validation failures are not converted.
the type of the value
+the type of elements in the collection
+the value
+Optional
name: stringthe name of the value
+a validator for the value
+Abstract
assertValidates the state of a boolean
.
+The returned validator throws an error immediately if a validation fails. This error is then +converted into an AssertionError. Errors unrelated to validation failures are not converted.
the value
+Optional
name: stringthe name of the value
+a validator for the value
+Abstract
assertValidates the state of a Map
.
+The returned validator throws an error immediately if a validation fails. This error is then +converted into an AssertionError. Errors unrelated to validation failures are not converted.
the value
+Optional
name: stringthe name of the value
+a validator for the value
+Abstract
assertValidates the state of a number
.
+The returned validator throws an error immediately if a validation fails. This error is then +converted into an AssertionError. Errors unrelated to validation failures are not converted.
the value
+Optional
name: stringthe name of the value
+a validator for the value
+Abstract
assertValidates the state of a Set
.
+The returned validator throws an error immediately if a validation fails. This error is then +converted into an AssertionError. Errors unrelated to validation failures are not converted.
the value
+Optional
name: stringthe name of the value
+a validator for the value
+Abstract
assertValidates the state of a string
.
+The returned validator throws an error immediately if a validation fails. This error is then +converted into an AssertionError. Errors unrelated to validation failures are not converted.
the value
+Optional
name: stringthe name of the value
+a validator for the value
+Abstract
checkValidates the state of an unknown value or a value that does not have a specialized validator.
++The returned validator captures errors on validation failure rather than throwing them immediately. +These errors can be retrieved or thrown once the validation completes. Errors unrelated to +validation failures are thrown immediately.
the value
+Optional
name: stringthe name of the value
+a validator for the value
+Abstract
checkValidates the state of an array.
++The returned validator captures errors on validation failure rather than throwing them immediately. +These errors can be retrieved or thrown once the validation completes. Errors unrelated to +validation failures are thrown immediately.
the type of the value
+the type of elements in the collection
+the value
+Optional
name: stringthe name of the value
+a validator for the value
+Abstract
checkValidates the state of a boolean
.
+The returned validator captures errors on validation failure rather than throwing them immediately. +These errors can be retrieved or thrown once the validation completes. Errors unrelated to +validation failures are thrown immediately.
the value
+Optional
name: stringthe name of the value
+a validator for the value
+Abstract
checkValidates the state of a Map
.
+The returned validator captures errors on validation failure rather than throwing them immediately. +These errors can be retrieved or thrown once the validation completes. Errors unrelated to +validation failures are thrown immediately.
the value
+Optional
name: stringthe name of the value
+a validator for the value
+Abstract
checkValidates the state of a number
.
+The returned validator captures errors on validation failure rather than throwing them immediately. +These errors can be retrieved or thrown once the validation completes. Errors unrelated to +validation failures are thrown immediately.
the value
+Optional
name: stringthe name of the value
+a validator for the value
+Abstract
checkValidates the state of a Set
.
+The returned validator captures errors on validation failure rather than throwing them immediately. +These errors can be retrieved or thrown once the validation completes. Errors unrelated to +validation failures are thrown immediately.
the value
+Optional
name: stringthe name of the value
+a validator for the value
+Abstract
checkValidates the state of a string
.
+The returned validator captures errors on validation failure rather than throwing them immediately. +These errors can be retrieved or thrown once the validation completes. Errors unrelated to +validation failures are thrown immediately.
the value
+Optional
name: stringthe name of the value
+a validator for the value
+Abstract
copyReturns a new factory instance with an independent configuration. This method is commonly used to inherit +and update contextual information from the original factory before passing it into a nested operation. +For example,
+JavascriptValidators copy = validators.copy();
copy.getContext().put(json.toString(), "json");
nestedOperation(copy);
+
+
+a copy of this factory
+Abstract
getReturns the contextual information inherited by validators created out by this factory. The contextual
+information is a map of key-value pairs that can provide more details about validation failures. For
+example, if the message is "Password may not be empty" and the map contains the key-value pair
+{"username": "john.smith"}
, the error message would be:
Password may not be empty
username: john.smith
+
+
+an unmodifiable map from each entry's name to its value
+Abstract
getReturns the global configuration shared by all validators.
++NOTE: Updating this configuration affects existing and new validators.
the global configuration updater
+Abstract
removeAbstract
requireValidates the state of an unknown value or a value that does not have a specialized validator.
++The returned validator throws an error immediately if a validation fails.
the value
+the name of the value
+a validator for the value
+Abstract
requireValidates the state of an array.
++The returned validator throws an error immediately if a validation fails.
the type of the value
+the type of elements in the collection
+the value
+the name of the value
+a validator for the value
+Abstract
requireValidates the state of a boolean
.
+The returned validator throws an error immediately if a validation fails.
the value
+the name of the value
+a validator for the value
+Abstract
requireValidates the state of a Map
.
+The returned validator throws an error immediately if a validation fails.
the value
+the name of the value
+a validator for the value
+Abstract
requireValidates the state of a number
.
+The returned validator throws an error immediately if a validation fails.
the value
+the name of the value
+a validator for the value
+Abstract
requireValidates the state of a Set
.
+The returned validator throws an error immediately if a validation fails.
the value
+the name of the value
+a validator for the value
+Abstract
requireValidates the state of a string
.
+The returned validator throws an error immediately if a validation fails.
the value
+the name of the value
+a validator for the value
+Abstract
withSets the contextual information for validators created by this factory.
++This method adds contextual information to error messages. The contextual information is stored as +key-value pairs in a map. Values set by this method may be overridden by +ValidatorComponent.withContext.
the value of the entry
+the name of an entry
+this
+Static
newCreates a new instance using the default configuration.
+an instance of this interface
+Thrown if multiple validations have failed.
+Creates a new error.
+the list of validation failures
+Returns the list of validation failures.
+the list of validation failures
+Describes the type of a value.
+Readonly
categoryReadonly
nameOptional
Readonly
typeStatic
Readonly
ANONYMOUS_An anonymous or arrow function.
+Static
Readonly
ARRAYStatic
Readonly
BIGINTStatic
Readonly
BOOLEANStatic
Readonly
NULLStatic
Readonly
NUMBERStatic
Readonly
STRINGStatic
Readonly
SYMBOLStatic
Readonly
UNDEFINEDIndicates if this type is equal to another type.
+another type
+true if this type matches other
Indicates whether this type is a subtype of another type. Note that types are considered subtypes of +themselves.
+the parent type
+Static
getStatic
namedReturns the type of a named class.
+the name of the class, or null
to represent any class.
Optional
typeGuard: ((value: unknown) => boolean)(optional) for certain types, such as Typescript interfaces, runtime validation is +not possible. In such a case, use a type guard to check if the value satisfies the type condition.
+the type
+Static
namedStatic
ofReturns the type of a value.
+a value
+the value's type
+A collection of validation failures.
+Creates a new instance.
+the validation failures
+Adds validation failures into this collection.
+the failures to add
+this
+Returns the error for the validation failures, if any.
+the error or null
if no validation has failed
Returns the list of failed validations.
+an unmodifiable list of failed validations
+Thrown when assertion has failed or a condition thought to be impossible has occurred.
+Creates a new error.
+Optional
message: stringan explanation of what went wrong
+Optional
options: { configuration options
+Determines the behavior of a validator.
+Creates a new configuration that:
+true
if error messages may include a diff that compares actual and expected values
the configuration used to map contextual values to a string
+true
if the error stack trace must be recorded when a validation failure
+occurs. If false
, the error type remains the same, but the stack trace points to the invocation
+of elseGetError()
. Users who only plan to
+list of failure messages instead of retrieving an error
+may see a performance improvement if this value is set to false
.
true
if an error is thrown on validation failure
a function that transforms the validation error before it is thrown or +returned
+Returns true
if the error stack trace must be recorded when a validation failure occurs. If false
,
+the error type remains the same, but the stack trace points to the invocation of
+elseGetError()
. Users who only plan to
+list of failure messages instead of retrieving an error
+may see a performance improvement if this value is set to false
.
true
if error stack traces must be recorded when a validation failure occurs
Returns the configuration used to map contextual values to a String. Supports common types such as +arrays, numbers, collections, maps, paths and errors.
+a function that takes an object and returns the string
representation of the object
Returns the String representation of
+Creates a new instance.
+a mapping from the name of a type to the string representation of its values
+Sets the function that maps a value of the given type to a string. This method is useful for customizing +the formatting of validation failure messages.
+a type
+a function that returns the String representation of the type's instances
+this
+Removes a mapper for a type.
+the type
+this
+Returns an immutable copy of the mapper configuration.
+an immutable copy of the mapper configuration
+Static
fromReturns a mutable copy of the StringMappers.
+a StringMappers
object
a mutable copy of the StringMappers
+Returns the String representation of an object.
+Creates a new instance. If typeToMapper
is undefined
the new instance uses the
+default mappings. Otherwise, it contains a copy of the typeToMapper
mappings.
Optional
typeToMapper: Map<Type, StringMapper>a mapping from the name of a type to the string representation of its values
+Returns the string representation of the mappers.
+the string representation of the mappers
+Returns the string representation of a value using the mappers.
+a value
+the string representation of the value
+The encodings supported by the terminal.
+Node terminal supports a 16-color palette.
+Node terminal supports a 24-bit color palette.
+Node terminal supports a 256-color palette.
+A terminal that does not support any colors.
+Validates the state of an unknown value or a value that does not have a specialized validator.
++The returned validator throws an exception immediately if a validation fails. This exception is then +converted into an AssertionError. Exceptions unrelated to validation failures are not converted.
the value
+Optional
name: stringthe name of the value
+validator for the value
+Validates the state of an array.
++The returned validator throws an exception immediately if a validation fails. This exception is then +converted into an AssertionError. Exceptions unrelated to validation failures are not converted.
the type the value
+the type elements in the array
+the value
+Optional
name: stringthe name of the value
+validator for the value
+Validates the state of a boolean.
++The returned validator throws an exception immediately if a validation fails. This exception is then +converted into an AssertionError. Exceptions unrelated to validation failures are not converted.
the value
+Optional
name: stringthe name of the value
+validator for the value
+Validates the state of a map.
++The returned validator throws an exception immediately if a validation fails. This exception is then +converted into an AssertionError. Exceptions unrelated to validation failures are not converted.
the value
+Optional
name: stringthe name of the value
+validator for the value
+Validates the state of a number.
++The returned validator throws an exception immediately if a validation fails. This exception is then +converted into an AssertionError. Exceptions unrelated to validation failures are not converted.
the value
+Optional
name: stringthe name of the value
+validator for the value
+Validates the state of a set.
++The returned validator throws an exception immediately if a validation fails. This exception is then +converted into an AssertionError. Exceptions unrelated to validation failures are not converted.
the value
+Optional
name: stringthe name of the value
+validator for the value
+Validates the state of a string.
++The returned validator throws an exception immediately if a validation fails. This exception is then +converted into an AssertionError. Exceptions unrelated to validation failures are not converted.
the value
+Optional
name: stringthe name of the value
+validator for the value
+Validates the state of an unknown value or a value that does not have a specialized validator.
++The returned validator throws an error immediately if a validation fails.
the value
+the name of the value
+validator for the value
+Validates the state of an array.
++The returned validator throws an error immediately if a validation fails.
the type the value
+the type elements in the array
+the value
+the name of the value
+validator for the value
+Validates the state of a boolean.
++The returned validator throws an error immediately if a validation fails.
the value
+the name of the value
+validator for the value
+Validates the state of a number.
++The returned validator throws an error immediately if a validation fails.
the value
+the name of the value
+validator for the value
+Validates the state of a set.
++The returned validator throws an error immediately if a validation fails.
the value
+the name of the value
+validator for the value
+Validates the state of a string.
++The returned validator throws an error immediately if a validation fails.
the value
+the name of the value
+validator for the value
+Returns the contextual information for validators created out by this factory. The contextual information
+is a map of key-value pairs that can provide more details about validation failures. For example, if the
+message is "Password may not be empty" and the map contains the key-value pair
+{"username": "john.smith"}
, the error message would be:
+```console +Password may not be empty +username: john.smith +```
an unmodifiable map from each entry's name to its value
+Returns the global configuration shared by all validators.
++NOTE: Updating this configuration affects existing and new validators.
the global configuration updater
+Removes the contextual information of validators created by this factory.
+the parameter name
+the underlying validator factory
+Validates the state of an unknown value or a value that does not have a specialized validator.
++The returned validator throws an exception immediately if a validation fails. This exception is then +converted into an AssertionError. Exceptions unrelated to validation failures are not converted.
the value
+the name of the value
+validator for the value
+Validates the state of an array.
++The returned validator throws an exception immediately if a validation fails. This exception is then +converted into an AssertionError. Exceptions unrelated to validation failures are not converted.
the type the value
+the type elements in the array
+the value
+the name of the value
+validator for the value
+Validates the state of a boolean.
++The returned validator throws an error immediately if a validation fails.
the value
+the name of the value
+a verifier
+Validates the state of a map.
++The returned validator throws an exception immediately if a validation fails. This exception is then +converted into an AssertionError. Exceptions unrelated to validation failures are not converted.
the value
+the name of the value
+validator for the value
+Validates the state of a number.
++The returned validator throws an error immediately if a validation fails.
the value
+the name of the value
+a verifier
+Validates the state of a set.
++The returned validator throws an exception immediately if a validation fails. This exception is then +converted into an AssertionError. Exceptions unrelated to validation failures are not converted.
the value
+the name of the value
+validator for the value
+Validates the state of a string.
++The returned validator throws an exception immediately if a validation fails. This exception is then +converted into an AssertionError. Exceptions unrelated to validation failures are not converted.
the value
+the name of the value
+validator for the value
+Updates the configuration that will be used by new validators.
+a function that updates the configuration
+this
+Sets the contextual information for validators created by this factory.
++This method adds contextual information to error messages. The contextual information is stored as +key-value pairs in a map. Values set by this method may be overridden by +ValidatorComponent.withContext.
the value of the entry
+the name of an entry
+the underlying validator factory
+Returns a comparator that sorts encodings based on the number of colors that they support, from the most +to the least number of colors.
+the first encoding
+the second encoding
+a negative number if first
supports more colors than second
.
+0
if the encodings support the same number of colors. A positive number if
+first
supports fewer colors than second
.
a value
+true if the value is an instance of ValidationFailure
A fluent API for enforcing +design contracts with +automatic message generation:
+✔️ Easy to use
+✔️ Fast
+✔️ Production-ready
To get started, add this dependency:
+npm install --save @cowwoc/requirements@4.0.5
+
+
+or pnpm:
+pnpm add @cowwoc/requirements@4.0.5
+
+
+import {requireThatString} from "@cowwoc/requirements";
class Cake
{
private bitesTaken = 0;
private piecesLeft;
public constructor(piecesLeft: number)
{
requireThat(piecesLeft, "piecesLeft").isPositive();
this.piecesLeft = piecesLeft;
}
public eat(): number
{
++bitesTaken;
assertThat(bitesTaken, "bitesTaken").isNotNegative().elseThrow();
piecesLeft -= ThreadLocalRandom.current().nextInt(5);
assertThat(piecesLeft, "piecesLeft").isNotNegative().elseThrow();
return piecesLeft;
}
public getFailures(): String[]
{
return checkIf(bitesTaken, "bitesTaken").isNotNegative().
and(checkIf(piecesLeft, "piecesLeft").isGreaterThan(3)).
elseGetMessages();
}
}
+
+
+If you violate a precondition:
+const cake = new Cake(-1000);
+
+
+You'll get:
+RangeError: "piecesLeft" must be positive.
actual: -1000
+
+
+If you violate a class invariant:
+const cake = new Cake(1_000_000);
while (true)
cake.eat();
+
+
+You'll get:
+lang.AssertionError: "bitesTaken" may not be negative.
actual: -128
+
+
+If you violate a postcondition:
+const cake = new Cake(100);
while (true)
cake.eat();
+
+
+You'll get:
+AssertionError: "piecesLeft" may not be negative.
actual: -4
+
+
+If you violate multiple conditions at once:
+const cake = new Cake(1);
cake.bitesTaken = -1;
cake.piecesLeft = 2;
const failures = [];
for (const failure of cake.getFailures())
failures.add(failure);
console.log(failures.join("\n\n"));
+
+
+You'll get:
+"bitesTaken" may not be negative.
actual: -1
"piecesLeft" must be greater than 3.
actual: 2
+
+
+This library offers the following features:
+Designed for discovery using your favorite IDE's auto-complete feature. +The main entry points are:
+See the API documentation for more details.
+checkIf().elseGetMessages()
to return failure messages without throwing an exception.
+This is the fastest validation approach, ideal for web services.assert that(value, name)
over assert that(value)
.Code licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
+Icons made by Flat Icons from www.flaticon.com is licensed by CC 3.0 BY
+The configuration of an application. A process may contain multiple applications.
+the global configuration inherited by all validators
+The process configuration.
+Methods that all validators must contain.
+Facilitates the validation of related properties. For example,
++```ts +requireThat(nameToFrequency, "nameToFrequency"). + and(m => m.size().isPositive()). + and(m => m.keySet().contains("John")); +``` +
+Any changes made during the validation process will impact this validator.
the nested validation
+this
+Returns the array of failed validations.
+an array of failed validations
+Returns the contextual information for upcoming validations carried out by this validator. The contextual
+information is a map of key-value pairs that can provide more details about validation failures. For
+example, if the message is "Password may not be empty" and the map contains the key-value pair
+{"username": "john.smith"}
, the error message would be:
Password may not be empty
username: john.smith
+
+
+an unmodifiable map from each entry's name to its value
+Returns the value that is being validated.
+the fallback value to use if the value is invalid
+the validated value, or defaultValue
if the value is invalid (e.g. due to dereferencing a
+property of a null
object)
Ensures that the object is equal to expected
.
the expected value
+this
+Ensures that the object is equal to expected
.
the expected value
+the name of the expected value
+this
+Ensures that the object is an instance of a class.
+the desired class
+a validator for an object of the desired class
+Ensures that the object is not equal to unwanted
.
the unwanted value
+this
+Ensures that the object is not equal to unwanted
.
the unwanted value
+the name of the other value
+this
+Ensures that the object is not an instance of a class.
+the unwanted class
+this
+Ensures that the value is not null
.
+This method should be used to validate method arguments that are assigned to class fields but not +accessed right away (such as constructor and setter arguments). It should also be used to validate any +method arguments when the validator contains +additional contextual information.
this
+Ensures that the value is not undefined.
++This method should be used to validate method arguments that are assigned to class fields but not +accessed right away (such as constructor and setter arguments). It should also be used to validate any +method arguments when the validator contains +additional contextual information.
this
+Ensures that the value is null
.
this
+Ensures that the value has the specified type.
+the expected type of the value
+this
+Ensures that the value is undefined.
+this
+Sets the contextual information for upcoming validations.
++This method adds contextual information to error messages. The contextual information is stored as +key-value pairs in a map. Values set by this method override any values that are set using +Validators.withContext. +
+There is no way to remove contextual information from a validator. Thread-level contextual information is +removed automatically.
the value of the entry
+the name of an entry
+this
+The configuration shared by all validators. Changes apply to existing or new validators.
+Returns the encoding supported by the terminal.
+the encoding supported by the terminal
+Returns the current terminal encoding.
+the current terminal encoding (defaults to the auto-detected encoding)
+Sets the terminal encoding of the output.
++This can be used to force the use of ANSI colors when their support is not detected.
the type of encoding that the terminal supports
+this
+Optional
encoding: TerminalEncodingCreates validators for the Javascript API that throw AssertionError
immediately on validation failure.
Validates the state of an unknown value or a value that does not have a specialized validator.
++The returned validator throws an error immediately if a validation fails. This error is then +converted into an AssertionError. Errors unrelated to validation failures are not converted.
the value
+Optional
name: stringthe name of the value
+a validator for the value
+Validates the state of an array.
++The returned validator throws an error immediately if a validation fails. This error is then +converted into an AssertionError. Errors unrelated to validation failures are not converted.
the type of the value
+the type of elements in the collection
+the value
+Optional
name: stringthe name of the value
+a validator for the value
+Validates the state of a boolean
.
+The returned validator throws an error immediately if a validation fails. This error is then +converted into an AssertionError. Errors unrelated to validation failures are not converted.
the value
+Optional
name: stringthe name of the value
+a validator for the value
+Validates the state of a Map
.
+The returned validator throws an error immediately if a validation fails. This error is then +converted into an AssertionError. Errors unrelated to validation failures are not converted.
the value
+Optional
name: stringthe name of the value
+a validator for the value
+Validates the state of a number
.
+The returned validator throws an error immediately if a validation fails. This error is then +converted into an AssertionError. Errors unrelated to validation failures are not converted.
the value
+Optional
name: stringthe name of the value
+a validator for the value
+Validates the state of a Set
.
+The returned validator throws an error immediately if a validation fails. This error is then +converted into an AssertionError. Errors unrelated to validation failures are not converted.
the value
+Optional
name: stringthe name of the value
+a validator for the value
+Validates the state of a string
.
+The returned validator throws an error immediately if a validation fails. This error is then +converted into an AssertionError. Errors unrelated to validation failures are not converted.
the value
+Optional
name: stringthe name of the value
+a validator for the value
+Creates validators for the Javascript API that capture errors on validation failure rather than throwing +them immediately.
+Validates the state of an unknown value or a value that does not have a specialized validator.
++The returned validator captures errors on validation failure rather than throwing them immediately. +These errors can be retrieved or thrown once the validation completes. Errors unrelated to +validation failures are thrown immediately.
the value
+the name of the value
+a validator for the value
+Validates the state of an array.
++The returned validator captures errors on validation failure rather than throwing them immediately. +These errors can be retrieved or thrown once the validation completes. Errors unrelated to +validation failures are thrown immediately.
the type of the value
+the type of elements in the collection
+the value
+the name of the value
+a validator for the value
+Validates the state of a boolean
.
+The returned validator captures errors on validation failure rather than throwing them immediately. +These errors can be retrieved or thrown once the validation completes. Errors unrelated to +validation failures are thrown immediately.
the value
+the name of the value
+a validator for the value
+Validates the state of a Map
.
+The returned validator captures errors on validation failure rather than throwing them immediately. +These errors can be retrieved or thrown once the validation completes. Errors unrelated to +validation failures are thrown immediately.
the value
+the name of the value
+a validator for the value
+Validates the state of a number
.
+The returned validator captures errors on validation failure rather than throwing them immediately. +These errors can be retrieved or thrown once the validation completes. Errors unrelated to +validation failures are thrown immediately.
the value
+the name of the value
+a validator for the value
+Validates the state of a Set
.
+The returned validator captures errors on validation failure rather than throwing them immediately. +These errors can be retrieved or thrown once the validation completes. Errors unrelated to +validation failures are thrown immediately.
the value
+the name of the value
+a validator for the value
+Validates the state of a string
.
+The returned validator captures errors on validation failure rather than throwing them immediately. +These errors can be retrieved or thrown once the validation completes. Errors unrelated to +validation failures are thrown immediately.
the value
+the name of the value
+a validator for the value
+Creates validators for the Javascript API that throw errors immediately on validation failure.
+Validates the state of an unknown value or a value that does not have a specialized validator.
++The returned validator throws an error immediately if a validation fails.
the value
+the name of the value
+a validator for the value
+Validates the state of an array.
++The returned validator throws an error immediately if a validation fails.
the type of the value
+the type of elements in the collection
+the value
+the name of the value
+a validator for the value
+Validates the state of a boolean
.
+The returned validator throws an error immediately if a validation fails.
the value
+the name of the value
+a validator for the value
+Validates the state of a Map
.
+The returned validator throws an error immediately if a validation fails.
the value
+the name of the value
+a validator for the value
+Validates the state of a number
.
+The returned validator throws an error immediately if a validation fails.
the value
+the name of the value
+a validator for the value
+Validates the state of a Set
.
+The returned validator throws an error immediately if a validation fails.
the value
+the name of the value
+a validator for the value
+Validates the state of a string
.
+The returned validator throws an error immediately if a validation fails.
the value
+the name of the value
+a validator for the value
+A factory that creates different types of validators.
++There are three kinds of validators: +
Returns a new factory instance with an independent configuration. This method is commonly used to inherit +and update contextual information from the original factory before passing it into a nested operation. +For example,
+JavascriptValidators copy = validators.copy();
copy.getContext().put(json.toString(), "json");
nestedOperation(copy);
+
+
+a copy of this factory
+Returns the contextual information inherited by validators created out by this factory. The contextual
+information is a map of key-value pairs that can provide more details about validation failures. For
+example, if the message is "Password may not be empty" and the map contains the key-value pair
+{"username": "john.smith"}
, the error message would be:
Password may not be empty
username: john.smith
+
+
+an unmodifiable map from each entry's name to its value
+Returns the global configuration shared by all validators.
++NOTE: Updating this configuration affects existing and new validators.
the global configuration updater
+Sets the contextual information for validators created by this factory.
++This method adds contextual information to error messages. The contextual information is stored as +key-value pairs in a map. Values set by this method may be overridden by +ValidatorComponent.withContext.
the value of the entry
+the name of an entry
+this
+Validates the state of an array.
+the type of the value
+the type of elements in the collection
+Facilitates the validation of related properties. For example,
++```ts +requireThat(nameToFrequency, "nameToFrequency"). + and(m => m.size().isPositive()). + and(m => m.keySet().contains("John")); +``` +
+Any changes made during the validation process will impact this validator.
the nested validation
+this
+Ensures that the collection contains all the elements in expected
.
this
+Ensures that the collection contains all the elements in expected
.
the desired elements
+this
+Ensures that the collection contains all the elements in expected
.
this
+Ensures that the collection contains all the elements in expected
.
the desired elements
+the name of the expected collection
+this
+Ensures that the collection contains any elements in expected
.
this
+Ensures that the collection contains any elements in expected
.
the desired elements
+this
+Ensures that the collection contains at least one element in expected
.
this
+Ensures that the collection contains at least one element in expected
.
the desired elements
+the name of the expected collection
+this
+Ensures that the collection consists of the same elements as expected
, irrespective of their
+order.
+In contrast, isEqualTo() requires the same element ordering.
this
+Ensures that the collection consists of the same elements as expected
, irrespective of
+their order.
+In contrast, isEqualTo() requires the same element ordering.
the desired elements
+this
+Ensures that the collection consists of the same elements as expected
, irrespective of their
+order.
+In contrast, isEqualTo() requires the same element ordering.
this
+Ensures that the collection consists of the same elements as expected
, irrespective of
+their order.
+In contrast, isEqualTo() requires the same element ordering.
the desired elements
+the name of the expected collection
+this
+Ensures that the collection does not contain unwanted
.
the unwanted element
+this
+Ensures that the collection does not contain unwanted
.
the unwanted element
+the name of the element
+this
+Allows the collection to contain some, but not all, elements from a collection.
+this
+Allows the collection to contain some, but not all, elements from a collection.
+the unwanted elements
+this
+Allows the collection to contain some, but not all, elements from a collection.
+this
+Allows the collection to contain some, but not all, elements from a collection.
+the unwanted elements
+the name of the unwanted collection
+this
+Ensures that the collection does not contain any of the elements in unwanted
.
this
+Ensures that the collection does not contain any of the elements in unwanted
.
the unwanted elements
+this
+Ensures that the collection does not contain any of the elements in unwanted
.
this
+Ensures that the collection does not contain any of the elements in unwanted
.
the unwanted elements
+the name of the unwanted collection
+this
+Ensures that the collection and unwanted
consist of different elements, irrespective of their
+order.
this
+Ensures that the collection and unwanted
consist of different elements, irrespective of their
+order.
the unwanted elements
+this
+Ensures that the collection and unwanted
consist of different elements, irrespective of
+their order.
this
+Ensures that the collection and unwanted
consist of different elements, irrespective of their
+order.
the unwanted elements
+the name of the unwanted collection
+this
+Returns the array of failed validations.
+an array of failed validations
+Returns the contextual information for upcoming validations carried out by this validator. The contextual
+information is a map of key-value pairs that can provide more details about validation failures. For
+example, if the message is "Password may not be empty" and the map contains the key-value pair
+{"username": "john.smith"}
, the error message would be:
Password may not be empty
username: john.smith
+
+
+an unmodifiable map from each entry's name to its value
+Returns the value that is being validated.
+the fallback value to use if the value is invalid
+the validated value, or defaultValue
if the value is invalid (e.g. due to dereferencing a
+property of a null
object)
Ensures that the object is equal to expected
.
the expected value
+this
+Ensures that the object is equal to expected
.
the expected value
+the name of the expected value
+this
+Ensures that the object is an instance of a class.
+the desired class
+a validator for an object of the desired class
+Ensures that the object is not equal to unwanted
.
the unwanted value
+this
+Ensures that the object is not equal to unwanted
.
the unwanted value
+the name of the other value
+this
+Ensures that the object is not an instance of a class.
+the unwanted class
+this
+Ensures that the value is not null
.
+This method should be used to validate method arguments that are assigned to class fields but not +accessed right away (such as constructor and setter arguments). It should also be used to validate any +method arguments when the validator contains +additional contextual information.
this
+Ensures that the value is not undefined.
++This method should be used to validate method arguments that are assigned to class fields but not +accessed right away (such as constructor and setter arguments). It should also be used to validate any +method arguments when the validator contains +additional contextual information.
this
+Ensures that the value is null
.
this
+Ensures that the array is sorted.
+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
.
this
+Ensures that the value has the specified type.
+the expected type of the value
+this
+Ensures that the value is undefined.
+this
+Returns a validator for the array's length.
+a validator for the array's length
+Returns a validator for the collection's size.
+a validator for the collection's size
+Sets the contextual information for upcoming validations.
++This method adds contextual information to error messages. The contextual information is stored as +key-value pairs in a map. Values set by this method override any values that are set using +Validators.withContext. +
+There is no way to remove contextual information from a validator. Thread-level contextual information is +removed automatically.
the value of the entry
+the name of an entry
+this
+Validates the state of a boolean
.
Facilitates the validation of related properties. For example,
++```ts +requireThat(nameToFrequency, "nameToFrequency"). + and(m => m.size().isPositive()). + and(m => m.keySet().contains("John")); +``` +
+Any changes made during the validation process will impact this validator.
the nested validation
+this
+Returns the array of failed validations.
+an array of failed validations
+Returns the contextual information for upcoming validations carried out by this validator. The contextual
+information is a map of key-value pairs that can provide more details about validation failures. For
+example, if the message is "Password may not be empty" and the map contains the key-value pair
+{"username": "john.smith"}
, the error message would be:
Password may not be empty
username: john.smith
+
+
+an unmodifiable map from each entry's name to its value
+Returns the value that is being validated.
+the fallback value to use if the value is invalid
+the validated value, or defaultValue
if the value is invalid (e.g. due to dereferencing a
+property of a null
object)
Ensures that the object is equal to expected
.
the expected value
+this
+Ensures that the object is equal to expected
.
the expected value
+the name of the expected value
+this
+Ensures that the object is an instance of a class.
+the desired class
+a validator for an object of the desired class
+Ensures that the object is not equal to unwanted
.
the unwanted value
+this
+Ensures that the object is not equal to unwanted
.
the unwanted value
+the name of the other value
+this
+Ensures that the object is not an instance of a class.
+the unwanted class
+this
+Ensures that the value is not null
.
+This method should be used to validate method arguments that are assigned to class fields but not +accessed right away (such as constructor and setter arguments). It should also be used to validate any +method arguments when the validator contains +additional contextual information.
this
+Ensures that the value is not undefined.
++This method should be used to validate method arguments that are assigned to class fields but not +accessed right away (such as constructor and setter arguments). It should also be used to validate any +method arguments when the validator contains +additional contextual information.
this
+Ensures that the value is null
.
this
+Ensures that the value has the specified type.
+the expected type of the value
+this
+Ensures that the value is undefined.
+this
+Sets the contextual information for upcoming validations.
++This method adds contextual information to error messages. The contextual information is stored as +key-value pairs in a map. Values set by this method override any values that are set using +Validators.withContext. +
+There is no way to remove contextual information from a validator. Thread-level contextual information is +removed automatically.
the value of the entry
+the name of an entry
+this
+Updates the configuration used by new validators.
+Returns true
if error messages may include a diff that compares actual and expected values.
true
by default
Specifies that error messages may include a diff that compares actual and expected values.
+true
by default
this
+Optional
mayDiff: booleanReturns a function that transforms the validation error before it is thrown or returned. If the function
+returns undefined
or null
, it’s treated as if it returned the input error.
a function that transforms the validation error
+Sets a function that transforms the validation error before it is thrown or returned. If the function
+returns undefined
or null
, it’s treated as if it returned the input error.
this
+Returns true
if error stack traces should reference the code that triggers a validation
+failure. When set to false
, the error type remains unchanged, but the stack trace location is
+undefined. Users who only plan to list of failure messages
+instead of errors may experience a performance improvement if this value is set to false
.
true
if errors must be recorded when a validation failure occurs
Specifies whether error stack traces should reference the code that triggers a validation failure.
+When set to false
, the error type remains unchanged, but the stack trace location is
+undefined. Users who only plan to list of failure messages
+instead of errors may experience a performance improvement if this value is set to false
.
true
if errors must be recorded when a validation failure occurs
this
+Optional
recordStacktrace: booleanReturns the configuration used to map contextual values to a String. Supports common types such as +arrays or numbers.
+a function that takes an object and returns the string
representation of the object
Validates the state of a Map
.
Facilitates the validation of related properties. For example,
++```ts +requireThat(nameToFrequency, "nameToFrequency"). + and(m => m.size().isPositive()). + and(m => m.keySet().contains("John")); +``` +
+Any changes made during the validation process will impact this validator.
the nested validation
+this
+Returns the array of failed validations.
+an array of failed validations
+Returns a validator for the map's Map.entries
+(an array of [key, this.value]
for each element in the Map).
a validator for the map's Map.entries
+Returns the contextual information for upcoming validations carried out by this validator. The contextual
+information is a map of key-value pairs that can provide more details about validation failures. For
+example, if the message is "Password may not be empty" and the map contains the key-value pair
+{"username": "john.smith"}
, the error message would be:
Password may not be empty
username: john.smith
+
+
+an unmodifiable map from each entry's name to its value
+Returns the value that is being validated.
+the fallback value to use if the value is invalid
+the validated value, or defaultValue
if the value is invalid (e.g. due to dereferencing a
+property of a null
object)
Ensures that the object is equal to expected
.
the expected value
+this
+Ensures that the object is equal to expected
.
the expected value
+the name of the expected value
+this
+Ensures that the object is an instance of a class.
+the desired class
+a validator for an object of the desired class
+Ensures that the object is not equal to unwanted
.
the unwanted value
+this
+Ensures that the object is not equal to unwanted
.
the unwanted value
+the name of the other value
+this
+Ensures that the object is not an instance of a class.
+the unwanted class
+this
+Ensures that the value is not null
.
+This method should be used to validate method arguments that are assigned to class fields but not +accessed right away (such as constructor and setter arguments). It should also be used to validate any +method arguments when the validator contains +additional contextual information.
this
+Ensures that the value is not undefined.
++This method should be used to validate method arguments that are assigned to class fields but not +accessed right away (such as constructor and setter arguments). It should also be used to validate any +method arguments when the validator contains +additional contextual information.
this
+Ensures that the value is null
.
this
+Ensures that the value has the specified type.
+the expected type of the value
+this
+Ensures that the value is undefined.
+this
+Returns a validator for the map's Map.keys.
+a validator for the map's Map.keys
+Returns a validator for the map's Map.size.
+a validator for the map's Map.size
+Returns a validator for the map's Map.values.
+a validator for the map's Map.values
+Sets the contextual information for upcoming validations.
++This method adds contextual information to error messages. The contextual information is stored as +key-value pairs in a map. Values set by this method override any values that are set using +Validators.withContext. +
+There is no way to remove contextual information from a validator. Thread-level contextual information is +removed automatically.
the value of the entry
+the name of an entry
+this
+Validates the state of a number
.
Facilitates the validation of related properties. For example,
++```ts +requireThat(nameToFrequency, "nameToFrequency"). + and(m => m.size().isPositive()). + and(m => m.keySet().contains("John")); +``` +
+Any changes made during the validation process will impact this validator.
the nested validation
+this
+Returns the array of failed validations.
+an array of failed validations
+Returns the contextual information for upcoming validations carried out by this validator. The contextual
+information is a map of key-value pairs that can provide more details about validation failures. For
+example, if the message is "Password may not be empty" and the map contains the key-value pair
+{"username": "john.smith"}
, the error message would be:
Password may not be empty
username: john.smith
+
+
+an unmodifiable map from each entry's name to its value
+Returns the value that is being validated.
+the fallback value to use if the value is invalid
+the validated value, or defaultValue
if the value is invalid (e.g. due to dereferencing a
+property of a null
object)
Ensures that the value is within a range.
+the lower bound of the range (inclusive)
+the upper bound of the range (exclusive)
+this
+Ensures that the value is within a range.
+the lower bound of the range
+true
if the lower bound of the range is inclusive
the upper bound of the range
+true
if the upper bound of the range is inclusive
this
+RangeError if minimum
is greater than maximum
. If
+minimumInclusive
is true
, and the value is less than minimum
.
+If minimumInclusive
is false
, and the value is less than or equal to
+minimum
. If maximumInclusive
is true
and the value is greater
+than maximum
. If maximumInclusive
is false
, and the value is
+greater than or equal to maximum
.
Ensures that the object is equal to expected
.
the expected value
+this
+Ensures that the object is equal to expected
.
the expected value
+the name of the expected value
+this
+Ensures that the value is greater than a lower bound.
+the exclusive lower bound
+this
+Ensures that the value is greater than a lower bound.
+the exclusive lower bound
+the name of the lower bound
+this
+Ensures that the value is greater than or equal to a minimum value.
+the minimum value
+this
+Ensures that the value is greater than or equal a minimum value.
+the minimum value
+the name of the minimum value
+this
+Ensures that the object is an instance of a class.
+the desired class
+a validator for an object of the desired class
+Ensures that the value is less than an upper bound.
+the exclusive upper bound
+this
+Ensures that the value is less than an upper bound.
+the exclusive upper bound
+the name of the upper bound
+this
+Ensures that the value is less than or equal to a maximum value.
+the inclusive upper value
+this
+Ensures that the value is less than or equal to a maximum value.
+the maximum value
+the name of the maximum value
+this
+Ensures that the value is a multiple of factor
.
the number being multiplied
+this
+Ensures that the value is a multiple of factor
.
the number being multiplied
+the name of the factor
+this
+Ensures that the object is not equal to unwanted
.
the unwanted value
+this
+Ensures that the object is not equal to unwanted
.
the unwanted value
+the name of the other value
+this
+Ensures that the object is not an instance of a class.
+the unwanted class
+this
+Ensures that the value is not a multiple of factor
.
the number being multiplied
+this
+Ensures that the value is not a multiple of factor
.
the number being multiplied
+the name of the factor
+this
+Ensures that the value is not null
.
+This method should be used to validate method arguments that are assigned to class fields but not +accessed right away (such as constructor and setter arguments). It should also be used to validate any +method arguments when the validator contains +additional contextual information.
this
+Ensures that the value is not undefined.
++This method should be used to validate method arguments that are assigned to class fields but not +accessed right away (such as constructor and setter arguments). It should also be used to validate any +method arguments when the validator contains +additional contextual information.
this
+Ensures that the value is null
.
this
+Ensures that the value has the specified type.
+the expected type of the value
+this
+Ensures that the value is undefined.
+this
+Sets the contextual information for upcoming validations.
++This method adds contextual information to error messages. The contextual information is stored as +key-value pairs in a map. Values set by this method override any values that are set using +Validators.withContext. +
+There is no way to remove contextual information from a validator. Thread-level contextual information is +removed automatically.
the value of the entry
+the name of an entry
+this
+Validates the state of a Set
.
Facilitates the validation of related properties. For example,
++```ts +requireThat(nameToFrequency, "nameToFrequency"). + and(m => m.size().isPositive()). + and(m => m.keySet().contains("John")); +``` +
+Any changes made during the validation process will impact this validator.
the nested validation
+this
+Ensures that the collection contains all the elements in expected
.
this
+Ensures that the collection contains all the elements in expected
.
the desired elements
+this
+Ensures that the collection contains all the elements in expected
.
this
+Ensures that the collection contains all the elements in expected
.
the desired elements
+the name of the expected collection
+this
+Ensures that the collection contains any elements in expected
.
this
+Ensures that the collection contains any elements in expected
.
the desired elements
+this
+Ensures that the collection contains at least one element in expected
.
this
+Ensures that the collection contains at least one element in expected
.
the desired elements
+the name of the expected collection
+this
+Ensures that the collection consists of the same elements as expected
, irrespective of their
+order.
+In contrast, isEqualTo() requires the same element ordering.
this
+Ensures that the collection consists of the same elements as expected
, irrespective of
+their order.
+In contrast, isEqualTo() requires the same element ordering.
the desired elements
+this
+Ensures that the collection consists of the same elements as expected
, irrespective of their
+order.
+In contrast, isEqualTo() requires the same element ordering.
this
+Ensures that the collection consists of the same elements as expected
, irrespective of
+their order.
+In contrast, isEqualTo() requires the same element ordering.
the desired elements
+the name of the expected collection
+this
+Ensures that the collection does not contain unwanted
.
the unwanted element
+this
+Ensures that the collection does not contain unwanted
.
the unwanted element
+the name of the element
+this
+Allows the collection to contain some, but not all, elements from a collection.
+this
+Allows the collection to contain some, but not all, elements from a collection.
+the unwanted elements
+this
+Allows the collection to contain some, but not all, elements from a collection.
+this
+Allows the collection to contain some, but not all, elements from a collection.
+the unwanted elements
+the name of the unwanted collection
+this
+Ensures that the collection does not contain any of the elements in unwanted
.
this
+Ensures that the collection does not contain any of the elements in unwanted
.
the unwanted elements
+this
+Ensures that the collection does not contain any of the elements in unwanted
.
this
+Ensures that the collection does not contain any of the elements in unwanted
.
the unwanted elements
+the name of the unwanted collection
+this
+Ensures that the collection and unwanted
consist of different elements, irrespective of their
+order.
this
+Ensures that the collection and unwanted
consist of different elements, irrespective of their
+order.
the unwanted elements
+this
+Ensures that the collection and unwanted
consist of different elements, irrespective of
+their order.
this
+Ensures that the collection and unwanted
consist of different elements, irrespective of their
+order.
the unwanted elements
+the name of the unwanted collection
+this
+Returns the array of failed validations.
+an array of failed validations
+Returns the contextual information for upcoming validations carried out by this validator. The contextual
+information is a map of key-value pairs that can provide more details about validation failures. For
+example, if the message is "Password may not be empty" and the map contains the key-value pair
+{"username": "john.smith"}
, the error message would be:
Password may not be empty
username: john.smith
+
+
+an unmodifiable map from each entry's name to its value
+Returns the value that is being validated.
+the fallback value to use if the value is invalid
+the validated value, or defaultValue
if the value is invalid (e.g. due to dereferencing a
+property of a null
object)
Ensures that the object is equal to expected
.
the expected value
+this
+Ensures that the object is equal to expected
.
the expected value
+the name of the expected value
+this
+Ensures that the object is an instance of a class.
+the desired class
+a validator for an object of the desired class
+Ensures that the object is not equal to unwanted
.
the unwanted value
+this
+Ensures that the object is not equal to unwanted
.
the unwanted value
+the name of the other value
+this
+Ensures that the object is not an instance of a class.
+the unwanted class
+this
+Ensures that the value is not null
.
+This method should be used to validate method arguments that are assigned to class fields but not +accessed right away (such as constructor and setter arguments). It should also be used to validate any +method arguments when the validator contains +additional contextual information.
this
+Ensures that the value is not undefined.
++This method should be used to validate method arguments that are assigned to class fields but not +accessed right away (such as constructor and setter arguments). It should also be used to validate any +method arguments when the validator contains +additional contextual information.
this
+Ensures that the value is null
.
this
+Ensures that the value has the specified type.
+the expected type of the value
+this
+Ensures that the value is undefined.
+this
+Returns a validator for the set's size.
+a validator for the set's size
+Sets the contextual information for upcoming validations.
++This method adds contextual information to error messages. The contextual information is stored as +key-value pairs in a map. Values set by this method override any values that are set using +Validators.withContext. +
+There is no way to remove contextual information from a validator. Thread-level contextual information is +removed automatically.
the value of the entry
+the name of an entry
+this
+Validates the state of a string
.
Facilitates the validation of related properties. For example,
++```ts +requireThat(nameToFrequency, "nameToFrequency"). + and(m => m.size().isPositive()). + and(m => m.keySet().contains("John")); +``` +
+Any changes made during the validation process will impact this validator.
the nested validation
+this
+Returns the array of failed validations.
+an array of failed validations
+Returns the contextual information for upcoming validations carried out by this validator. The contextual
+information is a map of key-value pairs that can provide more details about validation failures. For
+example, if the message is "Password may not be empty" and the map contains the key-value pair
+{"username": "john.smith"}
, the error message would be:
Password may not be empty
username: john.smith
+
+
+an unmodifiable map from each entry's name to its value
+Returns the value that is being validated.
+the fallback value to use if the value is invalid
+the validated value, or defaultValue
if the value is invalid (e.g. due to dereferencing a
+property of a null
object)
Ensures that the object is equal to expected
.
the expected value
+this
+Ensures that the object is equal to expected
.
the expected value
+the name of the expected value
+this
+Ensures that the object is an instance of a class.
+the desired class
+a validator for an object of the desired class
+Ensures that the object is not equal to unwanted
.
the unwanted value
+this
+Ensures that the object is not equal to unwanted
.
the unwanted value
+the name of the other value
+this
+Ensures that the object is not an instance of a class.
+the unwanted class
+this
+Ensures that the value is not null
.
+This method should be used to validate method arguments that are assigned to class fields but not +accessed right away (such as constructor and setter arguments). It should also be used to validate any +method arguments when the validator contains +additional contextual information.
this
+Ensures that the value is not undefined.
++This method should be used to validate method arguments that are assigned to class fields but not +accessed right away (such as constructor and setter arguments). It should also be used to validate any +method arguments when the validator contains +additional contextual information.
this
+Ensures that the value is null
.
this
+Ensures that the value has the specified type.
+the expected type of the value
+this
+Ensures that the value is undefined.
+this
+Returns a validator for the length of the string.
+a validator for the length of the string
+Ensures that the value matches a regular expression.
+the regular expression
+this
+Sets the contextual information for upcoming validations.
++This method adds contextual information to error messages. The contextual information is stored as +key-value pairs in a map. Values set by this method override any values that are set using +Validators.withContext. +
+There is no way to remove contextual information from a validator. Thread-level contextual information is +removed automatically.
the value of the entry
+the name of an entry
+this
+Validates the state of an unsigned number
.
Facilitates the validation of related properties. For example,
++```ts +requireThat(nameToFrequency, "nameToFrequency"). + and(m => m.size().isPositive()). + and(m => m.keySet().contains("John")); +``` +
+Any changes made during the validation process will impact this validator.
the nested validation
+this
+Returns the array of failed validations.
+an array of failed validations
+Returns the contextual information for upcoming validations carried out by this validator. The contextual
+information is a map of key-value pairs that can provide more details about validation failures. For
+example, if the message is "Password may not be empty" and the map contains the key-value pair
+{"username": "john.smith"}
, the error message would be:
Password may not be empty
username: john.smith
+
+
+an unmodifiable map from each entry's name to its value
+Returns the value that is being validated.
+the fallback value to use if the value is invalid
+the validated value, or defaultValue
if the value is invalid (e.g. due to dereferencing a
+property of a null
object)
Ensures that the value is within a range.
+the lower bound of the range (inclusive)
+the upper bound of the range (exclusive)
+this
+Ensures that the value is within a range.
+the lower bound of the range
+true
if the lower bound of the range is inclusive
the upper bound of the range
+true
if the upper bound of the range is inclusive
this
+RangeError if minimum
is greater than maximum
. If
+minimumInclusive
is true
, and the value is less than minimum
.
+If minimumInclusive
is false
, and the value is less than or equal to
+minimum
. If maximumInclusive
is true
and the value is greater
+than maximum
. If maximumInclusive
is false
, and the value is
+greater than or equal to maximum
.
Ensures that the object is equal to expected
.
the expected value
+this
+Ensures that the object is equal to expected
.
the expected value
+the name of the expected value
+this
+Ensures that the value is greater than a lower bound.
+the exclusive lower bound
+this
+Ensures that the value is greater than a lower bound.
+the exclusive lower bound
+the name of the lower bound
+this
+Ensures that the value is greater than or equal to a minimum value.
+the minimum value
+this
+Ensures that the value is greater than or equal a minimum value.
+the minimum value
+the name of the minimum value
+this
+Ensures that the object is an instance of a class.
+the desired class
+a validator for an object of the desired class
+Ensures that the value is less than an upper bound.
+the exclusive upper bound
+this
+Ensures that the value is less than an upper bound.
+the exclusive upper bound
+the name of the upper bound
+this
+Ensures that the value is less than or equal to a maximum value.
+the inclusive upper value
+this
+Ensures that the value is less than or equal to a maximum value.
+the maximum value
+the name of the maximum value
+this
+Ensures that the value is a multiple of factor
.
the number being multiplied
+this
+Ensures that the value is a multiple of factor
.
the number being multiplied
+the name of the factor
+this
+Ensures that the object is not equal to unwanted
.
the unwanted value
+this
+Ensures that the object is not equal to unwanted
.
the unwanted value
+the name of the other value
+this
+Ensures that the object is not an instance of a class.
+the unwanted class
+this
+Ensures that the value is not a multiple of factor
.
the number being multiplied
+this
+Ensures that the value is not a multiple of factor
.
the number being multiplied
+the name of the factor
+this
+Ensures that the value is not null
.
+This method should be used to validate method arguments that are assigned to class fields but not +accessed right away (such as constructor and setter arguments). It should also be used to validate any +method arguments when the validator contains +additional contextual information.
this
+Ensures that the value is not undefined.
++This method should be used to validate method arguments that are assigned to class fields but not +accessed right away (such as constructor and setter arguments). It should also be used to validate any +method arguments when the validator contains +additional contextual information.
this
+Ensures that the value is null
.
this
+Ensures that the value has the specified type.
+the expected type of the value
+this
+Ensures that the value is undefined.
+this
+Sets the contextual information for upcoming validations.
++This method adds contextual information to error messages. The contextual information is stored as +key-value pairs in a map. Values set by this method override any values that are set using +Validators.withContext. +
+There is no way to remove contextual information from a validator. Thread-level contextual information is +removed automatically.
the value of the entry
+the name of an entry
+this
+Methods that all collection validators must contain.
+Ensures that the collection contains all the elements in expected
.
this
+Ensures that the collection contains all the elements in expected
.
the desired elements
+this
+Ensures that the collection contains all the elements in expected
.
this
+Ensures that the collection contains all the elements in expected
.
the desired elements
+the name of the expected collection
+this
+Ensures that the collection contains any elements in expected
.
this
+Ensures that the collection contains any elements in expected
.
the desired elements
+this
+Ensures that the collection contains at least one element in expected
.
this
+Ensures that the collection contains at least one element in expected
.
the desired elements
+the name of the expected collection
+this
+Ensures that the collection consists of the same elements as expected
, irrespective of their
+order.
+In contrast, isEqualTo() requires the same element ordering.
this
+Ensures that the collection consists of the same elements as expected
, irrespective of
+their order.
+In contrast, isEqualTo() requires the same element ordering.
the desired elements
+this
+Ensures that the collection consists of the same elements as expected
, irrespective of their
+order.
+In contrast, isEqualTo() requires the same element ordering.
this
+Ensures that the collection consists of the same elements as expected
, irrespective of
+their order.
+In contrast, isEqualTo() requires the same element ordering.
the desired elements
+the name of the expected collection
+this
+Ensures that the collection does not contain unwanted
.
the unwanted element
+this
+Ensures that the collection does not contain unwanted
.
the unwanted element
+the name of the element
+this
+Allows the collection to contain some, but not all, elements from a collection.
+this
+Allows the collection to contain some, but not all, elements from a collection.
+the unwanted elements
+this
+Allows the collection to contain some, but not all, elements from a collection.
+this
+Allows the collection to contain some, but not all, elements from a collection.
+the unwanted elements
+the name of the unwanted collection
+this
+Ensures that the collection does not contain any of the elements in unwanted
.
this
+Ensures that the collection does not contain any of the elements in unwanted
.
the unwanted elements
+this
+Ensures that the collection does not contain any of the elements in unwanted
.
this
+Ensures that the collection does not contain any of the elements in unwanted
.
the unwanted elements
+the name of the unwanted collection
+this
+Ensures that the collection and unwanted
consist of different elements, irrespective of their
+order.
this
+Ensures that the collection and unwanted
consist of different elements, irrespective of their
+order.
the unwanted elements
+this
+Ensures that the collection and unwanted
consist of different elements, irrespective of
+their order.
this
+Ensures that the collection and unwanted
consist of different elements, irrespective of their
+order.
the unwanted elements
+the name of the unwanted collection
+this
+Returns a validator for the collection's size.
+a validator for the collection's size
+Methods that validators for numbers that may be negative must contain.
+Methods that all number validators must contain.
+Ensures that the value is within a range.
+the lower bound of the range (inclusive)
+the upper bound of the range (exclusive)
+this
+Ensures that the value is within a range.
+the lower bound of the range
+true
if the lower bound of the range is inclusive
the upper bound of the range
+true
if the upper bound of the range is inclusive
this
+RangeError if minimum
is greater than maximum
. If
+minimumInclusive
is true
, and the value is less than minimum
.
+If minimumInclusive
is false
, and the value is less than or equal to
+minimum
. If maximumInclusive
is true
and the value is greater
+than maximum
. If maximumInclusive
is false
, and the value is
+greater than or equal to maximum
.
Ensures that the value is greater than a lower bound.
+the exclusive lower bound
+this
+Ensures that the value is greater than a lower bound.
+the exclusive lower bound
+the name of the lower bound
+this
+Ensures that the value is greater than or equal to a minimum value.
+the minimum value
+this
+Ensures that the value is greater than or equal a minimum value.
+the minimum value
+the name of the minimum value
+this
+Ensures that the value is less than an upper bound.
+the exclusive upper bound
+this
+Ensures that the value is less than an upper bound.
+the exclusive upper bound
+the name of the upper bound
+this
+Ensures that the value is less than or equal to a maximum value.
+the inclusive upper value
+this
+Ensures that the value is less than or equal to a maximum value.
+the maximum value
+the name of the maximum value
+this
+Ensures that the value is a multiple of factor
.
the number being multiplied
+this
+Ensures that the value is a multiple of factor
.
the number being multiplied
+the name of the factor
+this
+Ensures that the value is not a multiple of factor
.
the number being multiplied
+this
+Ensures that the value is not a multiple of factor
.
the number being multiplied
+the name of the factor
+this
+Methods that validators for numbers that may be positive must contain.
+Methods that validators for numbers that may be zero must contain.
+Returns the string representation of a value.
+a value
+Optional
seen: Set<unknown>the objects that we've seen before
+the string representation of the value
+Validates the state of an unknown value or a value that does not have a specialized validator.
+
Validates the state of a value, recording failures without throwing an error.
+