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

Make path in keyof T required in SchemaDefinition if T is supplied #14872

Open
2 tasks done
Grsz opened this issue Sep 7, 2024 · 1 comment
Open
2 tasks done

Make path in keyof T required in SchemaDefinition if T is supplied #14872

Grsz opened this issue Sep 7, 2024 · 1 comment
Labels
discussion If you have any thoughts or comments on this issue, please share them! 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

@Grsz
Copy link

Grsz commented Sep 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

In

export type SchemaDefinition<T = undefined, EnforcedDocType = any> = T extends undefined
    ? { [path: string]: SchemaDefinitionProperty; }
    : { [path in keyof T]?: SchemaDefinitionProperty<T[path], EnforcedDocType>; };

instead of just forcing the possible keys (as path in keyof T), let them be required (remove ? at [path in keyof T]?:), or at least provide an option to toggle it being strict or not.

Motivation

At the moment when defining a Schema, and supplying the first generic to it (RawDocType), it doesn't complain if I miss some keys from the defined type, which is not ideal.

Example

type Item = {
  _id: string;
  name: string;
}

// complains as expected because foo is not in Item
new Schema<Item>({
  _id: String,
  foo: Number
})

// doesn't complain as expected because schema matches type
new Schema<Item>({
  _id: String,
  name: String
})

// doesn't complain, but it should because required property name is not defined
new Schema<Item>({
  _id: String,
})
@Grsz Grsz 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 Sep 7, 2024
@vkarpov15
Copy link
Collaborator

This is expected behavior because name can be added via a plugin or Schema.prototype.add() later.

The big issue with making this change would be timestamps:

type Item = {
  _id: string;
  createdAt: Date;
  updatedAt: Date
}

// Valid, schema gets `createdAt` and `updatedAt` properties from `timestamps`
new Schema<Item>({
  _id: String
}, { timestamps: true })

Any suggestions for how to make this work better with TypeScript?

@vkarpov15 vkarpov15 added the discussion If you have any thoughts or comments on this issue, please share them! label Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion If you have any thoughts or comments on this issue, please share them! 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