-
Notifications
You must be signed in to change notification settings - Fork 141
[Question] How can i get the id sent on mutation inside my custom validator #1503
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
Comments
Thanks for your time @impcyber, but I've tried this way and it didn't work. Using default As I still don't quite understand how nestjs-query works with the class-validator, I'm overriding the service class to do this validation. |
I think you should use Here is an example: https://github.com/doug-martin/nestjs-query/blob/master/examples/hooks/src/hooks.ts#L25 And there is from my code: import {
BeforeCreateOneHook,
CreateOneInputType,
OperationGroup,
} from '@nestjs-query/query-graphql'
import { BadRequestException, Injectable } from '@nestjs/common'
import { ReturnModelType } from '@typegoose/typegoose'
import assert from 'assert'
import { AccessService } from 'nest-casl'
import { InjectModel } from 'nestjs-typegoose'
import { AccountInputDTO } from '../dto/account'
import { AccountEntity } from '../entities'
import { IAccountContext } from './../interfaces/account-payload.interface'
@Injectable()
export class AccountCreateHook<T>
implements BeforeCreateOneHook<T, IAccountContext>
{
public constructor(
@InjectModel(AccountEntity)
private readonly accountModel: ReturnModelType<typeof AccountEntity>,
private readonly accessService: AccessService,
) {}
public async run(
instance: CreateOneInputType<T>,
context: IAccountContext,
): Promise<CreateOneInputType<T>> {
const { account } = context.req
return this.accountModel
.exists({
email: (instance.input as unknown as AccountInputDTO).email,
})
.then((exists): CreateOneInputType<T> => {
assert(!exists, new Error())
const granted = this.accessService.hasAbility(
account,
OperationGroup.CREATE,
instance.input,
)
assert(granted, new Error())
return instance
})
.catch(() => {
throw new BadRequestException()
})
}
} |
This is a very interesting solution, I hadn't thought of using hooks for this. I'm sure it's going to be better than my solution. Thank you for your help, @impcyber. |
In case anyone get here during search, inside |
BTW: The problem I'm facing now is for |
📚 Documentation
I'm using graphql and i need to create a validation for unique fields in the db, for example, the user's email.
When registering a new user, I just see if the email already exists in the database. The problem is in the user update, I need to know how to get the id sent in mutation to ignore it in the query.
I'm using the class-validator as in the example below:
Mutation to update user
DTO's
Custom validator
Console output:
How can i get the id sent on mutation inside my custom validator?
The text was updated successfully, but these errors were encountered: