Releases: kriasoft/validator-fluent
Releases · kriasoft/validator-fluent
v0.4.0
v0.3.0
v0.2.0
Allow to extend the built-in Validator
class:
import { validate, Validator, ValidationError } from "validator-fluent";
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;
}
}
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);
}
v0.1.0
import { validate, ValidationError } from "validator-fluent";
const input = {
givenName: "John",
familyName: "Doe",
email: "[email protected]",
phone: "(555) 555-55-55",
age: "18",
};
// Do not validate empty fields (validation only)
const dryRun = true;
const [data, errors] = validate(input, (value) => ({
given_name: value("givenName")
.notEmpty({ if: !dryRun })
.isLength({ min: 3, max: 25 }),
family_name: value("familyName")
.notEmpty({ if: !dryRun })
.isLength({ min: 1, max: 25 }),
email: value("email").notEmpty().isEmail(),
phone: value("phone").isMobilePhone({ locale: "en-US" }),
age: value("age").toNumber(),
}));
if (errors) {
throw new ValidationError(errors);
}
if (!dryRun) {
await db.table("customer").insert(data);
}
For the full list of available validation rules please refer to: