@decaf-ts/decorator-validation
Version:
simple decorator based validation engine
88 lines • 3.87 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.URLValidator = void 0;
const constants_1 = require("./constants.cjs");
const PatternValidator_1 = require("./PatternValidator.cjs");
const decorators_1 = require("./decorators.cjs");
/**
* @description Validator for checking if a string is a valid URL
* @summary 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 {@link 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.
*
* @param {string} [message] - Custom error message to display when validation fails, defaults to {@link DEFAULT_ERROR_MESSAGES#URL}
*
* @class URLValidator
* @extends PatternValidator
*
* @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)
* ```
*
* @mermaid
* 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
*
* @category Validators
*/
let URLValidator = class URLValidator extends PatternValidator_1.PatternValidator {
constructor(message = constants_1.DEFAULT_ERROR_MESSAGES.URL) {
super(message);
}
/**
* @description Checks if a string is a valid URL
* @summary Validates that the provided string matches the URL pattern.
* This method extends the PatternValidator's hasErrors method by ensuring
* the URL pattern is used, even if not explicitly provided in the options.
*
* @param {string} value - The string to validate as a URL
* @param {PatternValidatorOptions} [options={}] - Optional configuration options
*
* @return {string | undefined} Error message if validation fails, undefined if validation passes
*
* @override
*
* @see PatternValidator#hasErrors
*/
hasErrors(value, options = {}) {
return super.hasErrors(value, {
...options,
message: options.message || this.message,
pattern: options.pattern || constants_1.DEFAULT_PATTERNS.URL,
});
}
};
exports.URLValidator = URLValidator;
exports.URLValidator = URLValidator = __decorate([
(0, decorators_1.validator)(constants_1.ValidationKeys.URL),
__metadata("design:paramtypes", [String])
], URLValidator);
//# sourceMappingURL=URLValidator.js.map