1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

yupでenumのバリデーションをする

Posted at

やりたいこと

enumのバリデーション

ここで紹介されている。
https://github.com/jquense/yup/issues/1013#issuecomment-679113073

コード

import * as yup  from "yup";

enum Status {
    Success = "Success",
    Failure = "Failure"
}
const schema = yup.object({
    status: yup.mixed<Status>().oneOf(Object.values(Status)).required(),
});

説明

mixed<Status>()で型を指定しoneOf(Object.values(Status))で取りうる値を指定する。

mixed<Status>()でバリデーション後の値の型がStatusになる。
mixed<Status>()だけだと各列挙子以外の文字列も有効判定になってしまうためoneOf(Object.values(Status))で列挙子を指定する。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?