Card

Validating Cards is rather simple but time consuming task, valid8r Cards validators provides a handful of options to best suit your validation requirements

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

Start by importing valid8r into your file:

import valid8r from '@c4code/valid8r';

Configuration Options

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

import valid8r, { CardType } from '@c4code/valid8r';

const card: CardType = {
  number: "4762508284613371", // 13-19 Number format
  expirationDate: '02/29', // 'MM/YY'
  cardHolderName: 'John Smith',
  cvv: 933 // 3 digit cvv
}

// The configuration can be defaulted globally
valid8r.card(card, {
  safe: false, // 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 CardType type can be used to construct card that can be imported from '@c4code/valid8r' as a named export.

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

Basic Usage

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

Start by importing valid8r:

import valid8r from '@c4code/valid8r';
import valid8r, { CardType } from '@c4code/valid8r';

const card: CardType = {
  number: "4762508284613371",
  expirationDate: '02/21',
  cardHolderName: 'John Smith',
  cvv: 933
}

valid8r.card(card); // throws CardValidationFailed error as the card is expired

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, { CardType } from '@c4code/valid8r';

const card: CardType = {
  number: "4762508284613371",
  expirationDate: '02/21',
  cardHolderName: 'John Smith',
  cvv: 933
}

const [isValid, errors] = valid8r.card(
  { ...card,
    safe: true },
);


if (!isValid) {
  console.log(errors);
    // [{ expirationDate : "Card is expired!" }]
}

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 card function:

import valid8r, { CardType } from '@c4code/valid8r';

const card: CardType = {
  number: "4762508284613371",
  expirationDate: '02/26',
  cardHolderName: 'John Smith',
  cvv: 9334
}

const [isValid, errors] = valid8r.card(
  { ...card,
    safe: true },
  { cvv: "Enter a valid CVV." }
);

if (!isValid) {
  console.log(errors);
    // [{ cvv : "Enter a valid CVV" }]
}

Error Message Options

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

import valid8r from '@bitbybit/valid8r';

const username: string = "jo#n_sm1th";

// The configuration can be defaulted globally
valid8r.username(username, { /* Field Configurations */ }, {
  number:
    "Invalid card number.", // string
  expirationDate: 
    "Card is expired!", // string
  cvv: 
    "Invalid CVV.", // string
  cardHolderName: 
    "Invalid cardholder name.", // 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