@noahvarghese/get_j_opts
v1.0.0-alpha.0
Published
validate options coming out of a JSON object
Downloads
3
Readme
get_j_opts
Allows retrieving and checking options passed in json object.
Usage
/**
* Example app setup using express
*/
import { Request, Response, Router } from "express";
import getJOpts, {
Expected,
TypeKey,
ValidatorMap,
} from "@noahvarghese/get_j_opts";
import validator from "validator";
const router = Router();
/**
* Basic Usage
*/
router.post("/", (req: Request, res: Response): void => {
const body: Expected = {
email: {
required: true,
type: "string" as TypeKey,
},
};
let data: { email: string };
try {
// Reads data from first parameter using expected values from data
data = getJOpts(req.body, body);
res.status(200).send(data.email);
} catch (_e) {
console.error((_e as Error).message);
// Throws error if a required field does not exist or is not of the type desired
res.sendStatus(400);
}
});
/**
* Adv. Usage
* Custom format checkers
*/
const keys = ["email"] as const;
type FormatKeys = typeof keys[number];
const formats: ValidatorMap<FormatKeys> = {
email: (v: unknown): boolean => validator.isEmail(v),
};
router.put("/", (req: Request, res: Response): void => {
const body: Expected<FormatKeys> = {
email: {
required: true,
type: "string",
format: "email",
},
};
let data: { email: string };
try {
// Reads data from first parameter using expected values from data
data = getJOpts(req.body, body, formats);
res.status(200).send(body.email);
} catch (_e) {
console.error((_e as Error).message);
// Throws error if a required field does not exist
// Or if it exists but is not of the type desired
// Or if it exists and the custom formatter fails/returns false
// or the format key doesn't match the key of one of the custom format functions
res.sendStatus(400);
}
});
Development - Getting Started
git clone https://github.com/noahvarghese/get_j_opts
cd ./get_j_opts
npm i
# Configure pre commit hook and set shell preferences
npm run init