0

I have this schema:

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 }),
});

I have this data object:

{
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"
}

And I want to cast this object but the casting does not strip out the extra fields that are not defined in the schema which are addresses, updated_at and created_at. How can I do this automatically without specifically stripping out those field?

Sami Al-Subhi
  • 4,406
  • 9
  • 39
  • 66

1 Answers1

1

I found the solution:

schema.cast(dataObject, { stripUnknown: true})
Sami Al-Subhi
  • 4,406
  • 9
  • 39
  • 66