Skip to content

Instantly share code, notes, and snippets.

@uguisu-an
Created May 10, 2022 23:52
Show Gist options
  • Save uguisu-an/5698bedd78fe745c89719e8f04e9714a to your computer and use it in GitHub Desktop.
Save uguisu-an/5698bedd78fe745c89719e8f04e9714a to your computer and use it in GitHub Desktop.
yupで複数の型を扱う例
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