feat(schematype): support allowNull option to disallow null values even if not required#16237
Open
feat(schematype): support allowNull option to disallow null values even if not required#16237
allowNull option to disallow null values even if not required#16237Conversation
…even if not `required` Fix #15905
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a new allowNull schema path option to let users disallow null on optional (non-required) paths, with corresponding runtime validation behavior and TypeScript inference updates.
Changes:
- Introduces
allowNull?: booleanschema option andSchemaType#allowNull()API, plus a dedicated validator/error message. - Updates TypeScript inference (
InferSchemaType/InferRawDocType) to omit| nullwhenallowNull: falseis specified. - Extends JSON Schema generation and adds runtime + type-level tests covering validation, cloning, updates, discriminators, and JSON schema output.
Reviewed changes
Copilot reviewed 10 out of 13 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| types/schematypes.d.ts | Adds allowNull?: boolean option and allowNull() method typing. |
| types/inferschematype.d.ts | Adds PathAllowsNull helper and uses it to conditionally include null in inferred optional paths. |
| types/inferrawdoctype.d.ts | Applies PathAllowsNull to raw doc inference for optional paths. |
| lib/schemaType.js | Implements SchemaType#allowNull(), required/allowNull interaction checks, JSON schema nullability behavior, and cloning support. |
| lib/options/schemaTypeOptions.js | Documents and exposes allowNull as a schema type option. |
| lib/error/messages.js | Adds the default error message for the allowNull validator. |
| test/types/schema.test.ts | Adds type-level tests for allowNull: false inference. |
| test/types/schema.create.test.ts | Adds type-level tests for Schema.create() + allowNull: false. |
| test/schema.validation.test.js | Adds validation behavior tests for allowNull, including interaction with required. |
| test/schema.test.js | Adds clone and JSON schema tests for allowNull. |
| test/model.updateOne.test.js | Ensures allowNull validator runs with updateOne() + runValidators. |
| test/model.findOneAndUpdate.test.js | Ensures allowNull validator runs with findOneAndUpdate() + runValidators. |
| test/model.discriminator.test.js | Tests discriminator overrides involving allowNull and required. |
hasezoey
approved these changes
Apr 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix #15905
Summary
Mongoose has historically used
nullandundefinedinterchangeably, but TypeScript sometimes makes working with potentiallynullvalues cumbersome, leading to a lot of?? undefined. This PR adds aallowNull: falseoption which allows a value to beundefined, but does not allow said value to be set tonull, including automatic type inference in TypeScript.Examples