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.

Priority of [ typeError vs required ]

See original GitHub issue

Describe the bug

// my scheme
yup
    .number()
    .typeError('this is NaN')
    .positive('only positive')
    .integer('only int')
    .required('plz input the value'), // never comes here... 

when this scheme meets an empty string( like ""), I expected show error messege of required, which is plz input the value. But actual happen is that the error messege is this is NaN (which is typeError scheme)

In this case, required sechem never comes.

I want to show 'plz input the value' message when value is empty.

Thx in advance 😃

Platform (please complete the following information):

  • Browser [chrome]
  • Version ["yup": "^0.29.1"]

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
nl5887commented, Nov 4, 2021

The transform function also accepts a secondary value, originalValue. If you want to have empty values as valid, you can use:

let validator = Yup.number()
    .transform((value, originalValue) => {
        return (originalValue === '' ? undefined : value)
    })
    .typeError('Parameter is not a valid numeric value.')

Or if you want to distinguish invalid (type error) values from required values use:

let validator = Yup.number()
    .transform((value, originalValue) => {
        return (originalValue === '' ? undefined : value)
    })
    .typeError('Parameter is not a valid numeric value.')
    .required("Parameter is required.")
5reactions
CaitlinWeb-stcommented, Aug 18, 2020

For any of those confused like me, don’t use .default(undefined) use transform as described in #634

Yup.number()
    .transform(value => (isNaN(value) ? undefined : value)) // <---
    .required()
})

@jquense This would be really helpful to put into documentation about number().required(). I would expect it to transform like this under the hood for .required() because otherwise it’s essentially ignored/overruled by the typeError.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Appending a self constructed class in priority que gives ...
Herein, I am getting an error in while loop, after a few operations it fails to use the method q.get(), or q.put(). For...
Read more >
8. Errors and Exceptions — Python 3.11.1 documentation
The TypeError raised by dividing two strings is not handled by the except clause and therefore re-raised after the finally clause has been...
Read more >
Adding a priority value to tasks in a Flexible Job Shop Problem
Minimize(V*makespan+Wi*job_ends)? With what I've done now, I'm getting the following error: 'TypeError: Not an number: makespan'.
Read more >
Priority Queue in Python - blogboard.io
Priority queue is a data structure similar to a regular queue, but where elements have priorities assigned. Python comes with a built-in ...
Read more >
CSSStyleDeclaration.setProperty() - Web APIs | MDN
Thrown if the property or declaration block is read only. Alternative usage. If priority can be omitted, JavaScript has a special simpler syntax ......
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