Skip to content

Union schema types: paths that can be one of multiple types #10894

@israelKusayev

Description

@israelKusayev

Do you want to request a feature or report a bug?

What is the current behavior?

I have a birthday field that the client send a string like "03/02/2000" and I convert it via set to a Date
Is it possible to define two types the first one String (the first validation by mongoose to check if it's a string) and a second type of Date (when I read it from the DB)

birthday: {
    type: SchemaTypes.Date
    set: (value: string) => {
        return value ? startOfDay(parse(value, 'DD/MM/YYYY', new Date())).toISOString() : null;
    }
}

What is the expected behavior?

Right now I get the following error
mongoose validation error Cast to Date failed for value "03/02/2000" (type string) at path "birthday"

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Currently I use the "latest version" 6.0.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    new featureThis change adds new functionality, like a new method or class

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions