Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

SafetyCulture/joi-verhoeff

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

joi-verhoeff

Build Status Coverage Status

Provides a Joi rule to validate strings as sequences of numerals with a valid verhoeff checksum digit.

Strings are validated as being entirely composed of digits, and containing a valid checksum using node-verhoeff.

Usage

import BaseJoi from 'joi';
import JoiVerhoeff from 'joi-verhoeff';

const Joi = BaseJoi.extend(JoiVerhoeff);

// Create a joi schema using the Verhoeff validator;
const schema = Joi.string().verhoeff();

Results with valid checksum:

Joi.validate('3789125', Joi.string().verhoeff());
/*
  {
    error: null,
    value: '3789125'
  }
 */

Results with invalid checksum:

Joi.validate('3789122', Joi.string().verhoeff());
/*
{ error: {
    [ValidationError: "value" checksum failure]
    isJoi: true,
    name: 'ValidationError',
    details: [{
      message: '"value" checksum failure',
      path: 'value',
      type: 'string.verhoeff',
      context: { v: '3789122', key: 'value' }
    }],
    ...
  },
  value: '3789122'
}
 */