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!
npm install --save yup-phone-lite
# yarn add yup-phone-lite
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.
- 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)