admin管理员组

文章数量:1221019

I'm wondering if someone could advise me on how to get yup to validate strings of any length (including length 0).

Just using

yup.string().required().validateSync("")

will throw an error on an empty string...

In the past this was the recommended way to do it:

string().required().min(0) 

but that way no longer works.. ()

Can someone advise me on how to get yup to require that a string is sent, but to not error on length 0 strings?

Thanks!

I'm wondering if someone could advise me on how to get yup to validate strings of any length (including length 0).

Just using

yup.string().required().validateSync("")

will throw an error on an empty string...

In the past this was the recommended way to do it:

string().required().min(0) 

but that way no longer works.. (https://github.com/jquense/yup/issues/136#issuecomment-339235070)

Can someone advise me on how to get yup to require that a string is sent, but to not error on length 0 strings?

Thanks!

Share Improve this question edited Sep 17, 2020 at 18:00 tnrich asked Sep 17, 2020 at 17:54 tnrichtnrich 8,5808 gold badges43 silver badges68 bronze badges 7
  • required() will make the validation false if the string is empty, you will have to remove required(). – Fletcher Rippon Commented Sep 17, 2020 at 17:57
  • @FletcherRipp I still want to throw an error though if the string is simply absent. IMO there is a difference between an empty string and no string at all – tnrich Commented Sep 17, 2020 at 17:59
  • The string() method validates the data type while required() checks if there is anything at all and if a string is empty (""). So wouldn't yup.string() be all the validation you'll want because all you really want to know is check the data type is a String. – Fletcher Rippon Commented Sep 17, 2020 at 18:06
  • @FletcherRipp I think you're incorrect there. See this codesandbox: codesandbox.io/s/priceless-margulis-1zkot?file=/src/index.js I want the first validateSync to throw an error as well.. – tnrich Commented Sep 17, 2020 at 18:32
  • I'm not sure I understand what you want you said you want "the first one" to send an error and it does because undefined !== String from what I think you are asking for you want something that will validate a string of any length including empty ("") but you don't want it to validate undefined or null is that right? – Fletcher Rippon Commented Sep 17, 2020 at 18:45
 |  Show 2 more comments

4 Answers 4

Reset to default 8
yup.string().defined().strict(true);

This will work for undefined values as well.

What I would suggest would be to drop the .required() and, instead, look at using typeError for your validation whilst enabling strict to stop non-string values being coerced and transformed.

This will enable you to allow string values of any length whilst still erroring on values which are not strings.

Example:

yup.string().typeError().strict(true).validateSync(1) // Error
yup.string().typeError().strict(true).validateSync(null) // Error
yup.string().typeError().strict(true).validateSync({}) // Error
yup.string().typeError().strict(true).validateSync("") // Valid
yup.string().typeError().strict(true).validateSync("Foo") // Valid

typeError also has an optional message parameter allowing you to provide the error message there and then if you don't wish to handle it again later.

I like using mixed , test and typeof === 'string':

    firstName: Yup.mixed().test(
        'my test',
        'error: name is not a string',
        (text) => {
            if (typeof text === 'string') {
                return true
            } else {
                return false
            }
        }
    )
Required String that can be Empty

Example Field called: relevance_factor that has the same requirements as yours

let relevance_factor = yup
    .string()
    .test(
        'string-can-be-empty',
        MESSAGES.RELEVANCE_FACTOR_REQUIRED,
        function stringCanBeEmpty(value: any) {
            if (typeof value == 'string')
                return true;

            return false;
        }
    )

本文标签: javascriptHow to get yupstring() to require string of any length (including 0)Stack Overflow