light-paradate 1.0.1 → 1.1.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 +9 -2
- package/dist/cjs/index.d.ts +2 -1
- package/dist/cjs/index.js +9 -5
- package/dist/esm/index.js +10 -5
- package/package.json +6 -1
package/README.md
CHANGED
|
@@ -16,12 +16,14 @@ This library helps validate incoming request data by checking for:
|
|
|
16
16
|
- Value inclusion in a predefined list
|
|
17
17
|
- Email format validation
|
|
18
18
|
|
|
19
|
+
## Support
|
|
20
|
+
- **TypeScript Support**: Includes type definitions.
|
|
21
|
+
- **CJM, ESM support**: Supports both CommonJS and ES modules.
|
|
22
|
+
|
|
19
23
|
## Features
|
|
20
24
|
- [validField](#validfield)
|
|
21
25
|
- [validEmail](#validemail)
|
|
22
26
|
|
|
23
|
-
**CJM, ESM support**: Supports both CommonJS and ES modules.
|
|
24
|
-
|
|
25
27
|
## Installation
|
|
26
28
|
|
|
27
29
|
```bash
|
|
@@ -51,6 +53,11 @@ const firstName = validField(req, 'firstName', { minLength: 5 });
|
|
|
51
53
|
- `req`: A request-like object with `query` and `body`.
|
|
52
54
|
- `fieldName`: The field name to retrieve from `req.query` or `req.body`.
|
|
53
55
|
- `options`: An optional object containing validation requirements.
|
|
56
|
+
Possible values for the `options` argument include:
|
|
57
|
+
- `required`: boolean - Indicates whether the field must have a non-empty value.
|
|
58
|
+
- `minLength`: number - Specifies the minimum length the field value should have.
|
|
59
|
+
- `maxLength`: number - Specifies the maximum length the field value can have.
|
|
60
|
+
- `includedIn`: string[] - An array of valid strings that the field value must match to pass validation.
|
|
54
61
|
|
|
55
62
|
#### Return Value
|
|
56
63
|
- If valid, returns the string or string array (in case of fields that use `[]` notation).
|
package/dist/cjs/index.d.ts
CHANGED
package/dist/cjs/index.js
CHANGED
|
@@ -5,13 +5,17 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.validField = exports.validEmail = void 0;
|
|
7
7
|
var _emailValidator = require("email-validator");
|
|
8
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
9
|
+
|
|
10
|
+
/* eslint-enable */
|
|
11
|
+
|
|
8
12
|
var validateRequired = function validateRequired(options, fieldName) {
|
|
9
13
|
if (options.required || options.minLength) throw new Error("".concat(fieldName, "Required"));
|
|
10
14
|
};
|
|
11
|
-
var validateLength = function validateLength(fieldValue, fieldName, minLength) {
|
|
12
|
-
if (minLength &&
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
var validateLength = function validateLength(fieldValue, fieldName, minLength, maxLength) {
|
|
16
|
+
if (!minLength && !maxLength) return;
|
|
17
|
+
if (minLength && fieldValue.length < minLength) throw new Error("".concat(fieldName, "TooShort"));
|
|
18
|
+
if (maxLength && fieldValue.length > maxLength) throw new Error("".concat(fieldName, "TooLong"));
|
|
15
19
|
};
|
|
16
20
|
var validateInclusion = function validateInclusion(fieldValue, fieldName, includedIn) {
|
|
17
21
|
if (fieldName.endsWith('[]')) {
|
|
@@ -38,7 +42,7 @@ var validField = exports.validField = function validField(req, fieldName, option
|
|
|
38
42
|
validateRequired(options, fieldName);
|
|
39
43
|
return undefined;
|
|
40
44
|
}
|
|
41
|
-
validateLength(fieldValue, fieldName, options === null || options === void 0 ? void 0 : options.minLength);
|
|
45
|
+
validateLength(fieldValue, fieldName, options === null || options === void 0 ? void 0 : options.minLength, options === null || options === void 0 ? void 0 : options.maxLength);
|
|
42
46
|
return validateInclusion(fieldValue, fieldName, options === null || options === void 0 ? void 0 : options.includedIn);
|
|
43
47
|
};
|
|
44
48
|
var validEmail = exports.validEmail = function validEmail(req) {
|
package/dist/esm/index.js
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import { validate as emailIsValid } from 'email-validator';
|
|
2
|
+
|
|
3
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
4
|
+
|
|
5
|
+
/* eslint-enable */
|
|
6
|
+
|
|
2
7
|
var validateRequired = function validateRequired(options, fieldName) {
|
|
3
8
|
if (options.required || options.minLength) throw new Error("".concat(fieldName, "Required"));
|
|
4
9
|
};
|
|
5
|
-
var validateLength = function validateLength(fieldValue, fieldName, minLength) {
|
|
6
|
-
if (minLength &&
|
|
7
|
-
|
|
8
|
-
|
|
10
|
+
var validateLength = function validateLength(fieldValue, fieldName, minLength, maxLength) {
|
|
11
|
+
if (!minLength && !maxLength) return;
|
|
12
|
+
if (minLength && fieldValue.length < minLength) throw new Error("".concat(fieldName, "TooShort"));
|
|
13
|
+
if (maxLength && fieldValue.length > maxLength) throw new Error("".concat(fieldName, "TooLong"));
|
|
9
14
|
};
|
|
10
15
|
var validateInclusion = function validateInclusion(fieldValue, fieldName, includedIn) {
|
|
11
16
|
if (fieldName.endsWith('[]')) {
|
|
@@ -32,7 +37,7 @@ export var validField = function validField(req, fieldName, options) {
|
|
|
32
37
|
validateRequired(options, fieldName);
|
|
33
38
|
return undefined;
|
|
34
39
|
}
|
|
35
|
-
validateLength(fieldValue, fieldName, options === null || options === void 0 ? void 0 : options.minLength);
|
|
40
|
+
validateLength(fieldValue, fieldName, options === null || options === void 0 ? void 0 : options.minLength, options === null || options === void 0 ? void 0 : options.maxLength);
|
|
36
41
|
return validateInclusion(fieldValue, fieldName, options === null || options === void 0 ? void 0 : options.includedIn);
|
|
37
42
|
};
|
|
38
43
|
export var validEmail = function validEmail(req) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "light-paradate",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Lightweight request params validator",
|
|
5
5
|
"author": "Tamas Kamaras",
|
|
6
6
|
"repository": {
|
|
@@ -37,6 +37,9 @@
|
|
|
37
37
|
"validator",
|
|
38
38
|
"email-validator",
|
|
39
39
|
"email validation",
|
|
40
|
+
"light",
|
|
41
|
+
"light-paradate",
|
|
42
|
+
"paradate",
|
|
40
43
|
"parameter validation",
|
|
41
44
|
"params",
|
|
42
45
|
"params validation",
|
|
@@ -51,6 +54,8 @@
|
|
|
51
54
|
"@babel/core": "^7.26.0",
|
|
52
55
|
"@babel/preset-env": "^7.26.0",
|
|
53
56
|
"@babel/preset-typescript": "^7.26.0",
|
|
57
|
+
"@typescript-eslint/eslint-plugin": "^8.21.0",
|
|
58
|
+
"eslint": "^9.19.0",
|
|
54
59
|
"jest": "^29.7.0",
|
|
55
60
|
"typescript": "^5.7.3"
|
|
56
61
|
}
|