Questions tagged [yup]

Yup is a JavaScript object schema validator and object parser based on Joi

Yup is a JavaScript object schema validator and object parser. The API and style is inspired by Joi.

Yup is leaner than Joi: in the same spirit, without some of the fancy features. You can use it on the server as well, but in that case you might as well just use Joi.

Yup is also a good bit less opinionated than joi, allowing for custom transformations and async validation. It also allows "stacking" conditions via when for properties that depend on more than one other sibling or child property. Yup separates the parsing and validating functions into separate steps so it can be used to parse json separate from validating it, via the cast method.

Documentation and source can be found on the yup github page

1580 questions
169
votes
10 answers

Conditional Validation in Yup

I have an email field that only gets shown if a checkbox is selected (boolean value is true). When the form get submitted, I only what this field to be required if the checkbox is checked (boolean is true). This is what I've tried so far: const…
reectrix
  • 7,999
  • 20
  • 53
  • 81
102
votes
5 answers

Yup schema validation password and confirmPassword doesn't work

import * as Yup from 'yup'; import User from '../models/User'; class UserController { async store(req, res) { const schema = Yup.object().shape({ name: Yup.string().required(), email: Yup.string() .email() .required(), password:…
Player Josu
  • 1,133
  • 2
  • 6
  • 6
94
votes
17 answers

Validation using Yup to check string or number length

Is there a yup function that validates a specific length? I tried .min(5) and .max(5), but I want something that ensures the number is exactly 5 characters (ie, zip code).
reectrix
  • 7,999
  • 20
  • 53
  • 81
74
votes
10 answers

Validate phone number with Yup?

I'm trying to validate a phone number with Yup: phone: Yup.number() .typeError("That doesn't look like a phone number") .positive("A phone number can't start with a minus") .integer("A phone number can't include a decimal point") .min(8) …
Evanss
  • 23,390
  • 94
  • 282
  • 505
67
votes
3 answers

React Formik - Trigger validation only on form submit

I am using Formik in my React Native application. On the login form I have two fields: email and password, both of them are required. I've written such validation rules: export const LoginSchema = Yup.object().shape({ email: Yup.string() …
lecham
  • 2,294
  • 6
  • 22
  • 41
56
votes
5 answers

React formik form validation: How to initially have submit button disabled

Below is my React form validation code in which I am using formik. By default when the form loads, I want to keep the submit button disabled: import { useFormik } from "formik"; import * as Yup from "yup"; const formik = useFormik({ …
user8463989
  • 2,275
  • 4
  • 20
  • 48
53
votes
6 answers

Yup: deep validation in array of objects

I have a data structure like this: { "subject": "Ah yeah", "description": "Jeg siger...", "daysOfWeek": [ { "dayOfWeek": "MONDAY", "checked": false }, { "dayOfWeek": "TUESDAY", "checked": false }, { …
olefrank
  • 6,452
  • 14
  • 65
  • 90
44
votes
5 answers

Async validation with Formik, Yup and React

I want to make async validation with formik and validationschema with yup but i can't find the example or demo.
Day's Night
  • 613
  • 2
  • 8
  • 12
44
votes
2 answers

Yup validation with regex using matches problem

So I have below validation using Yup : const myValidtion = yup .string() .trim() .matches(/[abcdefghijklmnopqrstuvwxyz]+/ , 'Is not in correct format') .required(); So with this, this will pass : hello world as expected. But what make me confused…
Emad Dehnavi
  • 3,262
  • 4
  • 19
  • 44
43
votes
6 answers

Yup.when: "`NaN` (cast from the value `NaN`)"

I'm trying to implement a quite basic validation for a form field/select. Validation schema: vehicleProvider: Yup.object() // This is an object which is null by default .required('formvalidation.required.message') .nullable(),…
sandrooco
  • 8,016
  • 9
  • 48
  • 86
42
votes
5 answers

password validation with yup and formik

how would one go about having password validation but at the same time having the errors be passed to different variables? i.e password: Yup.string().required("Please provide a valid password"), passwordMin:…
Strahinja Ajvaz
  • 2,521
  • 5
  • 23
  • 38
41
votes
5 answers

Validating file presence with YUP

I'm using Yup to validate my form. In one of my form, I want to validate that one has a file. I've tested this (and it's not working): Yup.object().shape({ file: Yup.object().shape({ name:…
cappie013
  • 2,354
  • 1
  • 22
  • 30
39
votes
1 answer

What does the 'shape' function do in yup?

Most Yup examples use the shape method, but I find the documentation a little hard to understand why this is the case, and exactly what the method does. Can someone please explain the difference between a schema defined with Yup.object({...}) and…
cubabit
  • 2,527
  • 3
  • 21
  • 34
38
votes
1 answer

How can I make a yup number accept nullable values?

I have the following yup check: nrOfApples: yup.number().min(0).max(999), And right now if I leave the field blank, it validates as false. Is there any way to make yup.number() accept empty values? I have tried: yup.number().nullable() But it…
manzk
  • 586
  • 1
  • 4
  • 11
38
votes
8 answers

yup validation on multiple values

I want to validate my form using yup in formik. Suppose I have 4 fields A, B, C, D and they are all strings. How should I write the validation schema if I want to have at least one of the fields is not empty, then that's a valid form? Thanks in…
bunny
  • 1,797
  • 8
  • 29
  • 58
1
2 3
99 100