validatorcraft 1.0.0
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/LICENSE +21 -0
- package/README.md +118 -0
- package/package.json +38 -0
- package/src/color.js +13 -0
- package/src/date.js +16 -0
- package/src/email.js +13 -0
- package/src/index.js +30 -0
- package/src/json.js +16 -0
- package/src/number.js +13 -0
- package/src/password.js +19 -0
- package/src/phone.js +13 -0
- package/src/string.js +73 -0
- package/src/url.js +16 -0
- package/src/username.js +12 -0
- package/src/uuid.js +12 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Aman Sharma
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# validatorcraft
|
|
2
|
+
|
|
3
|
+
A lightweight, zero-dependency JavaScript validation library for Node.js.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Zero Dependencies**: Lightweight and fast.
|
|
8
|
+
- **Node.js Ready**: Works with Node >= 18.
|
|
9
|
+
- **Pure Functions**: Returns only booleans, throws no unnecessary exceptions.
|
|
10
|
+
- **Comprehensive**: Validate emails, phones, passwords, UUIDs, hex colors, and more.
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install validatorcraft
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
```javascript
|
|
21
|
+
const validator = require('validatorcraft');
|
|
22
|
+
|
|
23
|
+
// Email Validation
|
|
24
|
+
validator.isEmail('test@example.com'); // true
|
|
25
|
+
|
|
26
|
+
// Phone Validation
|
|
27
|
+
validator.isPhone('+1234567890'); // true
|
|
28
|
+
|
|
29
|
+
// Password Validation
|
|
30
|
+
validator.isStrongPassword('StrongP@ssw0rd'); // true
|
|
31
|
+
|
|
32
|
+
// Hex Color
|
|
33
|
+
validator.isHexColor('#ffffff'); // true
|
|
34
|
+
|
|
35
|
+
// Length checks
|
|
36
|
+
validator.betweenLength('hello', 3, 10); // true
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## API Reference
|
|
40
|
+
|
|
41
|
+
### `isEmail(email)`
|
|
42
|
+
Validates proper email format.
|
|
43
|
+
- **Returns**: `boolean`
|
|
44
|
+
|
|
45
|
+
### `isPhone(phone)`
|
|
46
|
+
Validates phone numbers, including international numbers beginning with `+`.
|
|
47
|
+
- **Returns**: `boolean`
|
|
48
|
+
|
|
49
|
+
### `isStrongPassword(password)`
|
|
50
|
+
Validates passwords based on strong criteria:
|
|
51
|
+
- Minimum 8 characters
|
|
52
|
+
- At least one uppercase letter
|
|
53
|
+
- At least one lowercase letter
|
|
54
|
+
- At least one number
|
|
55
|
+
- At least one special character
|
|
56
|
+
- **Returns**: `boolean`
|
|
57
|
+
|
|
58
|
+
### `isUsername(username)`
|
|
59
|
+
Validates a username based on:
|
|
60
|
+
- 3–20 characters long
|
|
61
|
+
- Contains only letters, numbers, and underscores
|
|
62
|
+
- **Returns**: `boolean`
|
|
63
|
+
|
|
64
|
+
### `isURL(url)`
|
|
65
|
+
Validates standard `http` and `https` URLs.
|
|
66
|
+
- **Returns**: `boolean`
|
|
67
|
+
|
|
68
|
+
### `isNumeric(value)`
|
|
69
|
+
Returns true only if the string contains digits.
|
|
70
|
+
- **Returns**: `boolean`
|
|
71
|
+
|
|
72
|
+
### `isAlpha(value)`
|
|
73
|
+
Returns true only if the string contains alphabetic characters.
|
|
74
|
+
- **Returns**: `boolean`
|
|
75
|
+
|
|
76
|
+
### `isAlphaNumeric(value)`
|
|
77
|
+
Returns true only if the string contains alphanumeric characters.
|
|
78
|
+
- **Returns**: `boolean`
|
|
79
|
+
|
|
80
|
+
### `isEmpty(value)`
|
|
81
|
+
Returns true for `""`, `null`, and `undefined`.
|
|
82
|
+
- **Returns**: `boolean`
|
|
83
|
+
|
|
84
|
+
### `minLength(str, len)`
|
|
85
|
+
Checks if the string length is at least `len`.
|
|
86
|
+
- **Returns**: `boolean`
|
|
87
|
+
|
|
88
|
+
### `maxLength(str, len)`
|
|
89
|
+
Checks if the string length is at most `len`.
|
|
90
|
+
- **Returns**: `boolean`
|
|
91
|
+
|
|
92
|
+
### `betweenLength(str, min, max)`
|
|
93
|
+
Checks if the string length is between `min` and `max` (inclusive).
|
|
94
|
+
- **Returns**: `boolean`
|
|
95
|
+
|
|
96
|
+
### `isJSON(str)`
|
|
97
|
+
Checks if a string is valid JSON.
|
|
98
|
+
- **Returns**: `boolean`
|
|
99
|
+
|
|
100
|
+
### `isUUID(str)`
|
|
101
|
+
Checks if a string is a valid UUID v4.
|
|
102
|
+
- **Returns**: `boolean`
|
|
103
|
+
|
|
104
|
+
### `isHexColor(color)`
|
|
105
|
+
Checks if a string is a valid hex color (e.g., `#fff`, `#000000`).
|
|
106
|
+
- **Returns**: `boolean`
|
|
107
|
+
|
|
108
|
+
### `isDate(date)`
|
|
109
|
+
Checks if a string is a valid ISO date.
|
|
110
|
+
- **Returns**: `boolean`
|
|
111
|
+
|
|
112
|
+
## Contributing
|
|
113
|
+
|
|
114
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
115
|
+
|
|
116
|
+
## License
|
|
117
|
+
|
|
118
|
+
This project is licensed under the MIT License.
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "validatorcraft",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A lightweight, dependency-free JavaScript validation library for Node.js.",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"src"
|
|
8
|
+
],
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "node --test tests/*.test.js"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"validator",
|
|
14
|
+
"validation",
|
|
15
|
+
"validate",
|
|
16
|
+
"dependency-free",
|
|
17
|
+
"string",
|
|
18
|
+
"email",
|
|
19
|
+
"password",
|
|
20
|
+
"phone",
|
|
21
|
+
"url",
|
|
22
|
+
"uuid",
|
|
23
|
+
"json"
|
|
24
|
+
],
|
|
25
|
+
"author": "Aman Sharma",
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/Amansharmacs1/validatorcraft.git"
|
|
30
|
+
},
|
|
31
|
+
"homepage": "https://github.com/Amansharmacs1/validatorcraft#readme",
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/Amansharmacs1/validatorcraft/issues"
|
|
34
|
+
},
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">=18.0.0"
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/color.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if a string is a valid hex color.
|
|
3
|
+
* Supports #RGB, #RGBA, #RRGGBB, #RRGGBBAA.
|
|
4
|
+
* @param {string} color - The string to check.
|
|
5
|
+
* @returns {boolean}
|
|
6
|
+
*/
|
|
7
|
+
function isHexColor(color) {
|
|
8
|
+
if (typeof color !== 'string') return false;
|
|
9
|
+
const regex = /^#([A-Fa-f0-9]{3,4}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$/;
|
|
10
|
+
return regex.test(color);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
module.exports = { isHexColor };
|
package/src/date.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if a string is a valid ISO date.
|
|
3
|
+
* @param {string} date - The string to check.
|
|
4
|
+
* @returns {boolean}
|
|
5
|
+
*/
|
|
6
|
+
function isDate(date) {
|
|
7
|
+
if (typeof date !== 'string') return false;
|
|
8
|
+
// Basic ISO format regex, e.g. YYYY-MM-DD or YYYY-MM-DDTHH:mm:ss.sssZ
|
|
9
|
+
const regex = /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}(\.\d{1,3})?(Z|[+-]\d{2}:\d{2})?)?$/;
|
|
10
|
+
if (!regex.test(date)) return false;
|
|
11
|
+
|
|
12
|
+
const parsedDate = new Date(date);
|
|
13
|
+
return !isNaN(parsedDate.getTime());
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
module.exports = { isDate };
|
package/src/email.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates a proper email format.
|
|
3
|
+
* @param {string} email - The email to validate.
|
|
4
|
+
* @returns {boolean} True if the email is valid, false otherwise.
|
|
5
|
+
*/
|
|
6
|
+
function isEmail(email) {
|
|
7
|
+
if (typeof email !== 'string') return false;
|
|
8
|
+
// A basic regex for email validation
|
|
9
|
+
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
10
|
+
return regex.test(email);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
module.exports = { isEmail };
|
package/src/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const { isEmail } = require('./email');
|
|
2
|
+
const { isPhone } = require('./phone');
|
|
3
|
+
const { isStrongPassword } = require('./password');
|
|
4
|
+
const { isUsername } = require('./username');
|
|
5
|
+
const { isURL } = require('./url');
|
|
6
|
+
const { isNumeric } = require('./number');
|
|
7
|
+
const { isAlpha, isAlphaNumeric, isEmpty, minLength, maxLength, betweenLength } = require('./string');
|
|
8
|
+
const { isJSON } = require('./json');
|
|
9
|
+
const { isUUID } = require('./uuid');
|
|
10
|
+
const { isHexColor } = require('./color');
|
|
11
|
+
const { isDate } = require('./date');
|
|
12
|
+
|
|
13
|
+
module.exports = {
|
|
14
|
+
isEmail,
|
|
15
|
+
isPhone,
|
|
16
|
+
isStrongPassword,
|
|
17
|
+
isUsername,
|
|
18
|
+
isURL,
|
|
19
|
+
isNumeric,
|
|
20
|
+
isAlpha,
|
|
21
|
+
isAlphaNumeric,
|
|
22
|
+
isEmpty,
|
|
23
|
+
minLength,
|
|
24
|
+
maxLength,
|
|
25
|
+
betweenLength,
|
|
26
|
+
isJSON,
|
|
27
|
+
isUUID,
|
|
28
|
+
isHexColor,
|
|
29
|
+
isDate
|
|
30
|
+
};
|
package/src/json.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if a string is valid JSON.
|
|
3
|
+
* @param {string} str - The string to check.
|
|
4
|
+
* @returns {boolean}
|
|
5
|
+
*/
|
|
6
|
+
function isJSON(str) {
|
|
7
|
+
if (typeof str !== 'string') return false;
|
|
8
|
+
try {
|
|
9
|
+
JSON.parse(str);
|
|
10
|
+
return true;
|
|
11
|
+
} catch (e) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
module.exports = { isJSON };
|
package/src/number.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns true only if the string contains digits.
|
|
3
|
+
* @param {string} value - The value to check.
|
|
4
|
+
* @returns {boolean}
|
|
5
|
+
*/
|
|
6
|
+
function isNumeric(value) {
|
|
7
|
+
if (typeof value !== 'string') return false;
|
|
8
|
+
if (value.length === 0) return false;
|
|
9
|
+
const regex = /^\d+$/;
|
|
10
|
+
return regex.test(value);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
module.exports = { isNumeric };
|
package/src/password.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates a strong password.
|
|
3
|
+
* Minimum 8 characters, at least one uppercase, one lowercase, one number, and one special character.
|
|
4
|
+
* @param {string} password - The password to validate.
|
|
5
|
+
* @returns {boolean} True if strong, false otherwise.
|
|
6
|
+
*/
|
|
7
|
+
function isStrongPassword(password) {
|
|
8
|
+
if (typeof password !== 'string') return false;
|
|
9
|
+
if (password.length < 8) return false;
|
|
10
|
+
|
|
11
|
+
const hasUppercase = /[A-Z]/.test(password);
|
|
12
|
+
const hasLowercase = /[a-z]/.test(password);
|
|
13
|
+
const hasNumber = /[0-9]/.test(password);
|
|
14
|
+
const hasSpecial = /[^A-Za-z0-9]/.test(password);
|
|
15
|
+
|
|
16
|
+
return hasUppercase && hasLowercase && hasNumber && hasSpecial;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
module.exports = { isStrongPassword };
|
package/src/phone.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates international phone numbers, including those beginning with +.
|
|
3
|
+
* @param {string} phone - The phone number to validate.
|
|
4
|
+
* @returns {boolean} True if valid, false otherwise.
|
|
5
|
+
*/
|
|
6
|
+
function isPhone(phone) {
|
|
7
|
+
if (typeof phone !== 'string') return false;
|
|
8
|
+
// Allows optional +, followed by 7 to 15 digits
|
|
9
|
+
const regex = /^\+?[0-9]{7,15}$/;
|
|
10
|
+
return regex.test(phone.replace(/[\s\-\(\)]/g, ''));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
module.exports = { isPhone };
|
package/src/string.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns true if the string contains only alphabetic characters.
|
|
3
|
+
* @param {string} value - The value to check.
|
|
4
|
+
* @returns {boolean}
|
|
5
|
+
*/
|
|
6
|
+
function isAlpha(value) {
|
|
7
|
+
if (typeof value !== 'string') return false;
|
|
8
|
+
if (value.length === 0) return false;
|
|
9
|
+
return /^[a-zA-Z]+$/.test(value);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Returns true if the string contains only alphanumeric characters.
|
|
14
|
+
* @param {string} value - The value to check.
|
|
15
|
+
* @returns {boolean}
|
|
16
|
+
*/
|
|
17
|
+
function isAlphaNumeric(value) {
|
|
18
|
+
if (typeof value !== 'string') return false;
|
|
19
|
+
if (value.length === 0) return false;
|
|
20
|
+
return /^[a-zA-Z0-9]+$/.test(value);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Returns true for "", null, and undefined.
|
|
25
|
+
* @param {any} value - The value to check.
|
|
26
|
+
* @returns {boolean}
|
|
27
|
+
*/
|
|
28
|
+
function isEmpty(value) {
|
|
29
|
+
return value === "" || value === null || value === undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Checks if string length is at least `len`.
|
|
34
|
+
* @param {string} str
|
|
35
|
+
* @param {number} len
|
|
36
|
+
* @returns {boolean}
|
|
37
|
+
*/
|
|
38
|
+
function minLength(str, len) {
|
|
39
|
+
if (typeof str !== 'string' || typeof len !== 'number') return false;
|
|
40
|
+
return str.length >= len;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Checks if string length is at most `len`.
|
|
45
|
+
* @param {string} str
|
|
46
|
+
* @param {number} len
|
|
47
|
+
* @returns {boolean}
|
|
48
|
+
*/
|
|
49
|
+
function maxLength(str, len) {
|
|
50
|
+
if (typeof str !== 'string' || typeof len !== 'number') return false;
|
|
51
|
+
return str.length <= len;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Checks if string length is between `min` and `max` (inclusive).
|
|
56
|
+
* @param {string} str
|
|
57
|
+
* @param {number} min
|
|
58
|
+
* @param {number} max
|
|
59
|
+
* @returns {boolean}
|
|
60
|
+
*/
|
|
61
|
+
function betweenLength(str, min, max) {
|
|
62
|
+
if (typeof str !== 'string' || typeof min !== 'number' || typeof max !== 'number') return false;
|
|
63
|
+
return str.length >= min && str.length <= max;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
module.exports = {
|
|
67
|
+
isAlpha,
|
|
68
|
+
isAlphaNumeric,
|
|
69
|
+
isEmpty,
|
|
70
|
+
minLength,
|
|
71
|
+
maxLength,
|
|
72
|
+
betweenLength
|
|
73
|
+
};
|
package/src/url.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates http and https URLs.
|
|
3
|
+
* @param {string} url - The URL to validate.
|
|
4
|
+
* @returns {boolean} True if valid, false otherwise.
|
|
5
|
+
*/
|
|
6
|
+
function isURL(url) {
|
|
7
|
+
if (typeof url !== 'string') return false;
|
|
8
|
+
try {
|
|
9
|
+
const parsed = new URL(url);
|
|
10
|
+
return parsed.protocol === 'http:' || parsed.protocol === 'https:';
|
|
11
|
+
} catch (e) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
module.exports = { isURL };
|
package/src/username.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates a username (3-20 chars, letters, numbers, underscore only).
|
|
3
|
+
* @param {string} username - The username to validate.
|
|
4
|
+
* @returns {boolean} True if valid, false otherwise.
|
|
5
|
+
*/
|
|
6
|
+
function isUsername(username) {
|
|
7
|
+
if (typeof username !== 'string') return false;
|
|
8
|
+
const regex = /^[a-zA-Z0-9_]{3,20}$/;
|
|
9
|
+
return regex.test(username);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
module.exports = { isUsername };
|
package/src/uuid.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if a string is a valid UUID v4.
|
|
3
|
+
* @param {string} str - The string to check.
|
|
4
|
+
* @returns {boolean}
|
|
5
|
+
*/
|
|
6
|
+
function isUUID(str) {
|
|
7
|
+
if (typeof str !== 'string') return false;
|
|
8
|
+
const regex = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
9
|
+
return regex.test(str);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
module.exports = { isUUID };
|