# new ListValidator(messageopt)
The ListValidator validates that all elements in an array or Set match the expected types. It checks each element against a list of allowed class types and ensures type consistency. This validator is typically used with the @list decorator.
Validator for checking if elements in a list or set match expected types
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
message |
string
|
<optional> |
Custom error message to display when validation fails, defaults to |
Example
```typescript
// Create a list validator with default error message
const listValidator = new ListValidator();
// Create a list validator with custom error message
const customListValidator = new ListValidator("All items must be of the specified type");
// Validate a list
const options = { clazz: ["String", "Number"] };
const result = listValidator.hasErrors(["test", 123], options); // undefined (valid)
const invalidResult = listValidator.hasErrors([new Date()], options); // Returns error message (invalid)
```
Extends
Methods
# hasErrors(value, options) → {string|undefined}
Validates that each element in the provided array or Set matches one of the class types specified in the options. For object types, it checks the constructor name, and for primitive types, it compares against the lowercase type name.
Checks if all elements in a list or set match the expected types
Parameters:
Name | Type | Description |
---|---|---|
value |
Array.<any>
|
Set.<any>
|
The array or Set to validate |
options |
ListValidatorOptions
|
Configuration options containing the allowed class types |
- Overrides:
- See:
View Source validation/Validators/ListValidator.ts, line 125
Error message if validation fails, undefined if validation passes
string
|
undefined