question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to check that a value is a number or change the default error message?

See original GitHub issue

Hi,

I have the following schema defined.

const schema = yup.object().shape({
  name: yup.string()
    .required('name is required'),
  age: yup.number()
    .positive('age must be greater than zero')
    .required('age is required')
});

I am also using this with react-formal. When I enter a value for myNumber form field that is not a number, say ‘abc’, I get the following error message:

this (value: ``) must be a `number` type

I’d like to specify the error message to display when the value is not a number but adding an error message to the number method doesn’t seem to work. Is there a way to do this?

Thanks

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:8

github_iconTop GitHub Comments

638reactions
danniefcommented, Apr 28, 2016

I got it. I needed to call the typeError() function

const schema = yup.object().shape({
  name: yup.string()
    .required('name is required'),
  age: yup.number()
    .typeError('age must be a number')
    .positive('age must be greater than zero')
    .required('age is required')
});

See: https://tonicdev.com/dannief/572294ef4a2c861200c181e5

7reactions
derpycodercommented, Apr 1, 2021

I found out 2 methods:

1. Not extremely strict: (Allows spaces in number input, if input type is text)

salary: yup.object({
    min: yup
        .number()
        .truncate()
        .positive("Must be more than Zero"),
    max: yup
        .number()
        .truncate()
        .moreThan(
            yup.ref("min"),
            "Must be more than Minimum"
        ),
    currency: yup.string(),
}),

2. Strict, Doesn’t even allow spaces: (misses out on number() functions though)

salary: yup.object({
    min: yup.string().matches(/^[0-9]+$/gi, "Must be a Number").required('Required'),
    max: yup.string().matches(/^[0-9]+$/gi, "Must be a Number").required('Required'),
    currency: yup.string().required(),
}),
Read more comments on GitHub >

github_iconTop Results From Across the Web

Change the default error message in the model state ...
Model validation occurs after model binding and reports errors ... FirstValue; // Check if the argument value is null or empty if (string....
Read more >
Video: Input and error messages - Microsoft Support
Select the cells that you want to create a message for, and click Data Validation. · On the Input Message tab, check the...
Read more >
I get a message about data type mismatch
To troubleshoot this message, try the following: If you just changed the Default Value for a field in a table and see a...
Read more >
Error.prototype.message - JavaScript - MDN Web Docs
This property contains a brief description of the error if one is available or has been set. The message property combined with the...
Read more >
Default Validator Messages | JET Developer Cookbook
Set focus to see the datepicker and its enabled dates. Type a value out of range and step off the field to see...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found