是的图像验证,仅当图像已上传
Yup image validation, only if image is uploaded
我正在尝试为特色图片编写验证。此字段也可以为空,因此我希望此字段作为图片验证,仅当图片已上传时。
const schema = Yup.object({
featured_image: Yup.mixed().when("featured_image", {
is: (value) => value?.length,
then: (schema) =>
schema
.test("name", "Image is required", (value) => {
return (
value != undefined &&
value[0] &&
value[0].name !== ""
);
})
.test("fileSize", "File must be less than 2MB", (value) => {
return (
value != undefined &&
value[0] &&
value[0].size <= 2000000
);
})
.test("type", "Only images are supported", (value) => {
return (
value != undefined &&
value[0] &&
value[0].type.includes("image")
);
}),
otherwise: (schema) => schema.nullable(),
}),
});
目前,它给出的错误是:Error: Cyclic dependency, node was:"featured_image"
循环错误与您的必填或非必填字段无关,您需要将 'featured-image' 添加到依赖项列表,试试这个:
const validationSchema = yup.object().shape({
//your scheme here
},
//cyclic dependencies
[
['featured_image', 'featured_image'],
]
)
我正在尝试为特色图片编写验证。此字段也可以为空,因此我希望此字段作为图片验证,仅当图片已上传时。
const schema = Yup.object({
featured_image: Yup.mixed().when("featured_image", {
is: (value) => value?.length,
then: (schema) =>
schema
.test("name", "Image is required", (value) => {
return (
value != undefined &&
value[0] &&
value[0].name !== ""
);
})
.test("fileSize", "File must be less than 2MB", (value) => {
return (
value != undefined &&
value[0] &&
value[0].size <= 2000000
);
})
.test("type", "Only images are supported", (value) => {
return (
value != undefined &&
value[0] &&
value[0].type.includes("image")
);
}),
otherwise: (schema) => schema.nullable(),
}),
});
目前,它给出的错误是:Error: Cyclic dependency, node was:"featured_image"
循环错误与您的必填或非必填字段无关,您需要将 'featured-image' 添加到依赖项列表,试试这个:
const validationSchema = yup.object().shape({
//your scheme here
},
//cyclic dependencies
[
['featured_image', 'featured_image'],
]
)