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:

import valid8r from '@c4code/valid8r';
import valid8r from '@c4code/valid8r';

// Your input's value
const input2: string = "M4rk Black";
valid8r.name(input); // throws NameValidationFailed error

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.

import valid8r from '@c4code/valid8r';

// Your input's value
const input2: string = "M4rk Black";
const [isValid, errors] = valid8r.name(input, { safe: true });

if (!isValid) {
    console.log(errors); 
    // [{ noSpChars: "Name should not contain any special characters." }]
}

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:

import valid8r from '@c4code/valid8r';

// Your input's value
const input2: string = "M4rk Black";
const [isValid, errors] = valid8r.name(input, {
    safe: true
}, {
    noSpChars: "Special Characters are not allowed!"
});

if (!isValid) {
    console.log(errors); 
    // [{ noSpChars: "Special Characters are not allowed!" }]
}

Error Message Options

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

import valid8r from '@c4code/valid8r';

const input: string = "Jonathan Wilson2";

// The configuration can be defaulted globally
valid8r.name(input, { /* Field Configurations */ }, {
  onlyFirst: 
    "Only a first name is allowed.", // string
  firstLast: 
    "Name must include both first and last names.", // string
  fullNameWithMiddle: 
    "Name must include first, middle and last name.", // string
  noSpChars: 
    "Name should not contain any special characters.", // string
  minLen: 
    "Name should be atleast {minLen} characters long.", // string
  maxLen: 
    "Name must not exceed {maxLen} characters.", // string
  minLenPerWord: 
    "Each word must be at least {minLenPerWord} characters long.", // string
  maxLenPerWord: 
    "Each word must not exceed {maxLenPerWord} characters.", // string
  allowNumbers: 
    "Name should not contain numbers.", // string
  properCapitalized: 
    "Each word must be properly capitalized.", // string
  noLeadingSpaces: 
    "Name should not have leading spaces.", // string
  noTrailingSpaces: 
    "Name should not have trailing spaces.", // string
  noConsecutiveSpaces: 
    "Name should not contain consecutive spaces.", // string
}); // the second and third argument is optional

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