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

Typescript: Asynchronous custom validation with error message #8171

Closed
lambou opened this issue Sep 16, 2019 · 1 comment
Closed

Typescript: Asynchronous custom validation with error message #8171

lambou opened this issue Sep 16, 2019 · 1 comment

Comments

@lambou
Copy link

lambou commented Sep 16, 2019

The current validate type in @types/mongoose/index.d.ts is:

validate?: RegExp | [RegExp, string] |
    SchemaTypeOpts.ValidateFn<T> | [SchemaTypeOpts.ValidateFn<T>, string] |
    SchemaTypeOpts.ValidateOpts | SchemaTypeOpts.AsyncValidateOpts |
    SchemaTypeOpts.AsyncPromiseValidationFn<T> | SchemaTypeOpts.AsyncPromiseValidationOpts |
    (SchemaTypeOpts.ValidateOpts | SchemaTypeOpts.AsyncValidateOpts |
      SchemaTypeOpts.AsyncPromiseValidationFn<T> | SchemaTypeOpts.AsyncPromiseValidationOpts)[];

Here is the definition where you can give the validation message:
[SchemaTypeOpts.ValidateFn, string] it is synchronous only

can you add somthing like [SchemaTypeOpts.AsyncPromiseValidationFn, string]

so it will be possible to write (using async):

const TestSchema = new Schema({  
    user : {
        type: Schema.Types.ObjectId,
        ref: 'User',
        validate: [
            async function(value){
                return UserSchema.findOne({_id: value}) != null;
            },
            `User doesn't exist.`
        ]
    }
}

Thanks in advance

@lambou lambou changed the title Asynchronous custom validation with typescript Typescript: Asynchronous custom validation with error message Sep 16, 2019
@hasezoey
Copy link
Collaborator

mongoose currently dosnt make offical typescript types, you should open a new issue in https://github.com/DefinitelyTyped/DefinitelyTyped/issues

reference: #8108

@Automattic Automattic locked as off-topic and limited conversation to collaborators Sep 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants