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

Improve ValidateOpts type #12606

Merged
merged 2 commits into from Oct 28, 2022
Merged

Improve ValidateOpts type #12606

merged 2 commits into from Oct 28, 2022

Conversation

Freezystem
Copy link
Contributor

@Freezystem Freezystem commented Oct 27, 2022

Summary

As the documention mentions it, the propsParameter property does not exists on the ValidateOpts<T> type and cause TypeScript to generate an error.

Examples

I have created a validator to check if the ObjectId references existing mongoDB collection document.

export const refExists: ValidateOpts<string> = {
  async validator(_id, props): Promise<boolean> {
    const self = this.constructor as Model<any>;
    const {ref} = self.schema.paths[props.path].options;
    const model = models[ref];
    
    return await model.exists({_id}).exec() !== null;
  },
  propsParameter: true,
};

This snippet generate two TypeScript errors:

error TS2322: Type '{ validator(_id: string, props: (result: boolean) => void): Promise; propsParameter: boolean; }' is not assignable to type 'ValidateOpts'.
Object literal may only specify known properties, and 'propsParameter' does not exist in type 'ValidateOpts'.

Here, the compiler is thinking we are using LegacyAsyncValidateFn<T> because it's the only one validator to have two parameters.

and

error TS2322: Type '{ validator(_id: any, opts: any): Promise; propsParameter: boolean; }' is not assignable to type 'ValidateOpts'.
Object literal may only specify known properties, and 'propsParameter' does not exist in type 'ValidateOpts'.

Here, the compiler is saying that propsParameter is not a valid property on that type.

As the documention mentions [it](https://mongoosejs.com/docs/api.html#schematype_SchemaType-validate), the `propsParameter` property does not exists on the `ValidateOpts<T>` type and cause TypeScript to generate an error.
Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

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

Thanks 👍

@vkarpov15 vkarpov15 added this to the 6.7.1 milestone Oct 28, 2022
@vkarpov15 vkarpov15 merged commit e693249 into Automattic:master Oct 28, 2022
@Freezystem Freezystem deleted the patch-1 branch October 28, 2022 21:11
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.

None yet

2 participants