snap-validate 0.3.1 โ†’ 0.3.2

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 CHANGED
@@ -11,19 +11,20 @@ A lightning-fast, lightweight validation library for common patterns without hea
11
11
 
12
12
  ## Features
13
13
 
14
- - โšก **Lightning Fast**: Optimized for speed and performance
15
- - ๐Ÿš€ **Lightweight**: No external dependencies, minimal footprint
16
- - ๐Ÿ”ง **Flexible**: Chainable validation rules and custom validators
17
- - ๐Ÿ“ง **Common Patterns**: Email, phone, credit card, URL, password validation
18
- - ๐ŸŒ **International**: Support for different formats (US/International phone, postal codes)
19
- - ๐Ÿ”„ **Async Support**: Full async validation support for database checks and API calls
20
- - ๐ŸŽฏ **Conditional**: Advanced conditional validation with `when()` and `optional()`
21
- - ๐Ÿ› ๏ธ **Custom Validators**: Add your own sync and async validation logic
22
- - ๐Ÿ”’ **Security First**: Built-in protection against ReDoS attacks and unsafe regex patterns
23
- - ๐Ÿ›ก๏ธ **Timeout Protection**: Configurable timeout for regex operations to prevent DoS attacks
24
- - ๐Ÿงช **Well Tested**: Comprehensive test suite with high coverage
25
- - ๐Ÿ“ฆ **Easy Integration**: Works in Node.js and browsers
26
- - ๐Ÿ”— **Chainable API**: Intuitive fluent interface
14
+ - โšก **Lightning Fast**: Optimized for speed and performance
15
+ - ๐Ÿš€ **Lightweight**: No external dependencies, minimal footprint
16
+ - ๐Ÿ”ง **Flexible**: Chainable validation rules and custom validators
17
+ - ๐Ÿ“ง **Common Patterns**: Email, phone, credit card, URL, password validation
18
+ - ๐ŸŒ **International**: Support for different formats (US/International phone, postal codes)
19
+ - ๐Ÿ”„ **Async Support**: Full async validation support for database checks and API calls
20
+ - ๐ŸŽฏ **Conditional**: Advanced conditional validation with `when()` and `optional()`
21
+ - ๐Ÿ› ๏ธ **Custom Validators**: Add your own sync and async validation logic
22
+ - ๐Ÿ”’ **Security First**: Built-in protection against ReDoS attacks and unsafe regex patterns
23
+ - ๐Ÿ›ก๏ธ **Timeout Protection**: Configurable timeout for regex operations to prevent DoS attacks
24
+ - ๐Ÿงช **Well Tested**: Comprehensive test suite with high coverage
25
+ - ๐Ÿ“ฆ **Easy Integration**: Works in Node.js and browsers
26
+ - ๐Ÿ”— **Chainable API**: Intuitive fluent interface
27
+ - ๐Ÿ“˜ **TypeScript Support**: Complete TypeScript definitions with full IntelliSense support
27
28
 
28
29
  ## Installation
29
30
 
@@ -31,6 +32,15 @@ A lightning-fast, lightweight validation library for common patterns without hea
31
32
  npm install snap-validate
32
33
  ```
33
34
 
35
+ ### TypeScript
36
+
37
+ For TypeScript projects, types are included automatically:
38
+
39
+ ```bash
40
+ npm install snap-validate
41
+ # Types are included - no need for @types/snap-validate
42
+ ```
43
+
34
44
  ## Quick Start
35
45
 
36
46
  ```javascript
@@ -57,6 +67,52 @@ const result = validate(schema, data);
57
67
  console.log(result.isValid); // true
58
68
  ```
59
69
 
70
+ ## TypeScript Support
71
+
72
+ Snap Validate includes comprehensive TypeScript definitions for enhanced developer experience:
73
+
74
+ ```typescript
75
+ import { BaseValidator, validators, validate, ValidationResult } from 'snap-validate';
76
+
77
+ // Full type safety and auto-completion
78
+ const validator = new BaseValidator('test-value')
79
+ .required('This field is required')
80
+ .min(5, 'Must be at least 5 characters')
81
+ .pattern(/^[a-zA-Z]+$/, 'Only letters allowed');
82
+
83
+ // Type-safe result handling
84
+ const result: ValidationResult = validator.validate();
85
+
86
+ // Schema validation with types
87
+ interface UserData {
88
+ email: string;
89
+ phone: string;
90
+ password: string;
91
+ }
92
+
93
+ const userData: UserData = {
94
+ email: 'john@example.com',
95
+ phone: '1234567890',
96
+ password: 'StrongPass123'
97
+ };
98
+
99
+ const schema = {
100
+ email: validators.email,
101
+ phone: (value: string) => validators.phone(value, 'us'),
102
+ password: validators.password
103
+ };
104
+
105
+ const result = validate(schema, userData);
106
+ ```
107
+
108
+ Features:
109
+
110
+ - Complete type definitions for all classes and functions
111
+ - IntelliSense support in VS Code, WebStorm, and other editors
112
+ - Compile-time validation prevents common usage errors
113
+ - Generic support for flexible validation workflows
114
+ - Rich JSDoc comments for comprehensive documentation
115
+
60
116
  ## Security Features
61
117
 
62
118
  ### ReDoS Protection
@@ -397,6 +453,14 @@ if (!unsafeResult.isValid) {
397
453
  - `validators.numeric(value)`
398
454
  - `validators.zipCode(value, country?)`
399
455
 
456
+ ### TypeScript Types
457
+
458
+ - `ValidationResult` - Interface for validation results
459
+ - `ValidatorFunction` - Type for validator functions used in schemas
460
+ - `ValidationSchema` - Type for validation schema objects
461
+ - `PasswordOptions` - Interface for password validation configuration
462
+ - `BaseValidator<T>` - Generic base validator class
463
+
400
464
  ### Validation Functions
401
465
 
402
466
  - `validate(schema, data)` - Synchronous schema validation
@@ -448,6 +512,12 @@ npm run format
448
512
 
449
513
  # Security audit
450
514
  npm audit
515
+
516
+ # Type checking (for TypeScript users)
517
+ npm run type-check
518
+
519
+ # Validate TypeScript definitions
520
+ npm run validate-types
451
521
  ```
