Skip to content

Models should follow the schema (Please god I want this for my sanity) #10343

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

Closed
wants to merge 1 commit into from

Conversation

ShadiestGoat
Copy link
Contributor

Summary

See, when I create a model, there is never a time where I ever have to create a model for a schema that isn't defined. This is basically to make it so that you NEED a proper document for the proper schema.
Thats it

Examples

new MyModel({must be a schema})

@ShadiestGoat
Copy link
Contributor Author

Any checks that weren't passed are irrelevant

@ShadiestGoat ShadiestGoat changed the title Please god I want this for my sanity Models should follow the schema (Please god I want this for my sanity) Jun 11, 2021
@vkarpov15
Copy link
Collaborator

vkarpov15 commented Jun 13, 2021

There's no way we're going to merge this unfortunately, it is perfectly valid to pass any object to a model constructor. Changing that would be a massive backwards breaking change and be completely opposed to how Mongoose is meant to be used.

Why is this change good for your sanity?

@vkarpov15 vkarpov15 closed this Jun 13, 2021
@ShadiestGoat
Copy link
Contributor Author

The reason for this is for autocomplete, mostly. I mean what'd be the point of typescript here if you its just any?

Wouldn't it be better then to just set the default type to be any, rather than having it or any? Cause putting the type to be X | any means it'd just be any, meaning typing is useless there.
This basically sets it be any by default, but if a schema is present, only the schema will be passed through. So, if you really wanted to pass any object through, then you could still do that, just don't define the type to be that schema...

@ShadiestGoat
Copy link
Contributor Author

Imo this should be reconsidered

@vkarpov15
Copy link
Collaborator

How about this instead?

new(doc?: T): EnforceDocument<T, TMethods>;
new(doc?: any): EnforceDocument<T, TMethods>;

That might help.

@ShadiestGoat
Copy link
Contributor Author

That the exact same thing?
T is any by default, so if you want to pass anything through there, just don't define T to be anything:

//explicitly state it's of type any
const mmodel:Model<any> = model('Foo', Bar)
//any by default (assuming SchemaDefinition is empty)
const mmodel2 = model('Foo2', Bar2)

//or, you could just ask ts to ignore
const mmodel:Model<FooBar> = model('Foo', Bar)
//@ts-ignore
new mmodel({wrongType: "Barrr"})

@vkarpov15
Copy link
Collaborator

I think it might be different for VS code autocomplete, because it would show autocomplete for T first. But if that doesn't help, there's nothing we can do here because Mongoose is meant to validate potentially untrusted data coming in to the Model() constructor, so it needs to accept any one way or another.

vkarpov15 added a commit that referenced this pull request Jul 28, 2021
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.

2 participants