@neoncoder/validator-utils
v0.0.14
Published
A Collection of utility functions to help with input validation both client and server side.
Downloads
10
Maintainers
Readme
Validator Utils
A Collection of utility functions to help with input validation both client and server side.
Installation
npm install @neoncoder/validator-utils
Usage
Standalone usage
- Import and use any functions you need from the package - Available functions are in the Functions list
import express, {Request, Response} from 'express';
import {isValidDate} from '@neoncoder/validator-utils';
import {BadRequest, OK ServiceResponse} from '@neoncoder/service-response';
const app = express();
app.get("/search", (req: Request, res: Response) => {
let sr: ServiceResponse;
const {startDate, endDate} = req.query
if(!isValidDate(startDate)) {
sr = BadRequest({message: 'Invalid Start Date'})
return res.status(sr.statusCode).send(sr);
}
const now = new Date(Date.now())
const result = isValidDate(endDate)
&& new Date(startDate) <= new Date(endDate)
? searchDataByDate(startDate, endDate)
: !isValidDate(endDate) && new Date(startDate) <= now
? searchDataByDate(startDate, now)
: null;
sr = result ? OK({data: result}) : BadRequest({message: 'Invalid search dates'})
return res.status(sr.statusCode).send(sr);
})
Use with a validation Library (like Zod)
import {
boolean, number, object, string,
} from 'zod';
import { isNotEmpty, isNumbersOnly, isValidObjectId } from '@neoncoder/validator-utils';
export const createAccountSchema = object({
body: object({
// using string instead of numbers allows the number to start with 0s (Zeros)
accountNumber: string({
required_error: 'Account number is required',
})
.min(10, 'Account number must be 10 digits long')
.max(10, 'Account number must be 10 digits long')
.refine((data) => isNumbersOnly(data), 'Account number must be numbers only'),
// Ensures bankId is not only present, but is a valid mongodb Object Id
bankId: string({
required_error: 'Bank Id is required',
}).refine((data) => isNotEmpty(data), 'Bank Id cannot be empty')
.refine((data) => isValidObjectId(data), 'Invalid Bank Id'),
})
});
Functions list
isValidEmail
: Checks if entered email is validisBoolean
: Checks if a value is boolean (true or false) - returns true even if the value is false, as false is also a booleanisNotEmpty
: Checks if a string is empty or notisNumbersOnly
: Checks if a string is digits onlyisOverDaysOld
: Checks if a timestamp is over a certain number of days from nowisValidAlphaNum
: Checks if a string is alphanumeric (contains only numbers and letters)isValidDate
: Checks if a string is a valid dateisValidImage
: Checks if a file extension is a valid imageisValidName
: Checks if entered string is valid name i.e a-z,A-Z and dashes (-)isValidPhone
: Checks if a string contains only numbers and dashes (like a phone number)isValidString
: Checks if a string is not empty and doesn't contain only spacesonlyOneTruthy
: Checks if an array of boolean object properties have only one true at a timesanitizeData
: Extracts properties from an object and returns a new object with extracted propertiesstripHTML
: Removes any html tags from a stringisValidObjectId
: Checks if a string is a valid 24 char hexString (like MongoDB ID)