light-paradate 1.0.1 → 1.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 CHANGED
@@ -16,12 +16,14 @@ This library helps validate incoming request data by checking for:
16
16
  - Value inclusion in a predefined list
17
17
  - Email format validation
18
18
 
19
+ ## Support
20
+ - **TypeScript Support**: Includes type definitions.
21
+ - **CJM, ESM support**: Supports both CommonJS and ES modules.
22
+
19
23
  ## Features
20
24
  - [validField](#validfield)
21
25
  - [validEmail](#validemail)
22
26
 
23
- **CJM, ESM support**: Supports both CommonJS and ES modules.
24
-
25
27
  ## Installation
26
28
 
27
29
  ```bash
@@ -51,6 +53,11 @@ const firstName = validField(req, 'firstName', { minLength: 5 });
51
53
  - `req`: A request-like object with `query` and `body`.
52
54
  - `fieldName`: The field name to retrieve from `req.query` or `req.body`.
53
55
  - `options`: An optional object containing validation requirements.
56
+ Possible values for the `options` argument include:
57
+ - `required`: boolean - Indicates whether the field must have a non-empty value.
58
+ - `minLength`: number - Specifies the minimum length the field value should have.
59
+ - `maxLength`: number - Specifies the maximum length the field value can have.
60
+ - `includedIn`: string[] - An array of valid strings that the field value must match to pass validation.
54
61
 
55
62
  #### Return Value
56
63
  - If valid, returns the string or string array (in case of fields that use `[]` notation).
@@ -1,14 +1,15 @@
1
1
  interface OptionsType {
2
- required?: boolean;
3
2
  includedIn?: string[];
4
3
  minLength?: number;
4
+ maxLength?: number;
5
+ required?: boolean;
5
6
  }
6
7
  interface RequestType {
7
8
  query: {
8
- [key: string]: any;
9
+ [key: string]: string | undefined;
9
10
  };
10
11
  body: {
11
- [key: string]: any;
12
+ [key: string]: string | undefined;
12
13
  };
13
14
  }
14
15
  export declare const validField: (req: RequestType, fieldName: string, options: OptionsType) => string | string[] | undefined;
package/dist/cjs/index.js CHANGED
@@ -8,10 +8,10 @@ var _emailValidator = require("email-validator");
8
8
  var validateRequired = function validateRequired(options, fieldName) {
9
9
  if (options.required || options.minLength) throw new Error("".concat(fieldName, "Required"));
10
10
  };
11
- var validateLength = function validateLength(fieldValue, fieldName, minLength) {
12
- if (minLength && fieldValue.length < minLength) {
13
- throw new Error("".concat(fieldName, "TooShort"));
14
- }
11
+ var validateLength = function validateLength(fieldValue, fieldName, minLength, maxLength) {
12
+ if (!minLength && !maxLength) return;
13
+ if (minLength && fieldValue.length < minLength) throw new Error("".concat(fieldName, "TooShort"));
14
+ if (maxLength && fieldValue.length > maxLength) throw new Error("".concat(fieldName, "TooLong"));
15
15
  };
16
16
  var validateInclusion = function validateInclusion(fieldValue, fieldName, includedIn) {
17
17
  if (fieldName.endsWith('[]')) {
@@ -38,7 +38,7 @@ var validField = exports.validField = function validField(req, fieldName, option
38
38
  validateRequired(options, fieldName);
39
39
  return undefined;
40
40
  }
41
- validateLength(fieldValue, fieldName, options === null || options === void 0 ? void 0 : options.minLength);
41
+ validateLength(fieldValue, fieldName, options === null || options === void 0 ? void 0 : options.minLength, options === null || options === void 0 ? void 0 : options.maxLength);
42
42
  return validateInclusion(fieldValue, fieldName, options === null || options === void 0 ? void 0 : options.includedIn);
43
43
  };
44
44
  var validEmail = exports.validEmail = function validEmail(req) {
package/dist/esm/index.js CHANGED
@@ -2,10 +2,10 @@ import { validate as emailIsValid } from 'email-validator';
2
2
  var validateRequired = function validateRequired(options, fieldName) {
3
3
  if (options.required || options.minLength) throw new Error("".concat(fieldName, "Required"));
4
4
  };
5
- var validateLength = function validateLength(fieldValue, fieldName, minLength) {
6
- if (minLength && fieldValue.length < minLength) {
7
- throw new Error("".concat(fieldName, "TooShort"));
8
- }
5
+ var validateLength = function validateLength(fieldValue, fieldName, minLength, maxLength) {
6
+ if (!minLength && !maxLength) return;
7
+ if (minLength && fieldValue.length < minLength) throw new Error("".concat(fieldName, "TooShort"));
8
+ if (maxLength && fieldValue.length > maxLength) throw new Error("".concat(fieldName, "TooLong"));
9
9
  };
10
10
  var validateInclusion = function validateInclusion(fieldValue, fieldName, includedIn) {
11
11
  if (fieldName.endsWith('[]')) {
@@ -32,7 +32,7 @@ export var validField = function validField(req, fieldName, options) {
32
32
  validateRequired(options, fieldName);
33
33
  return undefined;
34
34
  }
35
- validateLength(fieldValue, fieldName, options === null || options === void 0 ? void 0 : options.minLength);
35
+ validateLength(fieldValue, fieldName, options === null || options === void 0 ? void 0 : options.minLength, options === null || options === void 0 ? void 0 : options.maxLength);
36
36
  return validateInclusion(fieldValue, fieldName, options === null || options === void 0 ? void 0 : options.includedIn);
37
37
  };
38
38
  export var validEmail = function validEmail(req) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "light-paradate",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "Lightweight request params validator",
5
5
  "author": "Tamas Kamaras",
6
6
  "repository": {
@@ -37,6 +37,9 @@
37
37
  "validator",
38
38
  "email-validator",
39
39
  "email validation",
40
+ "light",
41
+ "light-paradate",
42
+ "paradate",
40
43
  "parameter validation",
41
44
  "params",
42
45
  "params validation",
@@ -51,6 +54,8 @@
51
54
  "@babel/core": "^7.26.0",
52
55
  "@babel/preset-env": "^7.26.0",
53
56
  "@babel/preset-typescript": "^7.26.0",
57
+ "@typescript-eslint/eslint-plugin": "^8.21.0",
58
+ "eslint": "^9.19.0",
54
59
  "jest": "^29.7.0",
55
60
  "typescript": "^5.7.3"
56
61
  }