for those who want a real and complex example :
Here is before upgrade version :
import * as Yup from 'yup';
interface optionsType {
id: string;
label: string;
value: string;
}
const formEpischema = Yup.object().shape(
{
attestationType: Yup.object({}).shape({
id: Yup.string().required(),
label: Yup.string().required(),
value: Yup.string().required()
}),
activityType: Yup.object({}).when('attestationType', {
is: (attestationType: optionsType) =>
attestationType.label !== 'Standard',
then: Yup.object({}).shape({
id: Yup.string().required(),
label: Yup.string().required(),
value: Yup.string().required()
}),
otherwise: Yup.object({
id: Yup.string(),
label: Yup.string(),
value: Yup.string()
})
}),
shoesType: Yup.array().when(['helmetType', 'otherEquipement'], {
is: (helmetType: optionsType[], otherEquipement: string) =>
!helmetType?.length && !otherEquipement,
then: Yup.array()
.of(
Yup.object().shape({
id: Yup.string().required(),
label: Yup.string().required(),
value: Yup.string().required()
})
)
.min(1)
.required()
}),
shoesCriteria: Yup.object({}).shape({
id: Yup.string(),
label: Yup.string(),
value: Yup.string()
}),
shoesSize: Yup.number().min(10).max(55),
helmetType: Yup.array().when(['shoesType', 'otherEquipement'], {
is: (shoesType: optionsType[], otherEquipement: string) =>
!shoesType?.length && !otherEquipement,
then: Yup.array()
.of(
Yup.object().shape({
id: Yup.string().required(),
label: Yup.string().required(),
value: Yup.string().required()
})
)
.min(1)
.required()
}),
otherEquipement: Yup.string()
.min(4)
.when(['shoesType', 'helmetType'], {
is: (shoesType: optionsType[], helmetType: optionsType[]) =>
!shoesType?.length && !helmetType?.length,
then: Yup.string().min(4).required()
}),
isCefri: Yup.boolean().oneOf([true, false]),
dosimeterType: Yup.object({}).when('isCefri', {
is: true,
then: Yup.object({}).shape({
id: Yup.string().required(),
label: Yup.string().required(),
value: Yup.string().required()
}),
otherwise: Yup.object({
id: Yup.string(),
label: Yup.string(),
value: Yup.string()
})
}),
dosimeterRef: Yup.string().when('isCefri', {
is: true,
then: Yup.string().min(7).max(7).required(),
otherwise: Yup.string()
})
},
[
['shoesType', 'helmetType'],
['shoesType', 'otherEquipement'],
['helmetType', 'otherEquipement']
]
);
export default formEpischema;
Here is now :
import * as Yup from 'yup';
interface optionsType {
id: string;
label: string;
value: string;
}
const formEpischema = Yup.object().shape({
attestationType: Yup.object({ // change is here 👈
id: Yup.string().required(),
label: Yup.string().required(),
value: Yup.string().required()
}),
activityType: Yup.object().when('attestationType', {
is: (attestationType: optionsType) => attestationType.label !== 'Standard',
then: () => // change is here 👈
Yup.object({
id: Yup.string().required(),
label: Yup.string().required(),
value: Yup.string().required()
}),
otherwise: () => // change is here 👈
Yup.object({
id: Yup.string(),
label: Yup.string(),
value: Yup.string()
})
}),
shoesType: Yup.array().when(['helmetType', 'otherEquipement'], {
is: (helmetType: optionsType[], otherEquipement: string) => !helmetType?.length && !otherEquipement,
then: () => // change is here 👈
Yup.array()
.of(
Yup.object().shape({
id: Yup.string().required(),
label: Yup.string().required(),
value: Yup.string().required()
})
)
.min(1)
.required()
}),
shoesCriteria: Yup.object({
id: Yup.string(),
label: Yup.string(),
value: Yup.string()
}),
shoesSize: Yup.number().min(10).max(55),
helmetType: Yup.array().when(['shoesType', 'otherEquipement'], {
is: (shoesType: optionsType[], otherEquipement: string) => !shoesType?.length && !otherEquipement,
then: () =>
Yup.array()
.of(
Yup.object().shape({
id: Yup.string().required(),
label: Yup.string().required(),
value: Yup.string().required()
})
)
.min(1)
.required()
}),
otherEquipement: Yup.string().min(4).when(['shoesType', 'helmetType'], {
is: (shoesType: optionsType[], helmetType: optionsType[]) => !shoesType?.length && !helmetType?.length,
then: () => Yup.string().min(4).required()
}),
isCefri: Yup.boolean().oneOf([true, false]),
dosimeterType: Yup.object().when('isCefri', {
is: true,
then: () =>
Yup.object({
id: Yup.string().required(),
label: Yup.string().required(),
value: Yup.string().required()
}),
otherwise: () =>
Yup.object({
id: Yup.string(),
label: Yup.string(),
value: Yup.string()
})
}),
dosimeterRef: Yup.string().when('isCefri', {
is: true,
then: (schema) => schema.min(7).max(7).required(),
otherwise: () => Yup.string()
})
},
[
['shoesType', 'helmetType'],
['shoesType', 'otherEquipement'],
['helmetType', 'otherEquipement']
]
);
export default formEpischema;
Old version (0.3): The is
, then
, and otherwise
functions of conditional validations accepted objects as arguments.
New version (1): The then and otherwise functions must return a modified schema. They now take empty arrow functions () => ... to return the modified schema.
Old version (0.3): Validation methods such as required()
, min(), max(), etc. were called directly on the fields (for example: Yup.string().required()).
New version (1): Validation methods must be called via the schema (for example: Yup.string().required() becomes Yup.string().required()).
Old version (0.3): The mixed()
method was used to create a schema when the type was not specified.
New version (1): The mixed()
method has been removed. You must specify the type directly when creating the schema (for example: Yup.string()).
Old version (0.3): The validate()
method directly returned validation errors if they existed, otherwise returned undefined.
New version (1): The validate() method returns a promise. You must use await or .then() to get the results.
Old version (0.3): The isValid()
method directly returned true if the validation was successful, otherwise returned false.
New version (1): The isValid() method returns a promise. You must use await or .then() to get the results.
Old version (0.3): The nullable()
and notRequired(
) methods were used to specify that a field could be null or not required.
New version (1): These methods have been removed. To allow a field to be null or not required, you can simply not call any validation method after the field (eg: Yup.string().nullable() just becomes Yup.string()).
New version (1): The oneOf()
method is now used to validate that a value is part of a given set. For example: Yup.string().oneOf(['value1', 'value2']).
These changes reflect some of the important differences between the two versions of Yup.