light-paradate v1.1.1
light-paradate
Lightweight request params validator
Table of Contents
Introduction
This library helps validate incoming request data by checking for:
- Presence (required fields)
- Minimum length of string fields
- Value inclusion in a predefined list
- Email format validation
Support
- TypeScript Support: Includes type definitions.
- CJM, ESM support: Supports both CommonJS and ES modules.
Features
Installation
npm install light-paradateOr with Yarn:
yarn add light-paradateUsage
validField
validField(req, fieldName, options)
Extracts a field from req.query or req.body, then runs validations according to the provided options.
example:
const uuid = validField(req, 'uuid', { minLength: 36 });
const orderBy = validField(req, 'orderBy', { includedIn: ['name', 'email', 'status'] });
const statuses = validField(req, 'statuses[]', { includedIn: ['invited', 'active'] });
const firstName = validField(req, 'firstName', { minLength: 5 });Parameters
req: A request-like object withqueryandbody.fieldName: The field name to retrieve fromreq.queryorreq.body.options: An optional object containing validation requirements. Possible values for theoptionsargument include:required: boolean - Indicates whether the field must have a non-empty value.minLength: number - Specifies the minimum length the field value should have.maxLength: number - Specifies the maximum length the field value can have.includedIn: string[] - An array of valid strings that the field value must match to pass validation.
Return Value
- If valid, returns the string or string array (in case of fields that use
[]notation). - Returns
undefinedif the field is not required and not present. - Throws an error if validation fails.
Behavior
- Check presence: If
options.requiredis set or ifoptions.minLengthis defined, the field must be present or an error will be thrown. - Check length: If
options.minLengthis set, throws an error if the field’s value is shorter than the specified length. Check inclusion: If
options.includedInis defined, throws an error if the field’s value (or values) are not in the specified list.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.
validEmail
validEmail(req)
Extracts and validates the "email" field from req.query or req.body, ensuring it is both present and syntactically valid.
example:
const email = validEmail(req);Parameters
req: A request-like object with query and body.
Return Value
- Returns a valid email string if validation passes.
- Throws an error if the email is missing or invalid.
Behavior
- Checks presence: Throws an error
emailRequiredif the fieldemailis not found in eitherreq.queryorreq.body. - Checks format: Uses the
email-validatorpackage under the hood to ensure the format is correct. If invalid, throws an erroremailInvalid.
License
This project is licensed under the MIT License.
Package URL
https://www.npmjs.com/package/light-paradate
Code repository
https://bitbucket.org/endeavia/light-paradate/src/master/