Skip to content

feat(schematype): support allowNull option to disallow null values even if not required#16237

Open
vkarpov15 wants to merge 2 commits intomasterfrom
vkarpov15/gh-15905
Open

feat(schematype): support allowNull option to disallow null values even if not required#16237
vkarpov15 wants to merge 2 commits intomasterfrom
vkarpov15/gh-15905

Conversation

@vkarpov15
Copy link
Copy Markdown
Collaborator

Fix #15905

Summary

Mongoose has historically used null and undefined interchangeably, but TypeScript sometimes makes working with potentially null values cumbersome, leading to a lot of ?? undefined. This PR adds a allowNull: false option which allows a value to be undefined, but does not allow said value to be set to null, including automatic type inference in TypeScript.

Examples

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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?: boolean schema option and SchemaType#allowNull() API, plus a dedicated validator/error message.
  • Updates TypeScript inference (InferSchemaType / InferRawDocType) to omit | null when allowNull: false is 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.

Comment thread lib/schemaType.js
Copy link
Copy Markdown
Collaborator

@AbdelrahmanHafez AbdelrahmanHafez left a comment

Choose a reason for hiding this comment

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

LGTM 👍

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.

All non-required inferred fields are nullable

4 participants