validator.js-asserts
Version:
A set of extra asserts for validator.js.
52 lines (38 loc) • 838 B
JavaScript
;
/**
* Module dependencies.
*/
const { Validator, Violation } = require('validator.js');
let iban;
/**
* Optional peer dependencies.
*/
try {
iban = require('iban');
// eslint-disable-next-line no-empty
} catch {}
/**
* Export `InternationalBankAccountNumberAssert`.
*/
module.exports = function internationalBankAccountNumberAssert() {
if (!iban) {
throw new Error('iban is not installed');
}
/**
* Class name.
*/
this.__class__ = 'InternationalBankAccountNumber';
/**
* Validation algorithm.
*/
this.validate = value => {
if (typeof value !== 'string') {
throw new Violation(this, value, { value: Validator.errorCode.must_be_a_string });
}
if (!iban.isValid(value)) {
throw new Violation(this, value);
}
return true;
};
return this;
};