light-paradate 1.1.2 → 2.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 +24 -13
- package/dist/cjs/index.d.ts +12 -2
- package/dist/cjs/index.js +71 -14
- package/dist/esm/index.js +71 -14
- package/package.json +6 -2
package/README.md
CHANGED
|
@@ -37,59 +37,70 @@ yarn add light-paradate
|
|
|
37
37
|
## Usage
|
|
38
38
|
### validField
|
|
39
39
|
|
|
40
|
-
`validField(
|
|
40
|
+
`validField(reqOrFieldValues, fieldName, options)`
|
|
41
41
|
|
|
42
|
-
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.
|
|
43
43
|
|
|
44
44
|
example:
|
|
45
45
|
```js
|
|
46
46
|
const uuid = validField(req, 'uuid', { minLength: 36 });
|
|
47
|
-
const
|
|
48
|
-
const
|
|
49
|
-
const
|
|
47
|
+
const parentId = validField(req.params, 'parent_id', { numericality: { onlyInteger: true, greaterThan: 0 } });
|
|
48
|
+
const id = validField(req, 'id', { match: /^[1-9]\d*$/ });
|
|
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 });
|
|
50
52
|
```
|
|
51
53
|
|
|
52
54
|
#### Parameters
|
|
53
|
-
- `
|
|
54
|
-
- `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.
|
|
55
57
|
- `options`: An optional object containing validation requirements.
|
|
56
58
|
Possible values for the `options` argument include:
|
|
59
|
+
- `optional`: boolean - Allows the field to be omitted even when other validators such as `minLength` are configured.
|
|
57
60
|
- `required`: boolean - Indicates whether the field must have a non-empty value.
|
|
58
61
|
- `minLength`: number - Specifies the minimum length the field value should have.
|
|
59
62
|
- `maxLength`: number - Specifies the maximum length the field value can have.
|
|
63
|
+
- `match`: RegExp - Validates the field value against a regular expression.
|
|
64
|
+
- `numericality`: boolean | object - Converts the field to a number and validates numeric constraints. Supported object keys: `onlyInteger`, `greaterThan`, `greaterThanOrEqualTo`, `lessThan`, `lessThanOrEqualTo`.
|
|
60
65
|
- `includedIn`: string[] - An array of valid strings that the field value must match to pass validation.
|
|
61
66
|
|
|
62
67
|
#### Return Value
|
|
63
|
-
- If valid, returns the string
|
|
64
|
-
- Returns `undefined` if the field is not required and not present.
|
|
68
|
+
- If valid, returns the string, number, string array, or number array (in case of fields that use `[]` notation).
|
|
69
|
+
- Returns `undefined` if the field is not required and not present, or if `optional: true` is set and the field is omitted.
|
|
65
70
|
- Throws an error if validation fails.
|
|
66
71
|
|
|
67
72
|
#### Behavior
|
|
68
|
-
- Check presence: If `options.required` is set or if `options.minLength` is defined, the field must be present
|
|
73
|
+
- Check presence: If `options.required` is set or if `options.minLength` is defined, the field must be present unless `optional: true` is set.
|
|
69
74
|
- Check length: If `options.minLength` is set, throws an error if the field’s value is shorter than the specified length.
|
|
75
|
+
- Check pattern: If `options.match` is defined, throws an error if the field’s value does not match the provided regular expression.
|
|
76
|
+
- Check numericality: If `options.numericality` is defined, converts the field to a number and throws an error if it is not numeric or does not satisfy the configured numeric constraints.
|
|
70
77
|
- Check inclusion: If `options.includedIn` is defined, throws an error if the field’s value (or values) are not in the specified list.
|
|
71
78
|
|
|
72
79
|
Array fields: If your field name ends with `[]` (e.g., `statuses[]`), the utility will treat the field as an Array. If there is only one value, it will be returned in a single-element array.
|
|
73
80
|
|
|
74
81
|
### validEmail
|
|
75
82
|
|
|
76
|
-
`validEmail(req)`
|
|
83
|
+
`validEmail(req, options)`
|
|
77
84
|
|
|
78
85
|
Extracts and validates the "email" field from `req.query` or `req.body`, ensuring it is both present and syntactically valid.
|
|
79
86
|
|
|
80
87
|
example:
|
|
81
88
|
```js
|
|
82
89
|
const email = validEmail(req);
|
|
90
|
+
const alternateEmail = validEmail(req, { optional: true });
|
|
83
91
|
```
|
|
84
92
|
#### Parameters
|
|
85
93
|
- `req`: A request-like object with query and body.
|
|
94
|
+
- `options`: Optional configuration.
|
|
95
|
+
- `optional`: boolean - Allows `email` to be omitted. If present, it is still validated.
|
|
86
96
|
|
|
87
97
|
#### Return Value
|
|
88
98
|
- Returns a valid email string if validation passes.
|
|
89
|
-
-
|
|
99
|
+
- Returns `undefined` if `optional: true` is set and the email is missing.
|
|
100
|
+
- Throws an error if the email is invalid, or if it is missing without `optional: true`.
|
|
90
101
|
|
|
91
102
|
#### Behavior
|
|
92
|
-
- Checks presence: Throws an error `emailRequired` if the field `email` is not found in either `req.query` or `req.body
|
|
103
|
+
- Checks presence: Throws an error `emailRequired` if the field `email` is not found in either `req.query` or `req.body`, unless `optional: true` is set.
|
|
93
104
|
- Checks format: Uses the `email-validator` package under the hood to ensure the format is correct. If invalid, throws an error `emailInvalid`.
|
|
94
105
|
|
|
95
106
|
#### License
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,9 +1,19 @@
|
|
|
1
1
|
interface OptionsType {
|
|
2
2
|
includedIn?: string[];
|
|
3
|
+
match?: RegExp;
|
|
3
4
|
minLength?: number;
|
|
4
5
|
maxLength?: number;
|
|
6
|
+
numericality?: boolean | NumericalityOptionsType;
|
|
7
|
+
optional?: boolean;
|
|
5
8
|
required?: boolean;
|
|
6
9
|
}
|
|
10
|
+
interface NumericalityOptionsType {
|
|
11
|
+
onlyInteger?: boolean;
|
|
12
|
+
greaterThan?: number;
|
|
13
|
+
greaterThanOrEqualTo?: number;
|
|
14
|
+
lessThan?: number;
|
|
15
|
+
lessThanOrEqualTo?: number;
|
|
16
|
+
}
|
|
7
17
|
interface RequestType {
|
|
8
18
|
query: {
|
|
9
19
|
[key: string]: any;
|
|
@@ -12,6 +22,6 @@ interface RequestType {
|
|
|
12
22
|
[key: string]: any;
|
|
13
23
|
};
|
|
14
24
|
}
|
|
15
|
-
export declare const validField: (req: RequestType, fieldName: string, options: OptionsType) => string | string[] | undefined;
|
|
16
|
-
export declare const validEmail: (req: RequestType) => string | undefined;
|
|
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;
|
|
17
27
|
export {};
|
package/dist/cjs/index.js
CHANGED
|
@@ -5,46 +5,103 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.validField = exports.validEmail = void 0;
|
|
7
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;
|
|
15
|
+
};
|
|
16
|
+
var fieldError = function fieldError(fieldName, errorSuffix) {
|
|
17
|
+
return "".concat(formatFieldNameForError(fieldName)).concat(errorSuffix);
|
|
18
|
+
};
|
|
8
19
|
var validateRequired = function validateRequired(options, fieldName) {
|
|
9
|
-
if (options.
|
|
20
|
+
if (options.optional) return;
|
|
21
|
+
if (options.required || options.minLength) throw new Error(fieldError(fieldName, 'Required'));
|
|
10
22
|
};
|
|
11
23
|
var validateLength = function validateLength(fieldValue, fieldName, minLength, maxLength) {
|
|
12
24
|
if (!minLength && !maxLength) return;
|
|
13
|
-
if (minLength && fieldValue.length < minLength) throw new Error(
|
|
14
|
-
if (maxLength && fieldValue.length > maxLength) throw new Error(
|
|
25
|
+
if (minLength && fieldValue.length < minLength) throw new Error(fieldError(fieldName, 'TooShort'));
|
|
26
|
+
if (maxLength && fieldValue.length > maxLength) throw new Error(fieldError(fieldName, 'TooLong'));
|
|
15
27
|
};
|
|
16
|
-
var
|
|
28
|
+
var validatePattern = function validatePattern(fieldValue, fieldName, pattern) {
|
|
29
|
+
if (!pattern) return;
|
|
30
|
+
if (!pattern.test(fieldValue)) throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
31
|
+
};
|
|
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;
|
|
55
|
+
};
|
|
56
|
+
var validateInclusion = function validateInclusion(fieldValue, fieldName, includedIn, pattern, numericality) {
|
|
17
57
|
if (fieldName.endsWith('[]')) {
|
|
18
58
|
// if the array has only 1 element, fieldValue is a string, not an array
|
|
19
59
|
var arrayValue = fieldValue.constructor === Array ? fieldValue : [fieldValue];
|
|
20
60
|
if (!arrayValue.every(function (value) {
|
|
21
61
|
return includedIn === null || includedIn === void 0 ? void 0 : includedIn.includes(value);
|
|
22
62
|
})) {
|
|
23
|
-
throw new Error(
|
|
63
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
64
|
+
}
|
|
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
|
+
});
|
|
24
72
|
}
|
|
25
73
|
return arrayValue;
|
|
26
74
|
} else {
|
|
27
75
|
if (includedIn && !includedIn.includes(fieldValue)) {
|
|
28
|
-
throw new Error(
|
|
76
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
29
77
|
}
|
|
30
|
-
|
|
78
|
+
validatePattern(fieldValue, fieldName, pattern);
|
|
79
|
+
return validateNumericality(fieldValue, fieldName, numericality);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
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]);
|
|
31
87
|
}
|
|
88
|
+
return reqOrFieldValues[fieldName];
|
|
32
89
|
};
|
|
33
|
-
var validField = exports.validField = function validField(
|
|
34
|
-
var _req$body;
|
|
90
|
+
var validField = exports.validField = function validField(reqOrFieldValues, fieldName, options) {
|
|
35
91
|
if (!options) throw new Error('optionsRequired');
|
|
36
|
-
var fieldValue =
|
|
92
|
+
var fieldValue = fieldValueFrom(reqOrFieldValues, fieldName);
|
|
37
93
|
if (!fieldValue) {
|
|
38
94
|
validateRequired(options, fieldName);
|
|
39
95
|
return undefined;
|
|
40
96
|
}
|
|
41
97
|
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);
|
|
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);
|
|
43
99
|
};
|
|
44
|
-
var validEmail = exports.validEmail = function validEmail(
|
|
45
|
-
var
|
|
46
|
-
var email =
|
|
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');
|
|
47
103
|
if (!email) {
|
|
104
|
+
if (options.optional) return undefined;
|
|
48
105
|
throw new Error('emailRequired');
|
|
49
106
|
}
|
|
50
107
|
if (!(0, _emailValidator.validate)(email)) {
|
package/dist/esm/index.js
CHANGED
|
@@ -1,44 +1,101 @@
|
|
|
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;
|
|
9
|
+
};
|
|
10
|
+
var fieldError = function fieldError(fieldName, errorSuffix) {
|
|
11
|
+
return "".concat(formatFieldNameForError(fieldName)).concat(errorSuffix);
|
|
12
|
+
};
|
|
2
13
|
var validateRequired = function validateRequired(options, fieldName) {
|
|
3
|
-
if (options.
|
|
14
|
+
if (options.optional) return;
|
|
15
|
+
if (options.required || options.minLength) throw new Error(fieldError(fieldName, 'Required'));
|
|
4
16
|
};
|
|
5
17
|
var validateLength = function validateLength(fieldValue, fieldName, minLength, maxLength) {
|
|
6
18
|
if (!minLength && !maxLength) return;
|
|
7
|
-
if (minLength && fieldValue.length < minLength) throw new Error(
|
|
8
|
-
if (maxLength && fieldValue.length > maxLength) throw new Error(
|
|
19
|
+
if (minLength && fieldValue.length < minLength) throw new Error(fieldError(fieldName, 'TooShort'));
|
|
20
|
+
if (maxLength && fieldValue.length > maxLength) throw new Error(fieldError(fieldName, 'TooLong'));
|
|
9
21
|
};
|
|
10
|
-
var
|
|
22
|
+
var validatePattern = function validatePattern(fieldValue, fieldName, pattern) {
|
|
23
|
+
if (!pattern) return;
|
|
24
|
+
if (!pattern.test(fieldValue)) throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
25
|
+
};
|
|
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;
|
|
49
|
+
};
|
|
50
|
+
var validateInclusion = function validateInclusion(fieldValue, fieldName, includedIn, pattern, numericality) {
|
|
11
51
|
if (fieldName.endsWith('[]')) {
|
|
12
52
|
// if the array has only 1 element, fieldValue is a string, not an array
|
|
13
53
|
var arrayValue = fieldValue.constructor === Array ? fieldValue : [fieldValue];
|
|
14
54
|
if (!arrayValue.every(function (value) {
|
|
15
55
|
return includedIn === null || includedIn === void 0 ? void 0 : includedIn.includes(value);
|
|
16
56
|
})) {
|
|
17
|
-
throw new Error(
|
|
57
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
58
|
+
}
|
|
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
|
+
});
|
|
18
66
|
}
|
|
19
67
|
return arrayValue;
|
|
20
68
|
} else {
|
|
21
69
|
if (includedIn && !includedIn.includes(fieldValue)) {
|
|
22
|
-
throw new Error(
|
|
70
|
+
throw new Error(fieldError(fieldName, 'IsInvalid'));
|
|
23
71
|
}
|
|
24
|
-
|
|
72
|
+
validatePattern(fieldValue, fieldName, pattern);
|
|
73
|
+
return validateNumericality(fieldValue, fieldName, numericality);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
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]);
|
|
25
81
|
}
|
|
82
|
+
return reqOrFieldValues[fieldName];
|
|
26
83
|
};
|
|
27
|
-
export var validField = function validField(
|
|
28
|
-
var _req$body;
|
|
84
|
+
export var validField = function validField(reqOrFieldValues, fieldName, options) {
|
|
29
85
|
if (!options) throw new Error('optionsRequired');
|
|
30
|
-
var fieldValue =
|
|
86
|
+
var fieldValue = fieldValueFrom(reqOrFieldValues, fieldName);
|
|
31
87
|
if (!fieldValue) {
|
|
32
88
|
validateRequired(options, fieldName);
|
|
33
89
|
return undefined;
|
|
34
90
|
}
|
|
35
91
|
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);
|
|
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);
|
|
37
93
|
};
|
|
38
|
-
export var validEmail = function validEmail(
|
|
39
|
-
var
|
|
40
|
-
var email =
|
|
94
|
+
export var validEmail = function validEmail(reqOrFieldValues) {
|
|
95
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
96
|
+
var email = fieldValueFrom(reqOrFieldValues, 'email');
|
|
41
97
|
if (!email) {
|
|
98
|
+
if (options.optional) return undefined;
|
|
42
99
|
throw new Error('emailRequired');
|
|
43
100
|
}
|
|
44
101
|
if (!emailIsValid(email)) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "light-paradate",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.1.0",
|
|
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,10 @@
|
|
|
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",
|
|
60
62
|
"@typescript-eslint/eslint-plugin": "^8.21.0",
|
|
63
|
+
"@typescript-eslint/parser": "^8.21.0",
|
|
64
|
+
"babel-jest": "^29.7.0",
|
|
61
65
|
"eslint": "^9.19.0",
|
|
62
66
|
"jest": "^29.7.0",
|
|
63
67
|
"typescript": "^5.7.3"
|