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.
@@ -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 RequestType {
18
- query: {
19
- [key: string]: any;
20
- };
21
- body: {
22
- [key: string]: any;
23
- };
19
+ interface FieldValuesType {
20
+ [key: string]: any;
24
21
  }
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;
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
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.validField = exports.validEmail = void 0;
7
- var _emailValidator = require("email-validator");
8
- var formatFieldNameForError = function formatFieldNameForError(fieldName) {
9
- var hasArraySuffix = fieldName.endsWith('[]');
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
- var fieldError = function fieldError(fieldName, errorSuffix) {
17
- return "".concat(formatFieldNameForError(fieldName)).concat(errorSuffix);
11
+ const fieldError = (fieldName, errorSuffix) => {
12
+ return `${formatFieldNameForError(fieldName)}${errorSuffix}`;
18
13
  };
19
- var validateRequired = function validateRequired(options, fieldName) {
20
- if (options.optional) return;
21
- if (options.required || options.minLength) throw new Error(fieldError(fieldName, 'Required'));
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
- var validateLength = function validateLength(fieldValue, fieldName, minLength, maxLength) {
24
- if (!minLength && !maxLength) return;
25
- if (minLength && fieldValue.length < minLength) throw new Error(fieldError(fieldName, 'TooShort'));
26
- if (maxLength && fieldValue.length > maxLength) throw new Error(fieldError(fieldName, 'TooLong'));
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
- var validatePattern = function validatePattern(fieldValue, fieldName, pattern) {
29
- if (!pattern) return;
30
- if (!pattern.test(fieldValue)) throw new Error(fieldError(fieldName, 'IsInvalid'));
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
- var validateNumericality = function validateNumericality(fieldValue, fieldName, numericality) {
33
- if (!numericality) return fieldValue;
34
- var numericValue = Number(fieldValue);
35
- if (Number.isNaN(numericValue) || !Number.isFinite(numericValue)) {
36
- throw new Error(fieldError(fieldName, 'IsInvalid'));
37
- }
38
- if (numericality === true) return numericValue;
39
- if (numericality.onlyInteger && !Number.isInteger(numericValue)) {
40
- throw new Error(fieldError(fieldName, 'IsInvalid'));
41
- }
42
- if (numericality.greaterThan !== undefined && numericValue <= numericality.greaterThan) {
43
- throw new Error(fieldError(fieldName, 'IsInvalid'));
44
- }
45
- if (numericality.greaterThanOrEqualTo !== undefined && numericValue < numericality.greaterThanOrEqualTo) {
46
- throw new Error(fieldError(fieldName, 'IsInvalid'));
47
- }
48
- if (numericality.lessThan !== undefined && numericValue >= numericality.lessThan) {
49
- throw new Error(fieldError(fieldName, 'IsInvalid'));
50
- }
51
- if (numericality.lessThanOrEqualTo !== undefined && numericValue > numericality.lessThanOrEqualTo) {
52
- throw new Error(fieldError(fieldName, 'IsInvalid'));
53
- }
54
- return numericValue;
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
- var validateInclusion = function validateInclusion(fieldValue, fieldName, includedIn, pattern, numericality) {
57
- if (fieldName.endsWith('[]')) {
58
- // if the array has only 1 element, fieldValue is a string, not an array
59
- var arrayValue = fieldValue.constructor === Array ? fieldValue : [fieldValue];
60
- if (!arrayValue.every(function (value) {
61
- return includedIn === null || includedIn === void 0 ? void 0 : includedIn.includes(value);
62
- })) {
63
- throw new Error(fieldError(fieldName, 'IsInvalid'));
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
- arrayValue.forEach(function (value) {
66
- return validatePattern(value, fieldName, pattern);
67
- });
68
- if (numericality) {
69
- return arrayValue.map(function (value) {
70
- return validateNumericality(value, fieldName, numericality);
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
- return arrayValue;
74
- } else {
75
- if (includedIn && !includedIn.includes(fieldValue)) {
76
- throw new Error(fieldError(fieldName, 'IsInvalid'));
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
- validatePattern(fieldValue, fieldName, pattern);
79
- return validateNumericality(fieldValue, fieldName, numericality);
80
- }
86
+ return reqOrFieldValues[fieldName];
81
87
  };
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];
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
- var validField = exports.validField = function validField(reqOrFieldValues, fieldName, options) {
91
- if (!options) throw new Error('optionsRequired');
92
- var fieldValue = fieldValueFrom(reqOrFieldValues, fieldName);
93
- if (!fieldValue) {
94
- validateRequired(options, fieldName);
95
- return undefined;
96
- }
97
- validateLength(fieldValue, fieldName, options === null || options === void 0 ? void 0 : options.minLength, options === null || options === void 0 ? void 0 : options.maxLength);
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);
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
- var validEmail = exports.validEmail = function validEmail(reqOrFieldValues) {
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
- var formatFieldNameForError = function formatFieldNameForError(fieldName) {
3
- var hasArraySuffix = fieldName.endsWith('[]');
4
- var normalizedFieldName = hasArraySuffix ? fieldName.slice(0, -2) : fieldName;
5
- var camelizedFieldName = normalizedFieldName.replace(/[_-]+([a-zA-Z0-9])/g, function (_, _char) {
6
- return _char.toUpperCase();
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
- var fieldError = function fieldError(fieldName, errorSuffix) {
11
- return "".concat(formatFieldNameForError(fieldName)).concat(errorSuffix);
8
+ const fieldError = (fieldName, errorSuffix) => {
9
+ return `${formatFieldNameForError(fieldName)}${errorSuffix}`;
12
10
  };
13
- var validateRequired = function validateRequired(options, fieldName) {
14
- if (options.optional) return;
15
- if (options.required || options.minLength) throw new Error(fieldError(fieldName, 'Required'));
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
- var validateLength = function validateLength(fieldValue, fieldName, minLength, maxLength) {
18
- if (!minLength && !maxLength) return;
19
- if (minLength && fieldValue.length < minLength) throw new Error(fieldError(fieldName, 'TooShort'));
20
- if (maxLength && fieldValue.length > maxLength) throw new Error(fieldError(fieldName, 'TooLong'));
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
- var validatePattern = function validatePattern(fieldValue, fieldName, pattern) {
23
- if (!pattern) return;
24
- if (!pattern.test(fieldValue)) throw new Error(fieldError(fieldName, 'IsInvalid'));
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
- var validateNumericality = function validateNumericality(fieldValue, fieldName, numericality) {
27
- if (!numericality) return fieldValue;
28
- var numericValue = Number(fieldValue);
29
- if (Number.isNaN(numericValue) || !Number.isFinite(numericValue)) {
30
- throw new Error(fieldError(fieldName, 'IsInvalid'));
31
- }
32
- if (numericality === true) return numericValue;
33
- if (numericality.onlyInteger && !Number.isInteger(numericValue)) {
34
- throw new Error(fieldError(fieldName, 'IsInvalid'));
35
- }
36
- if (numericality.greaterThan !== undefined && numericValue <= numericality.greaterThan) {
37
- throw new Error(fieldError(fieldName, 'IsInvalid'));
38
- }
39
- if (numericality.greaterThanOrEqualTo !== undefined && numericValue < numericality.greaterThanOrEqualTo) {
40
- throw new Error(fieldError(fieldName, 'IsInvalid'));
41
- }
42
- if (numericality.lessThan !== undefined && numericValue >= numericality.lessThan) {
43
- throw new Error(fieldError(fieldName, 'IsInvalid'));
44
- }
45
- if (numericality.lessThanOrEqualTo !== undefined && numericValue > numericality.lessThanOrEqualTo) {
46
- throw new Error(fieldError(fieldName, 'IsInvalid'));
47
- }
48
- return numericValue;
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
- var validateInclusion = function validateInclusion(fieldValue, fieldName, includedIn, pattern, numericality) {
51
- if (fieldName.endsWith('[]')) {
52
- // if the array has only 1 element, fieldValue is a string, not an array
53
- var arrayValue = fieldValue.constructor === Array ? fieldValue : [fieldValue];
54
- if (!arrayValue.every(function (value) {
55
- return includedIn === null || includedIn === void 0 ? void 0 : includedIn.includes(value);
56
- })) {
57
- throw new Error(fieldError(fieldName, 'IsInvalid'));
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
- arrayValue.forEach(function (value) {
60
- return validatePattern(value, fieldName, pattern);
61
- });
62
- if (numericality) {
63
- return arrayValue.map(function (value) {
64
- return validateNumericality(value, fieldName, numericality);
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
- return arrayValue;
68
- } else {
69
- if (includedIn && !includedIn.includes(fieldValue)) {
70
- throw new Error(fieldError(fieldName, 'IsInvalid'));
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
- validatePattern(fieldValue, fieldName, pattern);
73
- return validateNumericality(fieldValue, fieldName, numericality);
74
- }
83
+ return reqOrFieldValues[fieldName];
75
84
  };
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];
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 var validField = function validField(reqOrFieldValues, fieldName, options) {
85
- if (!options) throw new Error('optionsRequired');
86
- var fieldValue = fieldValueFrom(reqOrFieldValues, fieldName);
87
- if (!fieldValue) {
88
- validateRequired(options, fieldName);
89
- return undefined;
90
- }
91
- validateLength(fieldValue, fieldName, options === null || options === void 0 ? void 0 : options.minLength, options === null || options === void 0 ? void 0 : options.maxLength);
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);
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
- };
@@ -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.0",
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/cjs/index.js",
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": "babel src --out-dir dist/cjs --env-name cjs --extensions '.ts'",
32
- "build:esm": "babel src --out-dir dist/esm --env-name esm --extensions '.ts'",
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": "npm run build && jest --config=tests/jest.config.js",
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
- "@babel/cli": "^7.26.4",
58
- "@babel/core": "^7.26.0",
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
- "babel-jest": "^29.7.0",
65
- "eslint": "^9.19.0",
66
- "jest": "^29.7.0",
67
- "typescript": "^5.7.3"
59
+ "eslint": "^10.2.1",
60
+ "jest": "^30.3.0",
61
+ "typescript": "^6.0.3"
68
62
  }
69
63
  }