Logo

dev-resources.site

for different kinds of informations.

Yup Schema Commonly Used Examples

Published at
10/15/2020
Categories
javascript
schema
yup
regex
Author
CP
Categories
4 categories in total
javascript
open
schema
open
yup
open
regex
open
Yup Schema Commonly Used Examples

Here are two commonly used schema validations using Yup:

  1. Phone number validation with Regex
  2. How to compare two fields in Yup
import * as yup from "yup";

const phoneRegex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
const schema = yup.object().shape({
  phone: yup.string().matches(phoneRegex, "Invalid phone."),
  password: yup.string().required("Password is required"),
  confirmPassword: yup
    .string()
    .oneOf([yup.ref("password")], "Mismatched passwords")
    .required("Please confirm your password")
});

export default schema;

Featured ones: