Skip to content

Releases: kriasoft/validator-fluent

v0.4.0

23 Mar 16:19
Compare
Choose a tag to compare

Remove validate(input, validator, mapFn) overloaded method.

v0.3.0

23 Mar 10:39
Compare
Choose a tag to compare

Add .is(check, message?) validation rule

v0.2.0

23 Mar 10:08
Compare
Choose a tag to compare

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

22 Mar 14:18
Compare
Choose a tag to compare
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:

https://github.com/validatorjs/validator.js#validators