light-paradate 1.0.0 → 1.1.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/README.md +9 -0
- package/dist/{index.d.ts → cjs/index.d.ts} +8 -3
- package/dist/cjs/index.js +54 -0
- package/dist/esm/index.js +48 -0
- package/package.json +24 -6
- package/dist/index.js +0 -47
package/README.md
CHANGED
|
@@ -16,6 +16,10 @@ 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)
|
|
@@ -49,6 +53,11 @@ const firstName = validField(req, 'firstName', { minLength: 5 });
|
|
|
49
53
|
- `req`: A request-like object with `query` and `body`.
|
|
50
54
|
- `fieldName`: The field name to retrieve from `req.query` or `req.body`.
|
|
51
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.
|
|
52
61
|
|
|
53
62
|
#### Return Value
|
|
54
63
|
- If valid, returns the string or string array (in case of fields that use `[]` notation).
|
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
interface OptionsType {
|
|
2
|
-
required?: boolean;
|
|
3
2
|
includedIn?: string[];
|
|
4
3
|
minLength?: number;
|
|
4
|
+
maxLength?: number;
|
|
5
|
+
required?: boolean;
|
|
5
6
|
}
|
|
6
7
|
interface RequestType {
|
|
7
|
-
query:
|
|
8
|
-
|
|
8
|
+
query: {
|
|
9
|
+
[key: string]: string | undefined;
|
|
10
|
+
};
|
|
11
|
+
body: {
|
|
12
|
+
[key: string]: string | undefined;
|
|
13
|
+
};
|
|
9
14
|
}
|
|
10
15
|
export declare const validField: (req: RequestType, fieldName: string, options: OptionsType) => string | string[] | undefined;
|
|
11
16
|
export declare const validEmail: (req: RequestType) => string | undefined;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.validField = exports.validEmail = void 0;
|
|
7
|
+
var _emailValidator = require("email-validator");
|
|
8
|
+
var validateRequired = function validateRequired(options, fieldName) {
|
|
9
|
+
if (options.required || options.minLength) throw new Error("".concat(fieldName, "Required"));
|
|
10
|
+
};
|
|
11
|
+
var validateLength = function validateLength(fieldValue, fieldName, minLength, maxLength) {
|
|
12
|
+
if (!minLength && !maxLength) return;
|
|
13
|
+
if (minLength && fieldValue.length < minLength) throw new Error("".concat(fieldName, "TooShort"));
|
|
14
|
+
if (maxLength && fieldValue.length > maxLength) throw new Error("".concat(fieldName, "TooLong"));
|
|
15
|
+
};
|
|
16
|
+
var validateInclusion = function validateInclusion(fieldValue, fieldName, includedIn) {
|
|
17
|
+
if (fieldName.endsWith('[]')) {
|
|
18
|
+
// if the array has only 1 element, fieldValue is a string, not an array
|
|
19
|
+
var arrayValue = fieldValue.constructor === Array ? fieldValue : [fieldValue];
|
|
20
|
+
if (!arrayValue.every(function (value) {
|
|
21
|
+
return includedIn === null || includedIn === void 0 ? void 0 : includedIn.includes(value);
|
|
22
|
+
})) {
|
|
23
|
+
throw new Error("".concat(fieldName, "IsInvalid"));
|
|
24
|
+
}
|
|
25
|
+
return arrayValue;
|
|
26
|
+
} else {
|
|
27
|
+
if (includedIn && !includedIn.includes(fieldValue)) {
|
|
28
|
+
throw new Error("".concat(fieldName, "IsInvalid"));
|
|
29
|
+
}
|
|
30
|
+
return fieldValue;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var validField = exports.validField = function validField(req, fieldName, options) {
|
|
34
|
+
var _req$body;
|
|
35
|
+
if (!options) throw new Error('optionsRequired');
|
|
36
|
+
var fieldValue = req.query[fieldName] || ((_req$body = req.body) === null || _req$body === void 0 ? void 0 : _req$body[fieldName]);
|
|
37
|
+
if (!fieldValue) {
|
|
38
|
+
validateRequired(options, fieldName);
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
validateLength(fieldValue, fieldName, options === null || options === void 0 ? void 0 : options.minLength, options === null || options === void 0 ? void 0 : options.maxLength);
|
|
42
|
+
return validateInclusion(fieldValue, fieldName, options === null || options === void 0 ? void 0 : options.includedIn);
|
|
43
|
+
};
|
|
44
|
+
var validEmail = exports.validEmail = function validEmail(req) {
|
|
45
|
+
var _req$body2;
|
|
46
|
+
var email = req.query.email || ((_req$body2 = req.body) === null || _req$body2 === void 0 ? void 0 : _req$body2.email);
|
|
47
|
+
if (!email) {
|
|
48
|
+
throw new Error('emailRequired');
|
|
49
|
+
}
|
|
50
|
+
if (!(0, _emailValidator.validate)(email)) {
|
|
51
|
+
throw new Error('emailInvalid');
|
|
52
|
+
}
|
|
53
|
+
return email;
|
|
54
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { validate as emailIsValid } from 'email-validator';
|
|
2
|
+
var validateRequired = function validateRequired(options, fieldName) {
|
|
3
|
+
if (options.required || options.minLength) throw new Error("".concat(fieldName, "Required"));
|
|
4
|
+
};
|
|
5
|
+
var validateLength = function validateLength(fieldValue, fieldName, minLength, maxLength) {
|
|
6
|
+
if (!minLength && !maxLength) return;
|
|
7
|
+
if (minLength && fieldValue.length < minLength) throw new Error("".concat(fieldName, "TooShort"));
|
|
8
|
+
if (maxLength && fieldValue.length > maxLength) throw new Error("".concat(fieldName, "TooLong"));
|
|
9
|
+
};
|
|
10
|
+
var validateInclusion = function validateInclusion(fieldValue, fieldName, includedIn) {
|
|
11
|
+
if (fieldName.endsWith('[]')) {
|
|
12
|
+
// if the array has only 1 element, fieldValue is a string, not an array
|
|
13
|
+
var arrayValue = fieldValue.constructor === Array ? fieldValue : [fieldValue];
|
|
14
|
+
if (!arrayValue.every(function (value) {
|
|
15
|
+
return includedIn === null || includedIn === void 0 ? void 0 : includedIn.includes(value);
|
|
16
|
+
})) {
|
|
17
|
+
throw new Error("".concat(fieldName, "IsInvalid"));
|
|
18
|
+
}
|
|
19
|
+
return arrayValue;
|
|
20
|
+
} else {
|
|
21
|
+
if (includedIn && !includedIn.includes(fieldValue)) {
|
|
22
|
+
throw new Error("".concat(fieldName, "IsInvalid"));
|
|
23
|
+
}
|
|
24
|
+
return fieldValue;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
export var validField = function validField(req, fieldName, options) {
|
|
28
|
+
var _req$body;
|
|
29
|
+
if (!options) throw new Error('optionsRequired');
|
|
30
|
+
var fieldValue = req.query[fieldName] || ((_req$body = req.body) === null || _req$body === void 0 ? void 0 : _req$body[fieldName]);
|
|
31
|
+
if (!fieldValue) {
|
|
32
|
+
validateRequired(options, fieldName);
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
validateLength(fieldValue, fieldName, options === null || options === void 0 ? void 0 : options.minLength, options === null || options === void 0 ? void 0 : options.maxLength);
|
|
36
|
+
return validateInclusion(fieldValue, fieldName, options === null || options === void 0 ? void 0 : options.includedIn);
|
|
37
|
+
};
|
|
38
|
+
export var validEmail = function validEmail(req) {
|
|
39
|
+
var _req$body2;
|
|
40
|
+
var email = req.query.email || ((_req$body2 = req.body) === null || _req$body2 === void 0 ? void 0 : _req$body2.email);
|
|
41
|
+
if (!email) {
|
|
42
|
+
throw new Error('emailRequired');
|
|
43
|
+
}
|
|
44
|
+
if (!emailIsValid(email)) {
|
|
45
|
+
throw new Error('emailInvalid');
|
|
46
|
+
}
|
|
47
|
+
return email;
|
|
48
|
+
};
|
package/package.json
CHANGED
|
@@ -1,23 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "light-paradate",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Lightweight request params validator",
|
|
5
5
|
"author": "Tamas Kamaras",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "https://bitbucket.org/endeavia/light-paradate"
|
|
9
9
|
},
|
|
10
|
-
"main": "dist/index.js",
|
|
11
|
-
"
|
|
10
|
+
"main": "dist/cjs/index.js",
|
|
11
|
+
"module": "dist/esm/index.js",
|
|
12
12
|
"type": "module",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"require": "./dist/cjs/index.js",
|
|
16
|
+
"import": "./dist/esm/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
13
19
|
"files": [
|
|
14
|
-
"dist/index.js",
|
|
15
|
-
"dist/index.d.ts",
|
|
20
|
+
"dist/cjs/index.js",
|
|
21
|
+
"dist/cjs/index.d.ts",
|
|
22
|
+
"dist/esm/index.js",
|
|
16
23
|
"LICENSE",
|
|
17
24
|
"README.md"
|
|
18
25
|
],
|
|
19
26
|
"scripts": {
|
|
20
|
-
"
|
|
27
|
+
"type-check": "tsc",
|
|
28
|
+
"build:cjs": "babel src --out-dir dist/cjs --env-name cjs --extensions '.ts'",
|
|
29
|
+
"build:esm": "babel src --out-dir dist/esm --env-name esm --extensions '.ts'",
|
|
30
|
+
"build": "npm run type-check && npm run build:cjs && npm run build:esm",
|
|
21
31
|
"test": "npm run build && jest --config=tests/jest.config.js"
|
|
22
32
|
},
|
|
23
33
|
"keywords": [
|
|
@@ -27,6 +37,9 @@
|
|
|
27
37
|
"validator",
|
|
28
38
|
"email-validator",
|
|
29
39
|
"email validation",
|
|
40
|
+
"light",
|
|
41
|
+
"light-paradate",
|
|
42
|
+
"paradate",
|
|
30
43
|
"parameter validation",
|
|
31
44
|
"params",
|
|
32
45
|
"params validation",
|
|
@@ -37,7 +50,12 @@
|
|
|
37
50
|
"email-validator": "^2.0.4"
|
|
38
51
|
},
|
|
39
52
|
"devDependencies": {
|
|
53
|
+
"@babel/cli": "^7.26.4",
|
|
54
|
+
"@babel/core": "^7.26.0",
|
|
40
55
|
"@babel/preset-env": "^7.26.0",
|
|
56
|
+
"@babel/preset-typescript": "^7.26.0",
|
|
57
|
+
"@typescript-eslint/eslint-plugin": "^8.21.0",
|
|
58
|
+
"eslint": "^9.19.0",
|
|
41
59
|
"jest": "^29.7.0",
|
|
42
60
|
"typescript": "^5.7.3"
|
|
43
61
|
}
|
package/dist/index.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { validate as emailIsValid } from 'email-validator';
|
|
2
|
-
const validateRequired = (options, fieldName) => {
|
|
3
|
-
if (options.required || options.minLength)
|
|
4
|
-
throw new Error(`${fieldName}Required`);
|
|
5
|
-
};
|
|
6
|
-
const validateLength = (fieldValue, fieldName, minLength) => {
|
|
7
|
-
if (minLength && fieldValue.length < minLength) {
|
|
8
|
-
throw new Error(`${fieldName}TooShort`);
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
const validateInclusion = (fieldValue, fieldName, includedIn) => {
|
|
12
|
-
if (fieldName.endsWith('[]')) {
|
|
13
|
-
// if the array has only 1 element, fieldValue is a string, not an array
|
|
14
|
-
const arrayValue = (fieldValue.constructor === Array ? fieldValue : [fieldValue]);
|
|
15
|
-
if (!arrayValue.every(value => includedIn?.includes(value))) {
|
|
16
|
-
throw new Error(`${fieldName}IsInvalid`);
|
|
17
|
-
}
|
|
18
|
-
return arrayValue;
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
21
|
-
if (includedIn && !includedIn.includes(fieldValue)) {
|
|
22
|
-
throw new Error(`${fieldName}IsInvalid`);
|
|
23
|
-
}
|
|
24
|
-
return fieldValue;
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
export const validField = (req, fieldName, options) => {
|
|
28
|
-
if (!options)
|
|
29
|
-
throw new Error('optionsRequired');
|
|
30
|
-
const fieldValue = req.query[fieldName] || req.body?.[fieldName];
|
|
31
|
-
if (!fieldValue) {
|
|
32
|
-
validateRequired(options, fieldName);
|
|
33
|
-
return undefined;
|
|
34
|
-
}
|
|
35
|
-
validateLength(fieldValue, fieldName, options?.minLength);
|
|
36
|
-
return validateInclusion(fieldValue, fieldName, options?.includedIn);
|
|
37
|
-
};
|
|
38
|
-
export const validEmail = (req) => {
|
|
39
|
-
const email = req.query.email || req.body?.email;
|
|
40
|
-
if (!email) {
|
|
41
|
-
throw new Error('emailRequired');
|
|
42
|
-
}
|
|
43
|
-
if (!emailIsValid(email)) {
|
|
44
|
-
throw new Error('emailInvalid');
|
|
45
|
-
}
|
|
46
|
-
return email;
|
|
47
|
-
};
|