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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a nullable field, its not same as "required" #14421

Open
2 tasks done
yhojann-cl opened this issue Mar 7, 2024 · 1 comment
Open
2 tasks done

Add a nullable field, its not same as "required" #14421

yhojann-cl opened this issue Mar 7, 2024 · 1 comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class

Comments

@yhojann-cl
Copy link

yhojann-cl commented Mar 7, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

馃殌 Feature Proposal

The "required" parameter of a model in mongoose defines whether the document attribute is required, but in turn it plays a double role, if it is required and if the value is null.

I think that both options should be defined separately since, for example, I can not require said field and have a value of type "undefined" and I can also require the field as mandatory but it can be null (that is, it must be defined but not necessarily with a value). Json schema works in a similar way since the "required" parameter only works to define whether it is required or not, content validation is done separately.

It would be less difficult to work with if mongoose had these two parameters separately, one called "required" and another "nullable", you could even have another one that says "canEmpty" that indicates whether an array, object or string can be empty and avoid having to perform validations by functions and regular expressions.

Currently, according to the official documentation, this can be done through custom functions, but I think that all of us could avoid those functions if those parameters existed and include others from json schema like as "pattern".

Motivation

Avoid using functions in each validation of null, empty and undefined data in each model to be created.

Example

From:

module.exports = mongoose.model('user',
    new mongoose.Schema({
        lastName: {
            type: String,
            required: true,
            validate: val => ((val === null) || !!val.match(/^[a-zA-Z]{1,12}$/))
        },
    }));

Simplificate to:

module.exports = mongoose.model('user',
    new mongoose.Schema({
        lastName: {
            type: String,
            required: true,
            nullable: true,
            pattern: /^[a-zA-Z]{1,12}$/,
        },
    }));
@yhojann-cl yhojann-cl added enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class labels Mar 7, 2024
@vkarpov15
Copy link
Collaborator

Mongoose generally treats null and undefined as interchangeable, because undefined values end up stored as null in MongoDB anyway.

I can see the benefit of supporting something like required: 'nullable' to have a field be required, but allow null values. However, how should that handle undefined values? Like new User({ lastName: undefined })?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class
Projects
None yet
Development

No branches or pull requests

2 participants