Name

Valid8r provides a validator to validate names to different kinds of configurations that can be either customized globally or provided on every validation.

Valid8r covers all the basic as well as advanced name validations that covers 99% edge cases

Start by importing valid8r into your file:

import valid8r from '@c4code/valid8r';

Configuration Options

Valid8r name validator provides handful of options to best meet your requirements and validation needs, the options that can be configured in name validator are:

import valid8r from '@c4code/valid8r';

const input: string = "Jonathan Wilson2";

// The configuration can be defaulted globally
valid8r.name(input, {
  safe: false, // boolean
  onlyFirst: false, // boolean
  firstLast: true, // boolean
  fullNameWithMiddle: false, // boolean
  noSpChars: true, // boolean
  minLen: 6, // number
  maxLen: 99, // number
  minLenPerWord: 4, // number
  maxLenPerWord: 30, // number
  allowNumbers: false, // boolean
  properCapitalized: true, // boolean
  noLeadingSpaces: true, // boolean
  noTrailingSpaces: true, // boolean
  noConsecutiveSpaces: true, // boolean
  throwErrorsAs: "throw-all", // "throw-first" | "throw-last" | "throw-all",
  /*
  "throw-first" => Throws or returns the first error encountered,
  "throw-last" => Throws or returns the last error encountered,
  "throw-all" => Throws or returns all errors in a Array of string
  */
}); // the second argument is optional

The Configuration can be globally set for all the validators in your project, know more?

Basic Usage

Here's how we can use the name validator in it's most basic manner:

Start by importing valid8r:

Safe handling

By default, all the validations in valid8r works on unsafe flag, meaning if any of the validation fails it will throw an Error.

However, if you don't want it to throw any error but return them. set the safe flag to true.

Error Messages

If you don't like the default error message and want to set a custom error message for any specific fields, pass an object as a third argument of the name function:

Error Message Options

Valid8r name validator provides handful of options to configure error messages to best fit your project requirements

All the options are optional, if passed will be used as the configuration for that specific validator

The Error messages can be globally set for all the validators in your project, know more?

Last updated