Class

ListValidator

ListValidator(messageopt)

Constructor

# 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

sequenceDiagram participant C as Client participant V as ListValidator C->>V: new ListValidator(message) C->>V: hasErrors(value, options) alt value is empty V-->>C: undefined (valid) else value has elements V->>V: Check each element's type alt All elements match allowed types V-->>C: undefined (valid) else Some elements don't match V-->>C: Error message end end
Parameters:
Name Type Attributes Description
message string <optional>

Custom error message to display when validation fails, defaults to DEFAULT_ERROR_MESSAGES#LIST

View Source validation/Validators/ListValidator.ts, line 11

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