UNPKG

smart-id-validator

Version:

A package to validate official document numbers and other IDs format for various countries.

155 lines (134 loc) 4.3 kB
import { validatePhoneNumber, validateCNIC, validatePassport } from "../src"; describe("Pakistani Phone Number Validation", () => { test("Valid mobile numbers", () => { expect(validatePhoneNumber("+923001234567", "pk")).toEqual({ isValid: true, type: "mobile", formatted: "03001234567", network: "Jazz", country: "PK", }); expect(validatePhoneNumber("03001234567", "pk")).toEqual({ isValid: true, type: "mobile", formatted: "03001234567", network: "Jazz", country: "PK", }); expect(validatePhoneNumber("+923451234567", "pk")).toEqual({ isValid: true, type: "mobile", formatted: "03451234567", network: "Telenor", country: "PK", }); }); test("Valid landline numbers", () => { expect(validatePhoneNumber("02134567890", "pk")).toEqual({ isValid: true, type: "landline", formatted: "02134567890", country: "PK", }); expect(validatePhoneNumber("+922134567890", "pk")).toEqual({ isValid: true, type: "landline", formatted: "02134567890", country: "PK", }); }); test("Valid toll-free numbers", () => { expect(validatePhoneNumber("080012345", "pk")).toEqual({ isValid: true, type: "tollFree", formatted: "080012345", country: "PK", }); expect(validatePhoneNumber("+9280012345", "pk")).toEqual({ isValid: true, type: "tollFree", formatted: "080012345", country: "PK", }); }); test("Valid emergency numbers", () => { expect(validatePhoneNumber("1122", "pk")).toEqual({ isValid: true, type: "emergency", formatted: "1122", country: "PK", }); expect(validatePhoneNumber("15", "pk")).toEqual({ isValid: true, type: "emergency", formatted: "15", country: "PK", }); }); test("Valid shortcodes", () => { expect(validatePhoneNumber("34567", "pk")).toEqual({ isValid: true, type: "shortcode", formatted: "34567", country: "PK", }); expect(validatePhoneNumber("123", "pk")).toEqual({ isValid: true, type: "shortcode", formatted: "123", country: "PK", }); }); test("Invalid phone numbers", () => { expect(validatePhoneNumber("999999999999", "pk")).toEqual({ isValid: false }); expect(validatePhoneNumber("1234567890", "pk")).toEqual({ isValid: false }); expect(validatePhoneNumber("abcd1234567", "pk")).toEqual({ isValid: false }); expect(validatePhoneNumber("+92300", "pk")).toEqual({ isValid: false }); }); }); describe("Pakistani CNIC Validation", () => { test("Valid CNICs", () => { expect(validateCNIC("42101-1234567-1", "pk")).toEqual({ isValid: true, formatted: "42101-1234567-1", country: "PK", }); expect(validateCNIC("4210112345671", "pk")).toEqual({ isValid: true, formatted: "42101-1234567-1", country: "PK", }); expect(validateCNIC("42101 1234567 1", "pk")).toEqual({ isValid: true, formatted: "42101-1234567-1", country: "PK", }); }); test("Invalid CNICs", () => { expect(validateCNIC("42101-1234567", "pk")).toEqual({ isValid: false }); expect(validateCNIC("00101-1234567-1", "pk")).toEqual({ isValid: false }); expect(validateCNIC("abcd-1234567-1", "pk")).toEqual({ isValid: false }); expect(validateCNIC("9999912345671", "pk")).toEqual({ isValid: false }); }); }); describe("Pakistani Passport Validation", () => { test("Valid passport numbers", () => { expect(validatePassport("AB1234567", 'pk')).toEqual({ isValid: true, formatted: "AB1234567", country: "PK", }); expect(validatePassport("CD7654321", 'pk')).toEqual({ isValid: true, formatted: "CD7654321", country: "PK", }); }); test("Invalid passport numbers", () => { expect(validatePassport("A12345678", 'pk')).toEqual({ isValid: false }); expect(validatePassport("AB123456", 'pk')).toEqual({ isValid: false }); expect(validatePassport("123456789", 'pk')).toEqual({ isValid: false }); expect(validatePassport("AB12345678", 'pk')).toEqual({ isValid: false }); expect(validatePassport("ABCD12345", 'pk')).toEqual({ isValid: false }); }); });