yupで複合 validation 〜春のeslint-disable-next-lineを添えて〜
4491 ワード
TL;DR
「2つののフィールドを切り替え可能で、いずれか一つが必須」みたいなやつをyupでやりたかったけど、地味にハマった。
結論としては
-
test
を使う - thisが必要なのでarrow functionをあきらめる
- eslint のこのrule をdisableする
実装例
フィールド id
/ email
のどちらかが必須というのをやりたい場合
// イメージが湧く程度の適当なjsx
/*
Props
loginWithId: boolean ... IDとメールアドレスのどちらを表示するかを保持するProps
toggleLoginKey: () => void ... IDとメールアドレスのどちらを表示するかを切り替える関数
*/
{loginWithId && <p><input type="text" name="id" value="" /></p>}
{loginWithId && <p><input type="text" name="email" value="" /></p>}
<button onClick={toggleLoginKey}>
yup.object().shape({
id: yup
.string()
// eslint-disable-next-line func-names
.test('email', 'ログインIDを入力してください', function(value) {
return this.parent.email || value
}),
email: yup
.string()
.email('メールアドレス形式で入力してください')
// eslint-disable-next-line func-names
.test('email', 'メールアドレスを入力してください', function(value) {
return this.parent.id || value
}),
})
見えなくなったFiledの値はToggleする関数内でemptyにしておく。
Author And Source
この問題について(yupで複合 validation 〜春のeslint-disable-next-lineを添えて〜), 我々は、より多くの情報をここで見つけました https://qiita.com/shoota/items/1a889c98bcfb5880a01e著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .