quantique-field-validator 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +258 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
# quantique-field-validator
|
|
2
|
+
|
|
3
|
+
Enterprise-grade dynamic field validator for Indian and international form fields. Written in TypeScript with full type safety, ReDoS-safe regex handling, and tree-shakeable named exports.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/quantique-field-validator)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install quantique-field-validator
|
|
14
|
+
# or
|
|
15
|
+
yarn add quantique-field-validator
|
|
16
|
+
# or
|
|
17
|
+
pnpm add quantique-field-validator
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## Quick Start
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import dynamicValidator from 'quantique-field-validator';
|
|
26
|
+
|
|
27
|
+
dynamicValidator('test@example.com', 'email');
|
|
28
|
+
// { isValid: true, error: '' }
|
|
29
|
+
|
|
30
|
+
dynamicValidator('', 'email', { required: true });
|
|
31
|
+
// { isValid: false, error: 'This field is required' }
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## Usage
|
|
37
|
+
|
|
38
|
+
### `dynamicValidator(value, type, rules?, customValidator?)`
|
|
39
|
+
|
|
40
|
+
The single entry point that dispatches to the correct validator based on `type`.
|
|
41
|
+
|
|
42
|
+
| Parameter | Type | Description |
|
|
43
|
+
|-------------------|-------------------------|--------------------------------------------------------------|
|
|
44
|
+
| `value` | `unknown` | The raw input value |
|
|
45
|
+
| `type` | `ValidatorType` | Validator type key (see table below) |
|
|
46
|
+
| `rules` | `ValidationRules` | Optional rules (required, minLength, regex, etc.) |
|
|
47
|
+
| `customValidator` | `CustomValidatorFn` | Optional external validator — runs before type dispatch |
|
|
48
|
+
|
|
49
|
+
**Returns** `ValidationResult`:
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
{ isValid: boolean; error: string }
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## Supported Types
|
|
58
|
+
|
|
59
|
+
| Type | Description |
|
|
60
|
+
|-----------------|-------------------------------------------------------|
|
|
61
|
+
| `string` | Generic string with optional length and regex rules |
|
|
62
|
+
| `alphanumeric` | Letters and digits (lowercase by default) |
|
|
63
|
+
| `number` | Integer strings |
|
|
64
|
+
| `float` | Decimal number strings |
|
|
65
|
+
| `date` | Date strings (DD/MM/YYYY by default) |
|
|
66
|
+
| `name` | Personal or corporate name |
|
|
67
|
+
| `firstName` | First name (no spaces, no digits) |
|
|
68
|
+
| `middleName` | Middle name (no spaces, no digits) |
|
|
69
|
+
| `lastName` | Last name (no spaces, no digits) |
|
|
70
|
+
| `email` | Standard email address |
|
|
71
|
+
| `mobile` | Indian mobile number (10 digits, starts with 6–9) |
|
|
72
|
+
| `address` | Postal/street address |
|
|
73
|
+
| `password` | Password with configurable strength |
|
|
74
|
+
| `url` | HTTP/HTTPS URL |
|
|
75
|
+
| `pincode` | Indian 6-digit PIN code |
|
|
76
|
+
| `aadhaar` | 12-digit Aadhaar number |
|
|
77
|
+
| `pan` | PAN card (e.g. ABCDE1234F) |
|
|
78
|
+
| `gst` | GSTIN (15-character) |
|
|
79
|
+
| `ifsc` | Bank IFSC code |
|
|
80
|
+
| `chassis` | Vehicle chassis number (17-char VIN) |
|
|
81
|
+
| `engine` | Vehicle engine number |
|
|
82
|
+
| `rto` | RTO vehicle registration code |
|
|
83
|
+
| `ip` | IPv4 or IPv6 address |
|
|
84
|
+
| `custom` | Custom regex-based validator |
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
## Validation Rules
|
|
89
|
+
|
|
90
|
+
All rules are optional. Pass only what you need.
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
interface ValidationRules {
|
|
94
|
+
required?: boolean;
|
|
95
|
+
minLength?: number;
|
|
96
|
+
maxLength?: number;
|
|
97
|
+
minValue?: number; // number / float validators
|
|
98
|
+
maxValue?: number; // number / float validators
|
|
99
|
+
regex?: string; // custom regex string (compiled safely at runtime)
|
|
100
|
+
format?: string; // date format, e.g. "DD/MM/YYYY"
|
|
101
|
+
minAge?: number; // date: minimum age in years
|
|
102
|
+
maxAge?: number; // date: maximum age in years
|
|
103
|
+
minPlus?: number; // date: minimum days/months/years in the future
|
|
104
|
+
maxPlus?: number; // date: maximum days/months/years in the future
|
|
105
|
+
minMinus?: number; // date: minimum days/months/years in the past
|
|
106
|
+
maxMinus?: number; // date: maximum days/months/years in the past
|
|
107
|
+
unit?: 'day'|'days'|'month'|'months'|'year'|'years';
|
|
108
|
+
decimalPrecision?: number; // float: max decimal places
|
|
109
|
+
maxWholeDigits?: number; // float: max digits before decimal point
|
|
110
|
+
version?: 'v4' | 'v6'; // ip: restrict to IPv4 or IPv6
|
|
111
|
+
type?: 'user' | 'corporate'; // name: sub-type
|
|
112
|
+
strength?: 'basic' | 'medium' | 'strong'; // password strength
|
|
113
|
+
errorMessages?: ErrorMessages;
|
|
114
|
+
customValidator?: (value: string) => ValidationResult | boolean;
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
## Examples
|
|
121
|
+
|
|
122
|
+
### Required field
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
dynamicValidator('', 'email', { required: true });
|
|
126
|
+
// { isValid: false, error: 'This field is required' }
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Length constraints
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
dynamicValidator('Hi', 'string', { minLength: 5 });
|
|
133
|
+
// { isValid: false, error: 'Minimum 5 characters required' }
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Password strength
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
// basic — at least one letter and one digit
|
|
140
|
+
dynamicValidator('hello1', 'password', { strength: 'basic' });
|
|
141
|
+
// { isValid: true, error: '' }
|
|
142
|
+
|
|
143
|
+
// medium — uppercase + lowercase + digit
|
|
144
|
+
dynamicValidator('Hello1', 'password', { strength: 'medium' });
|
|
145
|
+
// { isValid: true, error: '' }
|
|
146
|
+
|
|
147
|
+
// strong — uppercase + lowercase + digit + special character
|
|
148
|
+
dynamicValidator('Hello1!', 'password', { strength: 'strong' });
|
|
149
|
+
// { isValid: true, error: '' }
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Date with age restriction
|
|
153
|
+
|
|
154
|
+
```ts
|
|
155
|
+
dynamicValidator('01/01/2010', 'date', { minAge: 18 });
|
|
156
|
+
// { isValid: false, error: 'Must be at least 18 years old' }
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### IP address version filter
|
|
160
|
+
|
|
161
|
+
```ts
|
|
162
|
+
dynamicValidator('192.168.1.1', 'ip', { version: 'v4' });
|
|
163
|
+
// { isValid: true, error: '' }
|
|
164
|
+
|
|
165
|
+
dynamicValidator('192.168.1.1', 'ip', { version: 'v6' });
|
|
166
|
+
// { isValid: false, error: 'Invalid IP address' }
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### Custom error messages
|
|
170
|
+
|
|
171
|
+
```ts
|
|
172
|
+
dynamicValidator('', 'mobile', {
|
|
173
|
+
required: true,
|
|
174
|
+
errorMessages: { required: 'Phone number is required' },
|
|
175
|
+
});
|
|
176
|
+
// { isValid: false, error: 'Phone number is required' }
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### Custom regex
|
|
180
|
+
|
|
181
|
+
```ts
|
|
182
|
+
dynamicValidator('ABC123', 'custom', {
|
|
183
|
+
regex: '^[A-Z]{3}[0-9]{3}$',
|
|
184
|
+
});
|
|
185
|
+
// { isValid: true, error: '' }
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### External custom validator
|
|
189
|
+
|
|
190
|
+
```ts
|
|
191
|
+
dynamicValidator('taken@example.com', 'email', {}, (value) => {
|
|
192
|
+
const takenEmails = ['taken@example.com'];
|
|
193
|
+
if (takenEmails.includes(value)) {
|
|
194
|
+
return { isValid: false, error: 'Email is already in use' };
|
|
195
|
+
}
|
|
196
|
+
return true;
|
|
197
|
+
});
|
|
198
|
+
// { isValid: false, error: 'Email is already in use' }
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### Inline custom validator (via rules)
|
|
202
|
+
|
|
203
|
+
```ts
|
|
204
|
+
dynamicValidator('hello', 'string', {
|
|
205
|
+
customValidator: (value) => value.startsWith('hello'),
|
|
206
|
+
});
|
|
207
|
+
// { isValid: true, error: '' }
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
## Tree-Shakeable Named Exports
|
|
213
|
+
|
|
214
|
+
Import individual validators directly to keep your bundle lean:
|
|
215
|
+
|
|
216
|
+
```ts
|
|
217
|
+
import { validateEmail, validateMobile, validateAadhaar } from 'quantique-field-validator';
|
|
218
|
+
|
|
219
|
+
validateEmail('user@example.com', {}, defaultErrorMessages, 'email');
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
All named validator exports follow the same signature:
|
|
223
|
+
|
|
224
|
+
```ts
|
|
225
|
+
(value: string, rules: ValidationRules, defaultErrorMessages: DefaultErrorMessagesFn, type: string) => ValidationResult
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
---
|
|
229
|
+
|
|
230
|
+
## TypeScript Types
|
|
231
|
+
|
|
232
|
+
All types are exported:
|
|
233
|
+
|
|
234
|
+
```ts
|
|
235
|
+
import type {
|
|
236
|
+
ValidationResult,
|
|
237
|
+
ValidationRules,
|
|
238
|
+
ErrorMessages,
|
|
239
|
+
ValidatorType,
|
|
240
|
+
ValidatorFn,
|
|
241
|
+
CustomValidatorFn,
|
|
242
|
+
DefaultErrorMessagesFn,
|
|
243
|
+
} from 'quantique-field-validator';
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
---
|
|
247
|
+
|
|
248
|
+
## Notes
|
|
249
|
+
|
|
250
|
+
- **Emoji rejection** — all validators block emoji characters by default.
|
|
251
|
+
- **ReDoS safety** — user-supplied `regex` strings are compiled with a length cap and timeout guard to prevent catastrophic backtracking.
|
|
252
|
+
- **Empty + not required** — passing an empty value with `required: false` (or omitted) always returns `{ isValid: true }` without running further checks.
|
|
253
|
+
|
|
254
|
+
---
|
|
255
|
+
|
|
256
|
+
## License
|
|
257
|
+
|
|
258
|
+
MIT © [Saket Brij Sinha](https://github.com/saketsinhaquantique)
|
package/package.json
CHANGED