Definitelytyped: @types/mongoose any Types of property 'validate' are incompatible.

Created on 31 May 2019  路  10Comments  路  Source: DefinitelyTyped/DefinitelyTyped

I've been using this style of validation for my mongoose schema:
image
Mongoose Doc

But I get an error message stating any Types of property 'validate' are incompatible. 'message' does not exist in type 'RegExp | [RegExp, string] | ValidateOpts | ...lots more)[]'.

Please test your code before publishing it!

Most helpful comment

After being stuck a moment I realized I had to use msg and not message.

        validate: {
            validator: yourAsyncValidator,
            msg: "Your message",
        },

That's understandable from the fact that the validate option is waiting for an AsyncPromiseValidationOpts which is defined as:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/7f8f7853566ad7d73e0e7c8ffafb463c3fa72313/types/mongoose/index.d.ts#L1219-L1221
And as you can see AsyncPromiseValidationOpts don't have any message property BUT AsyncPromiseValidationOptsextends ValidateOptsBase wich have a msg property
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/7f8f7853566ad7d73e0e7c8ffafb463c3fa72313/types/mongoose/index.d.ts#L1198-L1201
Suggesting a PR to add message property to AsyncPromiseValidationOpts could solve this issue as well.

All 10 comments

Same for me, any update on this?

~I think @types/mongoose isn't compatible (yet) for Mongoose v5, isn't it?~

~Folder https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/mongoose only has v3 and v4. The link that you gave is Mongoose v5. That might be the reason.~

EDIT: my bad. the v5.5.1 type definitions are in the root folder. Probably you should mention the author of it so they are aware of this issue.

@Alorel I think you're the owner?

There are many contributors to mongoose typings: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/mongoose/index.d.ts#L3

I do not have the capacity to make any changes at the moment.

Maybe it's a TypeScript issue, because the latest version of types (i.e. v5.5.6) works on TypeScript v3.4.5, but doesn't work on TypeScript v3.5.1.

I encountered this issue as well.

someone?

Here is how I fixed the issue on my end.

I created a custom typings file for my project and added this to it.
````typescript
declare module "mongoose" {
namespace SchemaTypeOpts {
interface ValidateOpts {
validator?: RegExp | ValidateFn | undefined;
message?: IMessageFn | string;
type?: string;
[key: string]: any;
}
}
}

interface IMessageFn {
(properties: {[key: string]: any}): string;
}
````

Here is an example of my Schema.

typescript const userSchema = new Schema({ email: { type: String, validate: { validator: function(v: string) { return false; }, message: 'It now works like it should', 'you can also set': 'other properties' }, required: [true, 'Email required'] }, })

And here is what the validation error looks like. Notice how the other properties come through. This might help if you want some other context along with the error message.

typescript MongooseError [ValidatorError]: It now works like it should at new ValidatorError (/root/workspace/my-project/node_modules/mongoose/lib/error/validator.js:29:11) at validate (/root/workspace/my-project/node_modules/mongoose/lib/schematype.js:978:13) at /root/workspace/my-project/node_modules/mongoose/lib/schematype.js:1031:11 at Array.forEach (<anonymous>) at SchemaString.SchemaType.doValidate (/root/workspace/my-project/node_modules/mongoose/lib/schematype.js:987:14) at /root/workspace/my-project/node_modules/mongoose/lib/document.js:2102:9 at processTicksAndRejections (internal/process/task_queues.js:82:9) { message: 'It now works like it should', name: 'ValidatorError', properties: { validator: [Function: validator], message: 'It now works like it should', 'you can also set': 'other properties', type: 'user defined', path: 'email', value: '[email protected]' }, kind: 'user defined', path: 'email', value: '[email protected]', reason: undefined, }

After being stuck a moment I realized I had to use msg and not message.

        validate: {
            validator: yourAsyncValidator,
            msg: "Your message",
        },

That's understandable from the fact that the validate option is waiting for an AsyncPromiseValidationOpts which is defined as:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/7f8f7853566ad7d73e0e7c8ffafb463c3fa72313/types/mongoose/index.d.ts#L1219-L1221
And as you can see AsyncPromiseValidationOpts don't have any message property BUT AsyncPromiseValidationOptsextends ValidateOptsBase wich have a msg property
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/7f8f7853566ad7d73e0e7c8ffafb463c3fa72313/types/mongoose/index.d.ts#L1198-L1201
Suggesting a PR to add message property to AsyncPromiseValidationOpts could solve this issue as well.

Any update on this ?

Was this page helpful?
0 / 5 - 0 ratings