tomdye / yup-phone-lite

Adds a phone number validation check to yup validator using libphonenumber-js

Home Page:https://www.npmjs.com/package/yup-phone-lite

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

yup-phone-lite

MIT License npm - yup-phone-lite bundle size - yup-phone-lite bundle size - yup-phone-lite Total Downloads - yup-phone-lite

Adds a phone number validation check to yup validator using libphonenumber-js which gives accurate validation checks.
Read more about the core library here libphonenumber.

This package is a fork of yup-phone made by abhisekp. It replaces google-libphonenumber with the much smaller port libphonenumber-js with the intention of drastically reducing the bundle size. The only drawback is that a few phone numbers will slip through the cracks and give false positives. If that is an issue for you, go ahead and use the original package!

Install

npm install --save yup-phone-lite
# yarn add yup-phone-lite

Examples

import * as Yup from "yup";
// const Yup = require("yup");
import "yup-phone-lite";
// require("yup-phone-lite");

// validate any phone number (defaults to The United States for country)
const phoneSchema = Yup.string().phone().required();

(async () => {
  console.log(await phoneSchema.isValid("(541) 754-3010")); // → true
})();

import * as Yup from "yup";
// const Yup = require("yup");
import "yup-phone-lite";
// require("yup-phone-lite");

// validate phone number loosely in the given region
const phoneSchema = Yup.string().phone("IN").required();

(async () => {
  console.log(await phoneSchema.isValid("+919876543210")); // → true
})();

import * as Yup from "yup";
// const Yup = require("yup");
import "yup-phone-lite";
// require("yup-phone-lite");

// validate phone number strictly in the given region with custom error message
const phoneSchema = Yup.string()
  .phone("IN", true, "${path} is invalid")
  .required();

try {
  phoneSchema.validateSync("+1-541-754-3010");
} catch (error) {
  console.log(error.message); // → this is invalid
}

For more examples, check yup-phone-lite.test.ts file.

Contributing

  • Uses Rollup for bundling.
  • Files are minified using closure compiler.
  • Uses jest for testing.
  • Generates CJS, UMD, and ESM builds.
  • Use npm version --major|--minor|--patch to version.
  • Use tslint and prettier for code formatting.
  • Uses semantic release for version.
$ npm run build # Build for production
$ npm test # Run tests
$ npm publish # Publish npm package (prompts for version)

License

MIT

About

Adds a phone number validation check to yup validator using libphonenumber-js

https://www.npmjs.com/package/yup-phone-lite

License:MIT License


Languages

Language:JavaScript 58.6%Language:TypeScript 41.4%