Yup: How to change error message "must be a `number` type..."

Created on 14 May 2018  路  4Comments  路  Source: jquense/yup

I try validate number field.

if I get value of type string or special symbols for example "++++" I get validation error "temperature must be a number type, but the final value was: NaN (cast from the value "++++")."

How can I to change this error message?
if I use typeError(), I not get messages from check on matches and required.

I need check matches, reuqired, min and max for number field. What is best way?

// matches
if (!/^([0]([.][0-9]+)?|[1-9]([0-9]+)?([.][0-9]+)?)$/.test(temperature)) {
        errors.temperature = 'Invalid format. Example 22, 22.1.';
}  

// required
if (/^\s*$/.test(temperature)) { 
        errors.temperature = 'Please enter temperature. Value from 0 to 30.';            
} 

// min and max
Yup.object().shape({           
        temperature: Yup.number()
                        .min(0, 'Min value 0.')
                        .max(30, 'Max value 30.'),  
})        

Most helpful comment

schema.typeError('you must specify a number')

All 4 comments

So, how did you do this? :) I have the same issue. Please, post a solution ;)

schema.typeError('you must specify a number')

For those who were a little confused like me from the previous answer, here's a good example: https://github.com/jquense/yup/issues/47#issuecomment-215588412

(errors.email as any)?.message it's work for me

Was this page helpful?
0 / 5 - 0 ratings