Using Validate.js via a fluent TypeScript interface

import { validate } from "validator-fluent";const input = {
givenName: "John",
familyName: "Doe",
email: "john@example.com",
age: "18",
website: "",
};
const [data, errors] = validate(input, (value) => ({
given_name: value("givenName")
.notEmpty()
.isLength({ min: 3, max: 25 }),
family_name: value("familyName")
.notEmpty()
.isLength({ min: 1, max: 25 }),
email: value("email")
.notEmpty()
.isEmail(),
age: value("aga").toNumber(),
website: value("website").isURL(),
});
if (Object.keys(errors).length === 0) {
await db.table("customer").insert(data);
}

Validation Only (mode)

const input = {
email: "john@example.com"
};
// Validate user input and exit
const dryRun = true;
const [data, errors] = validate(input, (value) => ({
email_address: value("email")
.notEmpty({ if: !dryRun })
.isEmail(),
});
if (!dryRun && Object.keys(errors).length === 0) {
await db.table("customer").insert(data);
}

Adding Custom Rules

import { Validator } from "validator-fluent";export class CoolValidator<K, V> extends Validator<K, V> {
constructor(key: K, value: V) {
super(key, value);
}
isLegit(): this {
if (!this.isEmpty && this.value !== "legit") {
this.errors.push("Not legit.");
}
return this;
}
}
// -----------------------------------------------------------------import { validate, ValidationError } from "validator-fluent";const input = { name: "???" };const [data, errors] = validate(input, CoolValidator, (value) => ({
name: value("name").notEmpty().isLegit(),
}));
if (Object.key(errors).length > 0) {
throw new ValidationError(errors);
}

References

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store