Created
May 10, 2022 23:52
-
-
Save uguisu-an/5698bedd78fe745c89719e8f04e9714a to your computer and use it in GitHub Desktop.
yupで複数の型を扱う例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as Yup from "yup"; | |
test("複数のタイプに対応する", () => { | |
const schema = Yup.object({ | |
content: Yup.lazy((content) => | |
Yup.object({ | |
type: Yup.string().oneOf(["text", "textarea", "file"]).required(), | |
value: | |
content.type === "file" | |
? Yup.object({ | |
data: Yup.string().required(), | |
filename: Yup.string().required(), | |
mimetype: Yup.string().required(), | |
}) | |
: Yup.array(Yup.string()), | |
}) | |
), | |
}); | |
interface Example extends Yup.InferType<typeof schema> {} | |
const validatedData: Example = schema.validateSync({ | |
content: { | |
type: "text", | |
value: [""], | |
}, | |
}); | |
console.log(validatedData); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment