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 | Show 2 more comments4 Answers
Reset to default 8yup.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
版权声明:本文标题:javascript - How to get yup.string() to require string of any length (including 0) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739302892a2157221.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
required()
will make the validation false if the string is empty, you will have to removerequired()
. – Fletcher Rippon Commented Sep 17, 2020 at 17:57string()
method validates the data type whilerequired()
checks if there is anything at all and if a string is empty (""
). So wouldn'tyup.string()
be all the validation you'll want because all you really want to know is check the data type is aString
. – Fletcher Rippon Commented Sep 17, 2020 at 18:06undefined !== 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 validateundefined
ornull
is that right? – Fletcher Rippon Commented Sep 17, 2020 at 18:45