Add a nullable field, its not same as "required" #14421
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
Prerequisites
🚀 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:
Simplificate to:
The text was updated successfully, but these errors were encountered: