[Question] Inconsistent behavior of .cast() and .validate() methods
Author: egorovliCreated Jan 8, 2020Updated Sep 26, 2025
Labelsbug
After reading the documentation, I had an impression that the following two snippets should yield the same result, provided the input is valid and doesn't throw:
let result = await schema.validate(input)let result = schema.cast(input)But consider the following example:
let schema = yup.object({
price: yup.number()
// Allow empty strings since I'm validating an HTML form where default
// values for input[type=text] is an empty string
.transform(value => isNaN(value) ? undefined : value)
})
let input = {
price: ''
}
let result1 = await schema.validate(input) // {}
let result2 = schema.cast(input) // Uncaught TypeError: The value of price could not be cast to a value that satisfies the schema type: "number".Is this the intended behavior? Thanks.
Source: jquense/yup