light-paradate 2.1.0 → 2.1.2
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/dist/cjs/index.d.ts +10 -9
- package/dist/cjs/index.js +99 -98
- package/dist/esm/index.js +94 -92
- package/dist/index.d.ts +28 -0
- package/dist/index.js +107 -0
- package/package.json +10 -16
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { ParsedQs } from 'qs';
|
|
2
|
+
import type { ParamsDictionary } from 'express-serve-static-core';
|
|
1
3
|
interface OptionsType {
|
|
2
4
|
includedIn?: string[];
|
|
3
5
|
match?: RegExp;
|
|
@@ -14,14 +16,13 @@ interface NumericalityOptionsType {
|
|
|
14
16
|
lessThan?: number;
|
|
15
17
|
lessThanOrEqualTo?: number;
|
|
16
18
|
}
|
|
17
|
-
interface
|
|
18
|
-
|
|
19
|
-
[key: string]: any;
|
|
20
|
-
};
|
|
21
|
-
body: {
|
|
22
|
-
[key: string]: any;
|
|
23
|
-
};
|
|
19
|
+
interface FieldValuesType {
|
|
20
|
+
[key: string]: any;
|
|
24
21
|
}
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
type ReqOrFieldType = ParamsDictionary | ParsedQs | FieldValuesType;
|
|
23
|
+
type ValidFieldReturnType = string | string[] | number | number[] | undefined;
|
|
24
|
+
type ValidFieldType = (reqOrFieldValues: ReqOrFieldType, fieldName: string, options: OptionsType) => ValidFieldReturnType;
|
|
25
|
+
export declare const validField: ValidFieldType;
|
|
26
|
+
type ValidEmailFunctionType = (reqOrFieldValues: ReqOrFieldType, options?: Pick<OptionsType, 'optional'>) => string | undefined;
|
|
27
|
+
export declare const validEmail: ValidEmailFunctionType;
|
|
27
28
|
export {};
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,111 +1,112 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
var normalizedFieldName = hasArraySuffix ? fieldName.slice(0, -2) : fieldName;
|
|
11
|
-
var camelizedFieldName = normalizedFieldName.replace(/[_-]+([a-zA-Z0-9])/g, function (_, _char) {
|
|
12
|
-
return _char.toUpperCase();
|
|
13
|
-
});
|
|
14
|
-
return hasArraySuffix ? "".concat(camelizedFieldName, "[]") : camelizedFieldName;
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validEmail = exports.validField = void 0;
|
|
4
|
+
const email_validator_1 = require("email-validator");
|
|
5
|
+
const formatFieldNameForError = (fieldName) => {
|
|
6
|
+
const hasArraySuffix = fieldName.endsWith('[]');
|
|
7
|
+
const normalizedFieldName = hasArraySuffix ? fieldName.slice(0, -2) : fieldName;
|
|
8
|
+
const camelizedFieldName = normalizedFieldName.replace(/[_-]+([a-zA-Z0-9])/g, (_, char) => char.toUpperCase());
|
|
9
|
+
return hasArraySuffix ? `${camelizedFieldName}[]` : camelizedFieldName;
|
|
15
10
|
};
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
const fieldError = (fieldName, errorSuffix) => {
|
|
12
|
+
return `${formatFieldNameForError(fieldName)}${errorSuffix}`;
|
|
18
13
|
};
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
14
|
+
const validateRequired = (options, fieldName) => {
|
|
15
|
+
if (options.optional)
|
|
16
|
+
return;
|
|
17
|
+
if (options.required || options.minLength)
|
|
18
|
+
throw new Error(fieldError(fieldName, 'Required'));
|
|
22
19
|
};
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
20
|
+
const validateLength = (fieldValue, fieldName, minLength, maxLength) => {
|
|
21
|
+
if (!minLength && !maxLength)
|
|
22
|
+
return;
|
|
23
|
+
if (minLength && fieldValue.length < minLength)
|
|
24
|
+
throw new Error(fieldError(fieldName, 'TooShort'));
|
|
25
|
+
if (maxLength && fieldValue.length > maxLength)
|
|
26
|
+
throw new Error(fieldError(fieldName, 'TooLong'));
|
|
27
27
|
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
const validatePattern = (fieldValue, fieldName, pattern) => {
|
|
29
|
+
if (!pattern)
|
|
30
|
+
return;
|
|
31
|
+
if (!pattern.test(fieldValue))
|
|
32
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
31
33
|
};
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
34
|
+
const validateNumericality = (fieldValue, fieldName, numericality) => {
|
|
35
|
+
if (!numericality)
|
|
36
|
+
return fieldValue;
|
|
37
|
+
const numericValue = Number(fieldValue);
|
|
38
|
+
if (Number.isNaN(numericValue) || !Number.isFinite(numericValue)) {
|
|
39
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
40
|
+
}
|
|
41
|
+
if (numericality === true)
|
|
42
|
+
return numericValue;
|
|
43
|
+
if (numericality.onlyInteger && !Number.isInteger(numericValue)) {
|
|
44
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
45
|
+
}
|
|
46
|
+
if (numericality.greaterThan !== undefined && numericValue <= numericality.greaterThan) {
|
|
47
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
48
|
+
}
|
|
49
|
+
if (numericality.greaterThanOrEqualTo !== undefined && numericValue < numericality.greaterThanOrEqualTo) {
|
|
50
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
51
|
+
}
|
|
52
|
+
if (numericality.lessThan !== undefined && numericValue >= numericality.lessThan) {
|
|
53
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
54
|
+
}
|
|
55
|
+
if (numericality.lessThanOrEqualTo !== undefined && numericValue > numericality.lessThanOrEqualTo) {
|
|
56
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
57
|
+
}
|
|
58
|
+
return numericValue;
|
|
55
59
|
};
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
60
|
+
const validateInclusion = (fieldValue, fieldName, includedIn, pattern, numericality) => {
|
|
61
|
+
if (fieldName.endsWith('[]')) {
|
|
62
|
+
// if the array has only 1 element, fieldValue is a string, not an array
|
|
63
|
+
const arrayValue = (fieldValue.constructor === Array ? fieldValue : [fieldValue]);
|
|
64
|
+
if (!arrayValue.every(value => includedIn?.includes(value))) {
|
|
65
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
66
|
+
}
|
|
67
|
+
arrayValue.forEach(value => validatePattern(value, fieldName, pattern));
|
|
68
|
+
if (numericality) {
|
|
69
|
+
return arrayValue.map(value => validateNumericality(value, fieldName, numericality));
|
|
70
|
+
}
|
|
71
|
+
return arrayValue;
|
|
64
72
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
return validateNumericality(
|
|
71
|
-
});
|
|
73
|
+
else {
|
|
74
|
+
if (includedIn && !includedIn.includes(fieldValue)) {
|
|
75
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
76
|
+
}
|
|
77
|
+
validatePattern(fieldValue, fieldName, pattern);
|
|
78
|
+
return validateNumericality(fieldValue, fieldName, numericality);
|
|
72
79
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
if (
|
|
76
|
-
|
|
80
|
+
};
|
|
81
|
+
const fieldValueFrom = (reqOrFieldValues, fieldName) => {
|
|
82
|
+
if ('query' in reqOrFieldValues || 'body' in reqOrFieldValues || 'params' in reqOrFieldValues) {
|
|
83
|
+
const req = reqOrFieldValues;
|
|
84
|
+
return req.params?.[fieldName] || req.query?.[fieldName] || req.body?.[fieldName];
|
|
77
85
|
}
|
|
78
|
-
|
|
79
|
-
return validateNumericality(fieldValue, fieldName, numericality);
|
|
80
|
-
}
|
|
86
|
+
return reqOrFieldValues[fieldName];
|
|
81
87
|
};
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
88
|
+
const validField = (reqOrFieldValues, fieldName, options) => {
|
|
89
|
+
if (!options)
|
|
90
|
+
throw new Error('optionsRequired');
|
|
91
|
+
const fieldValue = fieldValueFrom(reqOrFieldValues, fieldName);
|
|
92
|
+
if (!fieldValue) {
|
|
93
|
+
validateRequired(options, fieldName);
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
validateLength(fieldValue, fieldName, options?.minLength, options?.maxLength);
|
|
97
|
+
return validateInclusion(fieldValue, fieldName, options?.includedIn, options?.match, options?.numericality);
|
|
89
98
|
};
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
+
exports.validField = validField;
|
|
100
|
+
const validEmail = (reqOrFieldValues, options = {}) => {
|
|
101
|
+
const email = fieldValueFrom(reqOrFieldValues, 'email');
|
|
102
|
+
if (!email) {
|
|
103
|
+
if (options.optional)
|
|
104
|
+
return undefined;
|
|
105
|
+
throw new Error('emailRequired');
|
|
106
|
+
}
|
|
107
|
+
if (!(0, email_validator_1.validate)(email)) {
|
|
108
|
+
throw new Error('emailInvalid');
|
|
109
|
+
}
|
|
110
|
+
return email;
|
|
99
111
|
};
|
|
100
|
-
|
|
101
|
-
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
102
|
-
var email = fieldValueFrom(reqOrFieldValues, 'email');
|
|
103
|
-
if (!email) {
|
|
104
|
-
if (options.optional) return undefined;
|
|
105
|
-
throw new Error('emailRequired');
|
|
106
|
-
}
|
|
107
|
-
if (!(0, _emailValidator.validate)(email)) {
|
|
108
|
-
throw new Error('emailInvalid');
|
|
109
|
-
}
|
|
110
|
-
return email;
|
|
111
|
-
};
|
|
112
|
+
exports.validEmail = validEmail;
|
package/dist/esm/index.js
CHANGED
|
@@ -1,105 +1,107 @@
|
|
|
1
1
|
import { validate as emailIsValid } from 'email-validator';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
return
|
|
7
|
-
});
|
|
8
|
-
return hasArraySuffix ? "".concat(camelizedFieldName, "[]") : camelizedFieldName;
|
|
2
|
+
const formatFieldNameForError = (fieldName) => {
|
|
3
|
+
const hasArraySuffix = fieldName.endsWith('[]');
|
|
4
|
+
const normalizedFieldName = hasArraySuffix ? fieldName.slice(0, -2) : fieldName;
|
|
5
|
+
const camelizedFieldName = normalizedFieldName.replace(/[_-]+([a-zA-Z0-9])/g, (_, char) => char.toUpperCase());
|
|
6
|
+
return hasArraySuffix ? `${camelizedFieldName}[]` : camelizedFieldName;
|
|
9
7
|
};
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
const fieldError = (fieldName, errorSuffix) => {
|
|
9
|
+
return `${formatFieldNameForError(fieldName)}${errorSuffix}`;
|
|
12
10
|
};
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
const validateRequired = (options, fieldName) => {
|
|
12
|
+
if (options.optional)
|
|
13
|
+
return;
|
|
14
|
+
if (options.required || options.minLength)
|
|
15
|
+
throw new Error(fieldError(fieldName, 'Required'));
|
|
16
16
|
};
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
const validateLength = (fieldValue, fieldName, minLength, maxLength) => {
|
|
18
|
+
if (!minLength && !maxLength)
|
|
19
|
+
return;
|
|
20
|
+
if (minLength && fieldValue.length < minLength)
|
|
21
|
+
throw new Error(fieldError(fieldName, 'TooShort'));
|
|
22
|
+
if (maxLength && fieldValue.length > maxLength)
|
|
23
|
+
throw new Error(fieldError(fieldName, 'TooLong'));
|
|
21
24
|
};
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
const validatePattern = (fieldValue, fieldName, pattern) => {
|
|
26
|
+
if (!pattern)
|
|
27
|
+
return;
|
|
28
|
+
if (!pattern.test(fieldValue))
|
|
29
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
25
30
|
};
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
31
|
+
const validateNumericality = (fieldValue, fieldName, numericality) => {
|
|
32
|
+
if (!numericality)
|
|
33
|
+
return fieldValue;
|
|
34
|
+
const numericValue = Number(fieldValue);
|
|
35
|
+
if (Number.isNaN(numericValue) || !Number.isFinite(numericValue)) {
|
|
36
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
37
|
+
}
|
|
38
|
+
if (numericality === true)
|
|
39
|
+
return numericValue;
|
|
40
|
+
if (numericality.onlyInteger && !Number.isInteger(numericValue)) {
|
|
41
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
42
|
+
}
|
|
43
|
+
if (numericality.greaterThan !== undefined && numericValue <= numericality.greaterThan) {
|
|
44
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
45
|
+
}
|
|
46
|
+
if (numericality.greaterThanOrEqualTo !== undefined && numericValue < numericality.greaterThanOrEqualTo) {
|
|
47
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
48
|
+
}
|
|
49
|
+
if (numericality.lessThan !== undefined && numericValue >= numericality.lessThan) {
|
|
50
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
51
|
+
}
|
|
52
|
+
if (numericality.lessThanOrEqualTo !== undefined && numericValue > numericality.lessThanOrEqualTo) {
|
|
53
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
54
|
+
}
|
|
55
|
+
return numericValue;
|
|
49
56
|
};
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
57
|
+
const validateInclusion = (fieldValue, fieldName, includedIn, pattern, numericality) => {
|
|
58
|
+
if (fieldName.endsWith('[]')) {
|
|
59
|
+
// if the array has only 1 element, fieldValue is a string, not an array
|
|
60
|
+
const arrayValue = (fieldValue.constructor === Array ? fieldValue : [fieldValue]);
|
|
61
|
+
if (!arrayValue.every(value => includedIn?.includes(value))) {
|
|
62
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
63
|
+
}
|
|
64
|
+
arrayValue.forEach(value => validatePattern(value, fieldName, pattern));
|
|
65
|
+
if (numericality) {
|
|
66
|
+
return arrayValue.map(value => validateNumericality(value, fieldName, numericality));
|
|
67
|
+
}
|
|
68
|
+
return arrayValue;
|
|
58
69
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
return validateNumericality(
|
|
65
|
-
});
|
|
70
|
+
else {
|
|
71
|
+
if (includedIn && !includedIn.includes(fieldValue)) {
|
|
72
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
73
|
+
}
|
|
74
|
+
validatePattern(fieldValue, fieldName, pattern);
|
|
75
|
+
return validateNumericality(fieldValue, fieldName, numericality);
|
|
66
76
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
if (
|
|
70
|
-
|
|
77
|
+
};
|
|
78
|
+
const fieldValueFrom = (reqOrFieldValues, fieldName) => {
|
|
79
|
+
if ('query' in reqOrFieldValues || 'body' in reqOrFieldValues || 'params' in reqOrFieldValues) {
|
|
80
|
+
const req = reqOrFieldValues;
|
|
81
|
+
return req.params?.[fieldName] || req.query?.[fieldName] || req.body?.[fieldName];
|
|
71
82
|
}
|
|
72
|
-
|
|
73
|
-
return validateNumericality(fieldValue, fieldName, numericality);
|
|
74
|
-
}
|
|
83
|
+
return reqOrFieldValues[fieldName];
|
|
75
84
|
};
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
85
|
+
export const validField = (reqOrFieldValues, fieldName, options) => {
|
|
86
|
+
if (!options)
|
|
87
|
+
throw new Error('optionsRequired');
|
|
88
|
+
const fieldValue = fieldValueFrom(reqOrFieldValues, fieldName);
|
|
89
|
+
if (!fieldValue) {
|
|
90
|
+
validateRequired(options, fieldName);
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
validateLength(fieldValue, fieldName, options?.minLength, options?.maxLength);
|
|
94
|
+
return validateInclusion(fieldValue, fieldName, options?.includedIn, options?.match, options?.numericality);
|
|
83
95
|
};
|
|
84
|
-
export
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
96
|
+
export const validEmail = (reqOrFieldValues, options = {}) => {
|
|
97
|
+
const email = fieldValueFrom(reqOrFieldValues, 'email');
|
|
98
|
+
if (!email) {
|
|
99
|
+
if (options.optional)
|
|
100
|
+
return undefined;
|
|
101
|
+
throw new Error('emailRequired');
|
|
102
|
+
}
|
|
103
|
+
if (!emailIsValid(email)) {
|
|
104
|
+
throw new Error('emailInvalid');
|
|
105
|
+
}
|
|
106
|
+
return email;
|
|
93
107
|
};
|
|
94
|
-
export var validEmail = function validEmail(reqOrFieldValues) {
|
|
95
|
-
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
96
|
-
var email = fieldValueFrom(reqOrFieldValues, 'email');
|
|
97
|
-
if (!email) {
|
|
98
|
-
if (options.optional) return undefined;
|
|
99
|
-
throw new Error('emailRequired');
|
|
100
|
-
}
|
|
101
|
-
if (!emailIsValid(email)) {
|
|
102
|
-
throw new Error('emailInvalid');
|
|
103
|
-
}
|
|
104
|
-
return email;
|
|
105
|
-
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ParsedQs } from 'qs';
|
|
2
|
+
import type { ParamsDictionary } from 'express-serve-static-core';
|
|
3
|
+
interface OptionsType {
|
|
4
|
+
includedIn?: string[];
|
|
5
|
+
match?: RegExp;
|
|
6
|
+
minLength?: number;
|
|
7
|
+
maxLength?: number;
|
|
8
|
+
numericality?: boolean | NumericalityOptionsType;
|
|
9
|
+
optional?: boolean;
|
|
10
|
+
required?: boolean;
|
|
11
|
+
}
|
|
12
|
+
interface NumericalityOptionsType {
|
|
13
|
+
onlyInteger?: boolean;
|
|
14
|
+
greaterThan?: number;
|
|
15
|
+
greaterThanOrEqualTo?: number;
|
|
16
|
+
lessThan?: number;
|
|
17
|
+
lessThanOrEqualTo?: number;
|
|
18
|
+
}
|
|
19
|
+
interface FieldValuesType {
|
|
20
|
+
[key: string]: any;
|
|
21
|
+
}
|
|
22
|
+
type ReqOrFieldType = ParamsDictionary | ParsedQs | FieldValuesType;
|
|
23
|
+
type ValidFieldReturnType = string | string[] | number | number[] | undefined;
|
|
24
|
+
type ValidFieldType = (reqOrFieldValues: ReqOrFieldType, fieldName: string, options: OptionsType) => ValidFieldReturnType;
|
|
25
|
+
export declare const validField: ValidFieldType;
|
|
26
|
+
type ValidEmailFunctionType = (reqOrFieldValues: ReqOrFieldType, options?: Pick<OptionsType, 'optional'>) => string | undefined;
|
|
27
|
+
export declare const validEmail: ValidEmailFunctionType;
|
|
28
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { validate as emailIsValid } from 'email-validator';
|
|
2
|
+
const formatFieldNameForError = (fieldName) => {
|
|
3
|
+
const hasArraySuffix = fieldName.endsWith('[]');
|
|
4
|
+
const normalizedFieldName = hasArraySuffix ? fieldName.slice(0, -2) : fieldName;
|
|
5
|
+
const camelizedFieldName = normalizedFieldName.replace(/[_-]+([a-zA-Z0-9])/g, (_, char) => char.toUpperCase());
|
|
6
|
+
return hasArraySuffix ? `${camelizedFieldName}[]` : camelizedFieldName;
|
|
7
|
+
};
|
|
8
|
+
const fieldError = (fieldName, errorSuffix) => {
|
|
9
|
+
return `${formatFieldNameForError(fieldName)}${errorSuffix}`;
|
|
10
|
+
};
|
|
11
|
+
const validateRequired = (options, fieldName) => {
|
|
12
|
+
if (options.optional)
|
|
13
|
+
return;
|
|
14
|
+
if (options.required || options.minLength)
|
|
15
|
+
throw new Error(fieldError(fieldName, 'Required'));
|
|
16
|
+
};
|
|
17
|
+
const validateLength = (fieldValue, fieldName, minLength, maxLength) => {
|
|
18
|
+
if (!minLength && !maxLength)
|
|
19
|
+
return;
|
|
20
|
+
if (minLength && fieldValue.length < minLength)
|
|
21
|
+
throw new Error(fieldError(fieldName, 'TooShort'));
|
|
22
|
+
if (maxLength && fieldValue.length > maxLength)
|
|
23
|
+
throw new Error(fieldError(fieldName, 'TooLong'));
|
|
24
|
+
};
|
|
25
|
+
const validatePattern = (fieldValue, fieldName, pattern) => {
|
|
26
|
+
if (!pattern)
|
|
27
|
+
return;
|
|
28
|
+
if (!pattern.test(fieldValue))
|
|
29
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
30
|
+
};
|
|
31
|
+
const validateNumericality = (fieldValue, fieldName, numericality) => {
|
|
32
|
+
if (!numericality)
|
|
33
|
+
return fieldValue;
|
|
34
|
+
const numericValue = Number(fieldValue);
|
|
35
|
+
if (Number.isNaN(numericValue) || !Number.isFinite(numericValue)) {
|
|
36
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
37
|
+
}
|
|
38
|
+
if (numericality === true)
|
|
39
|
+
return numericValue;
|
|
40
|
+
if (numericality.onlyInteger && !Number.isInteger(numericValue)) {
|
|
41
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
42
|
+
}
|
|
43
|
+
if (numericality.greaterThan !== undefined && numericValue <= numericality.greaterThan) {
|
|
44
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
45
|
+
}
|
|
46
|
+
if (numericality.greaterThanOrEqualTo !== undefined && numericValue < numericality.greaterThanOrEqualTo) {
|
|
47
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
48
|
+
}
|
|
49
|
+
if (numericality.lessThan !== undefined && numericValue >= numericality.lessThan) {
|
|
50
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
51
|
+
}
|
|
52
|
+
if (numericality.lessThanOrEqualTo !== undefined && numericValue > numericality.lessThanOrEqualTo) {
|
|
53
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
54
|
+
}
|
|
55
|
+
return numericValue;
|
|
56
|
+
};
|
|
57
|
+
const validateInclusion = (fieldValue, fieldName, includedIn, pattern, numericality) => {
|
|
58
|
+
if (fieldName.endsWith('[]')) {
|
|
59
|
+
// if the array has only 1 element, fieldValue is a string, not an array
|
|
60
|
+
const arrayValue = (fieldValue.constructor === Array ? fieldValue : [fieldValue]);
|
|
61
|
+
if (!arrayValue.every(value => includedIn?.includes(value))) {
|
|
62
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
63
|
+
}
|
|
64
|
+
arrayValue.forEach(value => validatePattern(value, fieldName, pattern));
|
|
65
|
+
if (numericality) {
|
|
66
|
+
return arrayValue.map(value => validateNumericality(value, fieldName, numericality));
|
|
67
|
+
}
|
|
68
|
+
return arrayValue;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
if (includedIn && !includedIn.includes(fieldValue)) {
|
|
72
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
73
|
+
}
|
|
74
|
+
validatePattern(fieldValue, fieldName, pattern);
|
|
75
|
+
return validateNumericality(fieldValue, fieldName, numericality);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
const fieldValueFrom = (reqOrFieldValues, fieldName) => {
|
|
79
|
+
if ('query' in reqOrFieldValues || 'body' in reqOrFieldValues || 'params' in reqOrFieldValues) {
|
|
80
|
+
const req = reqOrFieldValues;
|
|
81
|
+
return req.params?.[fieldName] || req.query?.[fieldName] || req.body?.[fieldName];
|
|
82
|
+
}
|
|
83
|
+
return reqOrFieldValues[fieldName];
|
|
84
|
+
};
|
|
85
|
+
export const validField = (reqOrFieldValues, fieldName, options) => {
|
|
86
|
+
if (!options)
|
|
87
|
+
throw new Error('optionsRequired');
|
|
88
|
+
const fieldValue = fieldValueFrom(reqOrFieldValues, fieldName);
|
|
89
|
+
if (!fieldValue) {
|
|
90
|
+
validateRequired(options, fieldName);
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
validateLength(fieldValue, fieldName, options?.minLength, options?.maxLength);
|
|
94
|
+
return validateInclusion(fieldValue, fieldName, options?.includedIn, options?.match, options?.numericality);
|
|
95
|
+
};
|
|
96
|
+
export const validEmail = (reqOrFieldValues, options = {}) => {
|
|
97
|
+
const email = fieldValueFrom(reqOrFieldValues, 'email');
|
|
98
|
+
if (!email) {
|
|
99
|
+
if (options.optional)
|
|
100
|
+
return undefined;
|
|
101
|
+
throw new Error('emailRequired');
|
|
102
|
+
}
|
|
103
|
+
if (!emailIsValid(email)) {
|
|
104
|
+
throw new Error('emailInvalid');
|
|
105
|
+
}
|
|
106
|
+
return email;
|
|
107
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "light-paradate",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.2",
|
|
4
4
|
"description": "Lightweight request params validator",
|
|
5
5
|
"author": "Tamas Kamaras",
|
|
6
6
|
"repository": {
|
|
@@ -20,18 +20,16 @@
|
|
|
20
20
|
}
|
|
21
21
|
},
|
|
22
22
|
"files": [
|
|
23
|
-
"dist
|
|
24
|
-
"dist/cjs/index.d.ts",
|
|
25
|
-
"dist/esm/index.js",
|
|
23
|
+
"dist",
|
|
26
24
|
"LICENSE",
|
|
27
25
|
"README.md"
|
|
28
26
|
],
|
|
29
27
|
"scripts": {
|
|
30
28
|
"type-check": "tsc",
|
|
31
|
-
"build:cjs": "
|
|
32
|
-
"build:esm": "
|
|
29
|
+
"build:cjs": "tsc --project tsconfig.json --module commonjs --outDir dist/cjs --declarationDir dist/cjs --emitDeclarationOnly false",
|
|
30
|
+
"build:esm": "tsc --project tsconfig.json --module esnext --outDir dist/esm --declaration false --emitDeclarationOnly false",
|
|
33
31
|
"build": "npm run type-check && npm run build:cjs && npm run build:esm",
|
|
34
|
-
"test": "
|
|
32
|
+
"test": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js",
|
|
35
33
|
"lint": "eslint src tests"
|
|
36
34
|
},
|
|
37
35
|
"keywords": [
|
|
@@ -54,16 +52,12 @@
|
|
|
54
52
|
"email-validator": "^2.0.4"
|
|
55
53
|
},
|
|
56
54
|
"devDependencies": {
|
|
57
|
-
"@
|
|
58
|
-
"@
|
|
59
|
-
"@babel/preset-env": "^7.26.0",
|
|
60
|
-
"@babel/preset-typescript": "^7.26.0",
|
|
61
|
-
"@eslint/js": "^9.19.0",
|
|
55
|
+
"@eslint/js": "^10.0.1",
|
|
56
|
+
"@types/express": "^5.0.6",
|
|
62
57
|
"@typescript-eslint/eslint-plugin": "^8.21.0",
|
|
63
58
|
"@typescript-eslint/parser": "^8.21.0",
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
"typescript": "^5.7.3"
|
|
59
|
+
"eslint": "^10.2.1",
|
|
60
|
+
"jest": "^30.3.0",
|
|
61
|
+
"typescript": "^6.0.3"
|
|
68
62
|
}
|
|
69
63
|
}
|