light-paradate 1.0.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/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ MIT License
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,96 @@
1
+ # light-paradate
2
+
3
+ ## Lightweight request params validator
4
+
5
+ ## Table of Contents
6
+ - [Introduction](#introduction)
7
+ - [Features](#features)
8
+ - [Installation](#installation)
9
+ - [Usage](#usage)
10
+ - [License](#license)
11
+
12
+ ## Introduction
13
+ This library helps validate incoming request data by checking for:
14
+ - Presence (required fields)
15
+ - Minimum length of string fields
16
+ - Value inclusion in a predefined list
17
+ - Email format validation
18
+
19
+ ## Features
20
+ - [validField](#validfield)
21
+ - [validEmail](#validemail)
22
+
23
+ ## Installation
24
+
25
+ ```bash
26
+ npm install light-paradate
27
+ ```
28
+ Or with Yarn:
29
+ ```bash
30
+ yarn add light-paradate
31
+ ```
32
+
33
+ ## Usage
34
+ ### validField
35
+
36
+ `validField(req, fieldName, options)`
37
+
38
+ Extracts a field from `req.query` or `req.body`, then runs validations according to the provided options.
39
+
40
+ example:
41
+ ```js
42
+ const uuid = validField(req, 'uuid', { minLength: 36 });
43
+ const orderBy = validField(req, 'orderBy', { includedIn: ['name', 'email', 'status'] });
44
+ const statuses = validField(req, 'statuses[]', { includedIn: ['invited', 'active'] });
45
+ const firstName = validField(req, 'firstName', { minLength: 5 });
46
+ ```
47
+
48
+ #### Parameters
49
+ - `req`: A request-like object with `query` and `body`.
50
+ - `fieldName`: The field name to retrieve from `req.query` or `req.body`.
51
+ - `options`: An optional object containing validation requirements.
52
+
53
+ #### Return Value
54
+ - If valid, returns the string or string array (in case of fields that use `[]` notation).
55
+ - Returns `undefined` if the field is not required and not present.
56
+ - Throws an error if validation fails.
57
+
58
+ #### Behavior
59
+ - Check presence: If `options.required` is set or if `options.minLength` is defined, the field must be present or an error will be thrown.
60
+ - Check length: If `options.minLength` is set, throws an error if the field’s value is shorter than the specified length.
61
+ - Check inclusion: If `options.includedIn` is defined, throws an error if the field’s value (or values) are not in the specified list.
62
+
63
+ 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.
64
+
65
+ ### validEmail
66
+
67
+ `validEmail(req)`
68
+
69
+ Extracts and validates the "email" field from `req.query` or `req.body`, ensuring it is both present and syntactically valid.
70
+
71
+ example:
72
+ ```js
73
+ const email = validEmail(req);
74
+ ```
75
+ #### Parameters
76
+ - `req`: A request-like object with query and body.
77
+
78
+ #### Return Value
79
+ - Returns a valid email string if validation passes.
80
+ - Throws an error if the email is missing or invalid.
81
+
82
+ #### Behavior
83
+ - Checks presence: Throws an error `emailRequired` if the field `email` is not found in either `req.query` or `req.body`.
84
+ - Checks format: Uses the `email-validator` package under the hood to ensure the format is correct. If invalid, throws an error `emailInvalid`.
85
+
86
+ #### License
87
+ This project is licensed under the [MIT License](https://bitbucket.org/endeavia/light-paradate/src/master/LICENSE).
88
+
89
+ #### Package URL
90
+ https://www.npmjs.com/package/light-paradate
91
+
92
+ #### Code repository
93
+ https://bitbucket.org/endeavia/light-paradate/src/master/
94
+
95
+ #### Author
96
+ [Tamás Kamarás](https://www.npmjs.com/~tamas.kamaras)
@@ -0,0 +1,12 @@
1
+ interface OptionsType {
2
+ required?: boolean;
3
+ includedIn?: string[];
4
+ minLength?: number;
5
+ }
6
+ interface RequestType {
7
+ query: Record<string, string>;
8
+ body: Record<string, string>;
9
+ }
10
+ export declare const validField: (req: RequestType, fieldName: string, options: OptionsType) => string | string[] | undefined;
11
+ export declare const validEmail: (req: RequestType) => string | undefined;
12
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,47 @@
1
+ import { validate as emailIsValid } from 'email-validator';
2
+ const validateRequired = (options, fieldName) => {
3
+ if (options.required || options.minLength)
4
+ throw new Error(`${fieldName}Required`);
5
+ };
6
+ const validateLength = (fieldValue, fieldName, minLength) => {
7
+ if (minLength && fieldValue.length < minLength) {
8
+ throw new Error(`${fieldName}TooShort`);
9
+ }
10
+ };
11
+ const validateInclusion = (fieldValue, fieldName, includedIn) => {
12
+ if (fieldName.endsWith('[]')) {
13
+ // if the array has only 1 element, fieldValue is a string, not an array
14
+ const arrayValue = (fieldValue.constructor === Array ? fieldValue : [fieldValue]);
15
+ if (!arrayValue.every(value => includedIn?.includes(value))) {
16
+ throw new Error(`${fieldName}IsInvalid`);
17
+ }
18
+ return arrayValue;
19
+ }
20
+ else {
21
+ if (includedIn && !includedIn.includes(fieldValue)) {
22
+ throw new Error(`${fieldName}IsInvalid`);
23
+ }
24
+ return fieldValue;
25
+ }
26
+ };
27
+ export const validField = (req, fieldName, options) => {
28
+ if (!options)
29
+ throw new Error('optionsRequired');
30
+ const fieldValue = req.query[fieldName] || req.body?.[fieldName];
31
+ if (!fieldValue) {
32
+ validateRequired(options, fieldName);
33
+ return undefined;
34
+ }
35
+ validateLength(fieldValue, fieldName, options?.minLength);
36
+ return validateInclusion(fieldValue, fieldName, options?.includedIn);
37
+ };
38
+ export const validEmail = (req) => {
39
+ const email = req.query.email || req.body?.email;
40
+ if (!email) {
41
+ throw new Error('emailRequired');
42
+ }
43
+ if (!emailIsValid(email)) {
44
+ throw new Error('emailInvalid');
45
+ }
46
+ return email;
47
+ };
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "light-paradate",
3
+ "version": "1.0.0",
4
+ "description": "Lightweight request params validator",
5
+ "author": "Tamas Kamaras",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://bitbucket.org/endeavia/light-paradate"
9
+ },
10
+ "main": "dist/index.js",
11
+ "types": "dist/index.d.ts",
12
+ "type": "module",
13
+ "files": [
14
+ "dist/index.js",
15
+ "dist/index.d.ts",
16
+ "LICENSE",
17
+ "README.md"
18
+ ],
19
+ "scripts": {
20
+ "build": "tsc",
21
+ "test": "npm run build && jest --config=tests/jest.config.js"
22
+ },
23
+ "keywords": [
24
+ "node",
25
+ "nodejs",
26
+ "email",
27
+ "validator",
28
+ "email-validator",
29
+ "email validation",
30
+ "parameter validation",
31
+ "params",
32
+ "params validation",
33
+ "parameter"
34
+ ],
35
+ "license": "MIT",
36
+ "dependencies": {
37
+ "email-validator": "^2.0.4"
38
+ },
39
+ "devDependencies": {
40
+ "@babel/preset-env": "^7.26.0",
41
+ "jest": "^29.7.0",
42
+ "typescript": "^5.7.3"
43
+ }
44
+ }