Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(schema): allow using a POJO for enum to support TypeScript enums #9547

Merged
merged 6 commits into from
Nov 14, 2020

Conversation

AbdelrahmanHafez
Copy link
Collaborator

fixes #9546 #9535

@vkarpov15 vkarpov15 changed the base branch from master to 5.11 November 14, 2020 19:47
Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work, we'll release this with v5.11 👍

@vkarpov15 vkarpov15 added this to the 5.11 milestone Nov 14, 2020
@vkarpov15 vkarpov15 merged commit ebac35b into Automattic:5.11 Nov 14, 2020
@AbdelrahmanHafez
Copy link
Collaborator Author

Thank you, that works. 👍

@AbdelrahmanHafez AbdelrahmanHafez deleted the gh-9546 branch November 14, 2020 21:40
@joergbaier
Copy link

@AbdelrahmanHafez @vkarpov15 My team began updating our enum values to typescript enums. However there is not a way to allow null in this case. This is somewhat related to the discussion here #3044

enum Color {
  Red = "red",
  Blue = "blue",
  ...
}

const SomeSchema = new Schema({
  color: { type: String, enum: Color },
  ...
});

As far as I can tell, there is no way to allow the color field to be nullable with this syntax. Typescript doesn't allow null in the enum and there is not an option to allow it in mongoose. We would need to use the previous syntax in this case (converting the enum to an array and concatenating null).

It looks like typegoose added a addNullToEnum option to handle this.
typegoose/typegoose#235

Any thoughts on exposing a nullable option? I imagine that allowing null with required: false would not make sense.

@vkarpov15
Copy link
Collaborator

@joergbaier the syntax you put would allow color to be null. The issue you posted says the following:

"Mongoose requires to add null to the enum array to allow an empty value to be used, even when required is false."

That statement is incorrect. You don't need to add null to the enum.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support passing a TypeScript enum to enum validator in schema
3 participants