452
522
 
453
523
  ## License
@@ -460,4 +530,4 @@ See [CHANGELOG.md](CHANGELOG.md) for a detailed history of changes.
460
530
 
461
531
  ---
462
532
 
463
- Made with โšก by [Ramachandra Anirudh Vemulapalli](https://github.com/aniru-dh21)
533
+ Made with โšก by [Ramachandra Anirudh Vemulapalli](https://github.com/aniru-dh21)
package/package.json CHANGED
@@ -1,9 +1,17 @@
1
1
  {
2
2
  "name": "snap-validate",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "Lightweight validation library for common patterns without heavy dependencies",
5
5
  "main": "src/index.js",
6
6
  "types": "types/index.d.ts",
7
+ "typings": "types/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./types/index.d.ts",
11
+ "require": "./src/index.js",
12
+ "import": "./src/index.js"
13
+ }
14
+ },
7
15
  "scripts": {
8
16
  "test": "jest",
9
17
  "test:watch": "jest --watch",
@@ -14,7 +22,12 @@
14
22
  "prepare": "husky install",
15
23
  "prepublishOnly": "npm test && npm run lint",
16
24
  "build": "echo 'No build step required for this library'",
17
- "example": "node examples/basic-usage.js"
25
+ "example": "node examples/basic-usage.js",
26
+ "type-check": "tsc --noEmit",
27
+ "test-types": "tsc test-types.ts --noEmit",
28
+ "validate-types": "tsc --noEmit && echo 'TypeScript definitions are valid!'",
29
+ "lint-types": "eslint types/**/*.ts",
30
+ "check-exports": "node -e \"console.log(Object.keys(require('./src/index.js')))\""
18
31
  },
19
32
  "keywords": [
20
33
  "validation",
package/src/index.js CHANGED
@@ -1,9 +1,8 @@
1
1
  /**
2
2
  * Snap Validate - Enhanced Lightweight validator library
3
- * @version 0.3.1 - Security Fixes
3
+ * @version 0.3.2 - Typescript Patch
4
4
  */
5
5
 
6
- // Utility function to safely test regex with timeout protection
7
6
  // Utility function to safely test regex with timeout protection
8
7
  const safeRegexTest = (regex, str, timeoutMs = 1000) => {
9
8
  return new Promise((resolve, reject) => {
package/types/index.d.ts CHANGED
@@ -63,6 +63,7 @@ declare module 'snap-validate' {
63
63
  rules: Array<() => ValidationResult>;
64
64
  asyncRules: Array<() => Promise<ValidationResult>>;
65
65
  isOptional: boolean;
66
+ regexTimeout: number;
66
67
 
67
68
  /**
68
69
  * Make field required
@@ -74,6 +75,11 @@ declare module 'snap-validate' {
74
75
  */
75
76
  optional(): BaseValidator;
76
77
 
78
+ /**
79
+ * Set timeout for regex operations in milliseconds
80
+ */
81
+ setRegexTimeout(timeoutMs: number): BaseValidator;
82
+
77
83
  /**
78
84
  * Set minimum length/value
79
85
  */
@@ -85,10 +91,15 @@ declare module 'snap-validate' {
85
91
  max(length: number, message?: string): BaseValidator;
86
92
 
87
93
  /**
88
- * Validate against regex pattern
94
+ * Validate against regex pattern (synchronous)
89
95
  */
90
96
  pattern(regex: RegExp, message?: string): BaseValidator;
91
97
 
98
+ /**
99
+ * Validate against regex pattern with timeout protection (asynchronous)
100
+ */
101
+ patternAsync(regex: RegExp, message?: string): BaseValidator;
102
+
92
103
  /**
93
104
  * Conditional validation
94
105
  */
@@ -177,4 +188,27 @@ declare module 'snap-validate' {
177
188
  schema: Schema,
178
189
  data: { [key: string]: any }
179
190
  ): Promise<SchemaValidationResult>;
191
+
192
+ /**
193
+ * Safely test regex with timeout protection (asynchronous)
194
+ */
195
+ export function safeRegexTest(
196
+ regex: RegExp,
197
+ str: string,
198
+ timeoutMs?: number
199
+ ): Promise<boolean>;
200
+
201
+ /**
202
+ * Safely test regex with input length protection (synchronous)
203
+ */
204
+ export function safeRegexTestSync(
205
+ regex: RegExp,
206
+ str: string,
207
+ maxLength?: number
208
+ ): boolean;
209
+
210
+ /**
211
+ * Check if a regex pattern is safe to use (ReDoS protection)
212
+ */
213
+ export function isRegexSafe(regex: RegExp): boolean;
180
214
  }