light-paradate 2.0.0 → 2.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 +8 -8
- package/dist/cjs/index.js +12 -6
- package/dist/esm/index.js +12 -6
- package/package.json +7 -2
- package/dist/cjs/index.d.ts +0 -27
package/README.md
CHANGED
|
@@ -31,29 +31,29 @@ npm install light-paradate
|
|
|
31
31
|
```
|
|
32
32
|
Or with Yarn:
|
|
33
33
|
```bash
|
|
34
|
-
const parentId = validField(req, 'parent_id', { numericality: { onlyInteger: true, greaterThan: 0 } });
|
|
35
34
|
yarn add light-paradate
|
|
36
35
|
```
|
|
37
36
|
|
|
38
37
|
## Usage
|
|
39
38
|
### validField
|
|
40
39
|
|
|
41
|
-
`validField(
|
|
40
|
+
`validField(reqOrFieldValues, fieldName, options)`
|
|
42
41
|
|
|
43
|
-
Extracts a field from `req.query
|
|
42
|
+
Extracts a field from `req.params`, `req.query`, or `req.body`, then runs validations according to the provided options. You can either pass one of those field containers directly as the first argument, or a `req` object. In that case, params, query, and body will be searched for the passed fieldName.
|
|
44
43
|
|
|
45
44
|
example:
|
|
46
45
|
```js
|
|
47
46
|
const uuid = validField(req, 'uuid', { minLength: 36 });
|
|
47
|
+
const parentId = validField(req.params, 'parent_id', { numericality: { onlyInteger: true, greaterThan: 0 } });
|
|
48
48
|
const id = validField(req, 'id', { match: /^[1-9]\d*$/ });
|
|
49
|
-
const orderBy = validField(req, 'orderBy', { includedIn: ['name', 'email', 'status'] });
|
|
50
|
-
const statuses = validField(req, 'statuses[]', { includedIn: ['invited', 'active'] });
|
|
51
|
-
const firstName = validField(req, 'firstName', { minLength: 5 });
|
|
49
|
+
const orderBy = validField(req.query, 'orderBy', { includedIn: ['name', 'email', 'status'] });
|
|
50
|
+
const statuses = validField(req.query, 'statuses[]', { includedIn: ['invited', 'active'] });
|
|
51
|
+
const firstName = validField(req.body, 'firstName', { minLength: 5 });
|
|
52
52
|
```
|
|
53
53
|
|
|
54
54
|
#### Parameters
|
|
55
|
-
- `
|
|
56
|
-
- `fieldName`: The field name to retrieve from
|
|
55
|
+
- `reqOrFieldValues`: A request-like object with `params`, `query`, and `body`, or a direct field container such as `req.params`, `req.query`, or `req.body`.
|
|
56
|
+
- `fieldName`: The field name to retrieve from the request object or direct field container.
|
|
57
57
|
- `options`: An optional object containing validation requirements.
|
|
58
58
|
Possible values for the `options` argument include:
|
|
59
59
|
- `optional`: boolean - Allows the field to be omitted even when other validators such as `minLength` are configured.
|
package/dist/cjs/index.js
CHANGED
|
@@ -79,10 +79,17 @@ var validateInclusion = function validateInclusion(fieldValue, fieldName, includ
|
|
|
79
79
|
return validateNumericality(fieldValue, fieldName, numericality);
|
|
80
80
|
}
|
|
81
81
|
};
|
|
82
|
-
var
|
|
83
|
-
|
|
82
|
+
var fieldValueFrom = function fieldValueFrom(reqOrFieldValues, fieldName) {
|
|
83
|
+
if ('query' in reqOrFieldValues || 'body' in reqOrFieldValues || 'params' in reqOrFieldValues) {
|
|
84
|
+
var _req$params, _req$query, _req$body;
|
|
85
|
+
var req = reqOrFieldValues;
|
|
86
|
+
return ((_req$params = req.params) === null || _req$params === void 0 ? void 0 : _req$params[fieldName]) || ((_req$query = req.query) === null || _req$query === void 0 ? void 0 : _req$query[fieldName]) || ((_req$body = req.body) === null || _req$body === void 0 ? void 0 : _req$body[fieldName]);
|
|
87
|
+
}
|
|
88
|
+
return reqOrFieldValues[fieldName];
|
|
89
|
+
};
|
|
90
|
+
var validField = exports.validField = function validField(reqOrFieldValues, fieldName, options) {
|
|
84
91
|
if (!options) throw new Error('optionsRequired');
|
|
85
|
-
var fieldValue =
|
|
92
|
+
var fieldValue = fieldValueFrom(reqOrFieldValues, fieldName);
|
|
86
93
|
if (!fieldValue) {
|
|
87
94
|
validateRequired(options, fieldName);
|
|
88
95
|
return undefined;
|
|
@@ -90,10 +97,9 @@ var validField = exports.validField = function validField(req, fieldName, option
|
|
|
90
97
|
validateLength(fieldValue, fieldName, options === null || options === void 0 ? void 0 : options.minLength, options === null || options === void 0 ? void 0 : options.maxLength);
|
|
91
98
|
return validateInclusion(fieldValue, fieldName, options === null || options === void 0 ? void 0 : options.includedIn, options === null || options === void 0 ? void 0 : options.match, options === null || options === void 0 ? void 0 : options.numericality);
|
|
92
99
|
};
|
|
93
|
-
var validEmail = exports.validEmail = function validEmail(
|
|
94
|
-
var _req$body2;
|
|
100
|
+
var validEmail = exports.validEmail = function validEmail(reqOrFieldValues) {
|
|
95
101
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
96
|
-
var email =
|
|
102
|
+
var email = fieldValueFrom(reqOrFieldValues, 'email');
|
|
97
103
|
if (!email) {
|
|
98
104
|
if (options.optional) return undefined;
|
|
99
105
|
throw new Error('emailRequired');
|
package/dist/esm/index.js
CHANGED
|
@@ -73,10 +73,17 @@ var validateInclusion = function validateInclusion(fieldValue, fieldName, includ
|
|
|
73
73
|
return validateNumericality(fieldValue, fieldName, numericality);
|
|
74
74
|
}
|
|
75
75
|
};
|
|
76
|
-
|
|
77
|
-
|
|
76
|
+
var fieldValueFrom = function fieldValueFrom(reqOrFieldValues, fieldName) {
|
|
77
|
+
if ('query' in reqOrFieldValues || 'body' in reqOrFieldValues || 'params' in reqOrFieldValues) {
|
|
78
|
+
var _req$params, _req$query, _req$body;
|
|
79
|
+
var req = reqOrFieldValues;
|
|
80
|
+
return ((_req$params = req.params) === null || _req$params === void 0 ? void 0 : _req$params[fieldName]) || ((_req$query = req.query) === null || _req$query === void 0 ? void 0 : _req$query[fieldName]) || ((_req$body = req.body) === null || _req$body === void 0 ? void 0 : _req$body[fieldName]);
|
|
81
|
+
}
|
|
82
|
+
return reqOrFieldValues[fieldName];
|
|
83
|
+
};
|
|
84
|
+
export var validField = function validField(reqOrFieldValues, fieldName, options) {
|
|
78
85
|
if (!options) throw new Error('optionsRequired');
|
|
79
|
-
var fieldValue =
|
|
86
|
+
var fieldValue = fieldValueFrom(reqOrFieldValues, fieldName);
|
|
80
87
|
if (!fieldValue) {
|
|
81
88
|
validateRequired(options, fieldName);
|
|
82
89
|
return undefined;
|
|
@@ -84,10 +91,9 @@ export var validField = function validField(req, fieldName, options) {
|
|
|
84
91
|
validateLength(fieldValue, fieldName, options === null || options === void 0 ? void 0 : options.minLength, options === null || options === void 0 ? void 0 : options.maxLength);
|
|
85
92
|
return validateInclusion(fieldValue, fieldName, options === null || options === void 0 ? void 0 : options.includedIn, options === null || options === void 0 ? void 0 : options.match, options === null || options === void 0 ? void 0 : options.numericality);
|
|
86
93
|
};
|
|
87
|
-
export var validEmail = function validEmail(
|
|
88
|
-
var _req$body2;
|
|
94
|
+
export var validEmail = function validEmail(reqOrFieldValues) {
|
|
89
95
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
90
|
-
var email =
|
|
96
|
+
var email = fieldValueFrom(reqOrFieldValues, 'email');
|
|
91
97
|
if (!email) {
|
|
92
98
|
if (options.optional) return undefined;
|
|
93
99
|
throw new Error('emailRequired');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "light-paradate",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"description": "Lightweight request params validator",
|
|
5
5
|
"author": "Tamas Kamaras",
|
|
6
6
|
"repository": {
|
|
@@ -31,7 +31,8 @@
|
|
|
31
31
|
"build:cjs": "babel src --out-dir dist/cjs --env-name cjs --extensions '.ts'",
|
|
32
32
|
"build:esm": "babel src --out-dir dist/esm --env-name esm --extensions '.ts'",
|
|
33
33
|
"build": "npm run type-check && npm run build:cjs && npm run build:esm",
|
|
34
|
-
"test": "npm run build && jest --config=tests/jest.config.js"
|
|
34
|
+
"test": "npm run build && jest --config=tests/jest.config.js",
|
|
35
|
+
"lint": "eslint src tests"
|
|
35
36
|
},
|
|
36
37
|
"keywords": [
|
|
37
38
|
"node",
|
|
@@ -57,7 +58,11 @@
|
|
|
57
58
|
"@babel/core": "^7.26.0",
|
|
58
59
|
"@babel/preset-env": "^7.26.0",
|
|
59
60
|
"@babel/preset-typescript": "^7.26.0",
|
|
61
|
+
"@eslint/js": "^9.19.0",
|
|
62
|
+
"@types/express": "^5.0.6",
|
|
60
63
|
"@typescript-eslint/eslint-plugin": "^8.21.0",
|
|
64
|
+
"@typescript-eslint/parser": "^8.21.0",
|
|
65
|
+
"babel-jest": "^29.7.0",
|
|
61
66
|
"eslint": "^9.19.0",
|
|
62
67
|
"jest": "^29.7.0",
|
|
63
68
|
"typescript": "^5.7.3"
|
package/dist/cjs/index.d.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
interface OptionsType {
|
|
2
|
-
includedIn?: string[];
|
|
3
|
-
match?: RegExp;
|
|
4
|
-
minLength?: number;
|
|
5
|
-
maxLength?: number;
|
|
6
|
-
numericality?: boolean | NumericalityOptionsType;
|
|
7
|
-
optional?: boolean;
|
|
8
|
-
required?: boolean;
|
|
9
|
-
}
|
|
10
|
-
interface NumericalityOptionsType {
|
|
11
|
-
onlyInteger?: boolean;
|
|
12
|
-
greaterThan?: number;
|
|
13
|
-
greaterThanOrEqualTo?: number;
|
|
14
|
-
lessThan?: number;
|
|
15
|
-
lessThanOrEqualTo?: number;
|
|
16
|
-
}
|
|
17
|
-
interface RequestType {
|
|
18
|
-
query: {
|
|
19
|
-
[key: string]: any;
|
|
20
|
-
};
|
|
21
|
-
body: {
|
|
22
|
-
[key: string]: any;
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
export declare const validField: (req: RequestType, fieldName: string, options: OptionsType) => string | string[] | number | number[] | undefined;
|
|
26
|
-
export declare const validEmail: (req: RequestType, options?: Pick<OptionsType, "optional">) => string | undefined;
|
|
27
|
-
export {};
|