Constructor
# new URLValidator(messageopt)
The URLValidator checks if a string matches a standard URL pattern. It extends the PatternValidator and uses a robust URL regex pattern to validate web addresses. The pattern is sourced from https://gist.github.com/dperini/729294 and is widely recognized for its accuracy in validating URLs. This validator is typically used with the @url decorator.
Validator for checking if a string is a valid URL
sequenceDiagram
participant C as Client
participant U as URLValidator
participant P as PatternValidator
C->>U: new URLValidator(message)
U->>P: super(message)
C->>U: hasErrors(value, options)
U->>P: super.hasErrors(value, options with URL pattern)
P-->>U: validation result
U-->>C: validation result
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
message |
string
|
<optional> |
Custom error message to display when validation fails, defaults to |
Example
```typescript
// Create a URL validator with default error message
const urlValidator = new URLValidator();
// Create a URL validator with custom error message
const customUrlValidator = new URLValidator("Please enter a valid web address");
// Validate a URL
const result = urlValidator.hasErrors("https://example.com"); // undefined (valid)
const invalidResult = urlValidator.hasErrors("not-a-url"); // Returns error message (invalid)
```