Skip to content

v0.2.0

Compare
Choose a tag to compare
@koistya koistya released this 23 Mar 10:08
· 3 commits to main since this release

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);
}