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-paradate
Or with Yarn:
yarn add light-paradate
Usage
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 withquery
andbody
.fieldName
: The field name to retrieve fromreq.query
orreq.body
.options
: An optional object containing validation requirements. Possible values for theoptions
argument 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
undefined
if the field is not required and not present. - Throws an error if validation fails.
Behavior
- Check presence: If
options.required
is set or ifoptions.minLength
is defined, the field must be present or an error will be thrown. - Check length: If
options.minLength
is set, throws an error if the field’s value is shorter than the specified length. Check inclusion: If
options.includedIn
is 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
emailRequired
if the fieldemail
is not found in eitherreq.query
orreq.body
. - Checks format: Uses the
email-validator
package 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/