@chicane-ai/box-regex-match
v1.0.0
Published
RegexMatch box for Chicane - regular expression validation
Maintainers
Readme
Chicane Box: RegexMatch
| Developed by | Chicane Team | | --- | --- | | Date of development | Jan 2024 | | Validator type | Format | | Blog | Chicane Blog | | License | MIT | | Input/Output | Input |
Box for regular expression validation in Chicane. Allows validating strings using regex patterns with flexible configurations.
🚀 Features
- ✅ Regex Validation: Complete regular expression support
- ✅ Flexible Configuration: Flags and custom messages
- ✅ Error Handling: Invalid regex validation
- ✅ TypeScript: Fully typed
- ✅ Easy Integration: Compatible with SafetyCar
📦 Installation
npm install @chicane-ai/box-regex-match🎯 Basic Usage
import { SafetyCar, OnFailAction } from '@chicane-ai/core';
import { RegexMatch } from '@chicane-ai/box-regex-match';
const validator = new SafetyCar().use(
new RegexMatch(),
{ regex: "\\d{3}-\\d{3}-\\d{4}" },
OnFailAction.EXCEPTION
);
// Validate phone
try {
const result = validator.validate("123-456-7890");
console.log("Valid:", result.valid);
} catch (error) {
console.error("Invalid:", error.message);
}🔧 Configuration
RegexMatchConfig
interface RegexMatchConfig {
regex: string | RegExp; // Regex pattern
flags?: string; // Regex flags (g, i, m, etc.)
message?: string; // Custom error message
}📝 Examples
Email Validation
const emailValidator = new SafetyCar().use(
new RegexMatch(),
{
regex: "^[^@]+@[^@]+\\.[^@]+$",
message: "Invalid email format"
},
OnFailAction.EXCEPTION
);Phone Number Validation
const phoneValidator = new SafetyCar().use(
new RegexMatch(),
{
regex: "\\d{3}-\\d{3}-\\d{4}",
message: "Phone must be in format XXX-XXX-XXXX"
},
OnFailAction.REFRAIN
);Validation with Flags
const caseInsensitiveValidator = new SafetyCar().use(
new RegexMatch(),
{
regex: "hello",
flags: "i" // Case insensitive
},
OnFailAction.EXCEPTION
);
// Accepts: "hello", "Hello", "HELLO"