仅投射模式中定义的内容并删除额外的字段 - 是的
Cast only what is defined in the schema and strip out extra fields - Yup
我有这个架构:
yup.object().shape({
name_first: yup.string().required().label("Name First").meta({ initial: "" }),
name_last: yup.string().required().label("Name Last").meta({ initial: "" }),
mobile: yup
.number()
.typeError("must be a number")
.integer()
.positive()
.test("is-mobile", "${path} is not a valid mobile number", (value, context) => {
var strValue = value.toString();
if (strValue.length !== 8 || strValue.charAt(0) !== "9" || strValue.charAt(0) !== "7") {
return false;
} else {
return true;
}
})
.label("Mobile")
.meta({ initial: "" }),
email: yup.string().email().notRequired().label("Email address").meta({ initial: "" }),
gender: yup.mixed().oneOf(["m", "f"]).required().label("Gender").meta({ initial: "" }),
birth_date: yup.date().required().min(new Date(1900, 0, 1)).label("Birth Date").meta({ initial: null }),
preferred_language: yup.mixed().oneOf(["ar", "en"]).required().label("Preferred Language").meta({ initial: "" }),
newsletter: yup.boolean().required().label("Subscribe to Newsletter").meta({ initial: true }),
disabled: yup.boolean().required().label("Disabled").meta({ initial: false }),
});
我有这个数据对象:
{
addresses: (75) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
birth_date: Wed Sep 23 2020 00:00:00 GMT+0400 (Gulf Standard Time) {}
created_at: "2020-09-23T13:57:24.156706"
disabled: false
email: "user@example.com"
email_confirmed: false
gender: "m"
id: 1
mobile: 99009900
mobile_confirmed: true
name_first: "string"
name_last: "string"
newsletter: true
preferred_language: "ar"
updated_at: "2020-09-23T13:57:24.156713"
}
而且我想转换此对象,但转换不会删除架构中未定义的额外字段,即 addresses
、updated_at
和 created_at
。
我怎样才能自动执行此操作而不专门删除这些字段?
我找到了解决方案:
schema.cast(dataObject, { stripUnknown: true})
我有这个架构:
yup.object().shape({
name_first: yup.string().required().label("Name First").meta({ initial: "" }),
name_last: yup.string().required().label("Name Last").meta({ initial: "" }),
mobile: yup
.number()
.typeError("must be a number")
.integer()
.positive()
.test("is-mobile", "${path} is not a valid mobile number", (value, context) => {
var strValue = value.toString();
if (strValue.length !== 8 || strValue.charAt(0) !== "9" || strValue.charAt(0) !== "7") {
return false;
} else {
return true;
}
})
.label("Mobile")
.meta({ initial: "" }),
email: yup.string().email().notRequired().label("Email address").meta({ initial: "" }),
gender: yup.mixed().oneOf(["m", "f"]).required().label("Gender").meta({ initial: "" }),
birth_date: yup.date().required().min(new Date(1900, 0, 1)).label("Birth Date").meta({ initial: null }),
preferred_language: yup.mixed().oneOf(["ar", "en"]).required().label("Preferred Language").meta({ initial: "" }),
newsletter: yup.boolean().required().label("Subscribe to Newsletter").meta({ initial: true }),
disabled: yup.boolean().required().label("Disabled").meta({ initial: false }),
});
我有这个数据对象:
{
addresses: (75) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
birth_date: Wed Sep 23 2020 00:00:00 GMT+0400 (Gulf Standard Time) {}
created_at: "2020-09-23T13:57:24.156706"
disabled: false
email: "user@example.com"
email_confirmed: false
gender: "m"
id: 1
mobile: 99009900
mobile_confirmed: true
name_first: "string"
name_last: "string"
newsletter: true
preferred_language: "ar"
updated_at: "2020-09-23T13:57:24.156713"
}
而且我想转换此对象,但转换不会删除架构中未定义的额外字段,即 addresses
、updated_at
和 created_at
。
我怎样才能自动执行此操作而不专门删除这些字段?
我找到了解决方案:
schema.cast(dataObject, { stripUnknown: true})