@dimensional-innovations/vue-toolkit
Version:
A comprehensive toolkit for Vue development including components, composables, and utilities
153 lines (103 loc) • 4.01 kB
Markdown
# Validation Utilities
A collection of utility functions for validating data such as emails, URLs, passwords, and more.
## Import
```js
import {
isValidEmail,
isValidUrl,
isValidPassword,
sanitizeInput,
isInRange
} from '@dimensional-innovations/vue-toolkit';
```
## Usage
```js
// Validate an email address
isValidEmail('user@example.com'); // true
// Validate a URL
isValidUrl('https://example.com'); // true
// Validate a password with custom requirements
isValidPassword('P@ssw0rd', { minLength: 6, requireSpecial: true }); // { isValid: true, errors: [] }
// Sanitize potentially unsafe input
sanitizeInput("<script>alert('XSS');</script>Hello world"); // "Hello world"
// Check if a value is within range
isInRange(5, 0, 10); // true
```
## API Reference
### isValidEmail(email)
Validates an email address.
**Parameters:**
| Parameter | Type | Description |
| --------- | ------ | ----------------------------- |
| email | string | The email address to validate |
**Returns:** boolean - True if the email is valid
**Examples:**
```js
// Returns true
isValidEmail('user@example.com');
// Returns false
isValidEmail('invalid-email');
```
### isValidUrl(url, requireProtocol)
Validates a URL.
**Parameters:**
| Parameter | Type | Default | Description |
| --------------- | ------- | ------- | -------------------------------------------- |
| url | string | - | The URL to validate |
| requireProtocol | boolean | true | Whether to require the protocol (http/https) |
**Returns:** boolean - True if the URL is valid
**Examples:**
```js
// Returns true
isValidUrl('https://example.com');
// Returns false with requireProtocol=true, true with requireProtocol=false
isValidUrl('example.com', false);
```
### sanitizeInput(input)
Sanitizes a string by removing HTML tags and special characters.
**Parameters:**
| Parameter | Type | Description |
| --------- | ------ | ---------------------- |
| input | string | The string to sanitize |
**Returns:** string - The sanitized string
**Examples:**
```js
// Returns "Hello world"
sanitizeInput("<script>alert('XSS');</script>Hello world");
```
### isValidPassword(password, options)
Validates a password against common security rules.
**Parameters:**
| Parameter | Type | Default | Description |
| ------------------------ | ------- | ------- | -------------------------------------- |
| password | string | - | The password to validate |
| options | Object | {} | Validation options |
| options.minLength | number | 8 | Minimum password length |
| options.requireUppercase | boolean | true | Require at least one uppercase letter |
| options.requireLowercase | boolean | true | Require at least one lowercase letter |
| options.requireNumbers | boolean | true | Require at least one number |
| options.requireSpecial | boolean | true | Require at least one special character |
**Returns:** Object - Validation result with isValid flag and errors array
**Examples:**
```js
// Returns { isValid: true, errors: [] }
isValidPassword('P@ssw0rd');
// Returns { isValid: false, errors: ["Password must be at least 8 characters long"] }
isValidPassword('short', { minLength: 8 });
```
### isInRange(value, min, max)
Validates that a value is within a specified numeric range.
**Parameters:**
| Parameter | Type | Description |
| --------- | ------ | ------------------------- |
| value | number | The value to validate |
| min | number | The minimum allowed value |
| max | number | The maximum allowed value |
**Returns:** boolean - True if the value is within range
**Examples:**
```js
// Returns true
isInRange(5, 0, 10);
// Returns false
isInRange(15, 0, 10);
```