light-paradate 1.0.0 → 1.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 +2 -0
- package/dist/{index.d.ts → cjs/index.d.ts} +6 -2
- package/dist/cjs/index.js +54 -0
- package/dist/esm/index.js +48 -0
- package/package.json +19 -6
- package/dist/index.js +0 -47
package/README.md
CHANGED
|
@@ -4,8 +4,12 @@ interface OptionsType {
|
|
|
4
4
|
minLength?: number;
|
|
5
5
|
}
|
|
6
6
|
interface RequestType {
|
|
7
|
-
query:
|
|
8
|
-
|
|
7
|
+
query: {
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
};
|
|
10
|
+
body: {
|
|
11
|
+
[key: string]: any;
|
|
12
|
+
};
|
|
9
13
|
}
|
|
10
14
|
export declare const validField: (req: RequestType, fieldName: string, options: OptionsType) => string | string[] | undefined;
|
|
11
15
|
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) {
|
|
12
|
+
if (minLength && fieldValue.length < minLength) {
|
|
13
|
+
throw new Error("".concat(fieldName, "TooShort"));
|
|
14
|
+
}
|
|
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);
|
|
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) {
|
|
6
|
+
if (minLength && fieldValue.length < minLength) {
|
|
7
|
+
throw new Error("".concat(fieldName, "TooShort"));
|
|
8
|
+
}
|
|
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);
|
|
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.0.
|
|
3
|
+
"version": "1.0.1",
|
|
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": [
|
|
@@ -37,7 +47,10 @@
|
|
|
37
47
|
"email-validator": "^2.0.4"
|
|
38
48
|
},
|
|
39
49
|
"devDependencies": {
|
|
50
|
+
"@babel/cli": "^7.26.4",
|
|
51
|
+
"@babel/core": "^7.26.0",
|
|
40
52
|
"@babel/preset-env": "^7.26.0",
|
|
53
|
+
"@babel/preset-typescript": "^7.26.0",
|
|
41
54
|
"jest": "^29.7.0",
|
|
42
55
|
"typescript": "^5.7.3"
|
|
43
56
|
}
|
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
|
-
};
|