light-paradate
v2.1.2
Published
Lightweight request params validator
Maintainers
Readme
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(reqOrFieldValues, fieldName, options)
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.
example:
const uuid = validField(req, 'uuid', { minLength: 36 });
const parentId = validField(req.params, 'parent_id', { numericality: { onlyInteger: true, greaterThan: 0 } });
const id = validField(req, 'id', { match: /^[1-9]\d*$/ });
const orderBy = validField(req.query, 'orderBy', { includedIn: ['name', 'email', 'status'] });
const statuses = validField(req.query, 'statuses[]', { includedIn: ['invited', 'active'] });
const firstName = validField(req.body, 'firstName', { minLength: 5 });Parameters
reqOrFieldValues: A request-like object withparams,query, andbody, or a direct field container such asreq.params,req.query, orreq.body.fieldName: The field name to retrieve from the request object or direct field container.options: An optional object containing validation requirements. Possible values for theoptionsargument include:optional: boolean - Allows the field to be omitted even when other validators such asminLengthare configured.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.match: RegExp - Validates the field value against a regular expression.numericality: boolean | object - Converts the field to a number and validates numeric constraints. Supported object keys:onlyInteger,greaterThan,greaterThanOrEqualTo,lessThan,lessThanOrEqualTo.includedIn: string[] - An array of valid strings that the field value must match to pass validation.
Return Value
- If valid, returns the string, number, string array, or number array (in case of fields that use
[]notation). - Returns
undefinedif the field is not required and not present, or ifoptional: trueis set and the field is omitted. - Throws an error if validation fails.
Behavior
Check presence: If
options.requiredis set or ifoptions.minLengthis defined, the field must be present unlessoptional: trueis set.Check length: If
options.minLengthis set, throws an error if the field’s value is shorter than the specified length.Check pattern: If
options.matchis defined, throws an error if the field’s value does not match the provided regular expression.Check numericality: If
options.numericalityis defined, converts the field to a number and throws an error if it is not numeric or does not satisfy the configured numeric constraints.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, options)
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);
const alternateEmail = validEmail(req, { optional: true });Parameters
req: A request-like object with query and body.options: Optional configuration.optional: boolean - Allowsemailto be omitted. If present, it is still validated.
Return Value
- Returns a valid email string if validation passes.
- Returns
undefinedifoptional: trueis set and the email is missing. - Throws an error if the email is invalid, or if it is missing without
optional: true.
Behavior
- Checks presence: Throws an error
emailRequiredif the fieldemailis not found in eitherreq.queryorreq.body, unlessoptional: trueis set. - 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/
