TypeGraphql-@inputtype在TypeForm上
您好,我需要检查数据库中是否已经有电子邮件: 为此:TypeGraphql-@inputtype在TypeForm上,graphql,typeorm,typegraphql,Graphql,Typeorm,Typegraphql,您好,我需要检查数据库中是否已经有电子邮件: 为此: return User.findOne({ where: { email } }).then((user) => { if (user) return false; return true; }); 我有以下输入类型: @InputType() export class RegisterInput { @Field() @IsEmail({}, { message: 'Invalid email' }) email
return User.findOne({ where: { email } }).then((user) => {
if (user) return false;
return true;
});
我有以下输入类型:
@InputType()
export class RegisterInput {
@Field()
@IsEmail({}, { message: 'Invalid email' })
email: string;
@Field()
@Length(1, 255)
name: string;
@Field()
password: string;
}
我想知道是否有任何方法让我验证输入类型中的电子邮件?或者我下定决心:
@Mutation(() => User)
async register(
@Arg('data')
{ email, name, password }: RegisterInput,
): Promise<User> {
const hashedPassword = await bcrypt.hash(password, 12);
const user = await User.create({
email,
name,
password: hashedPassword,
}).save();
return user;
}
@变异(()=>用户)
异步寄存器(
@Arg('数据')
{电子邮件、名称、密码}:RegisterInput,
):承诺{
const hashedPassword=await bcrypt.hash(密码,12);
const user=wait user.create({
电子邮件,
名称
密码:hashedPassword,
}).save();
返回用户;
}
事实上,我刚刚为自己的项目想出了这个办法
您只需从RegisterInput
参数在电子邮件上添加验证,如果电子邮件已经存在,则抛出错误
import { Repository } from 'typeorm'
import { InjectRepository } from 'typeorm-typedi-extensions'
...
// Use dependency injection in the resolver's constructor
constructor(
@InjectRepository(User) private readonly userRepository: Repository<User>
) {}
...
// Your mutation
@Mutation(() => User)
async register(
@Arg('data')
{ email, name, password }: RegisterInput,
): Promise<User> {
const hashedPassword = await bcrypt.hash(password, 12);
const userWithEmail = this.userRepository.find({ email: email })
// If a user with the email was found
if (userWithEmail) {
throw new Error('A user with that email already exists!')
}
const user = await User.create({
email,
name,
password: hashedPassword,
}).save();
return user;
}
让我知道这是否适合你?谢谢 实际上,您可以为
类验证器注册自己的decorator
例如,它可以看起来像这样:
IsemailReadyExists.ts
import {
registerDecorator,
ValidationOptions,
ValidatorConstraint,
ValidatorConstraintInterface,
} from 'class-validator';
import { UserRepo } from '../../repositories/UserRepo';
import { InjectRepository } from 'typeorm-typedi-extensions';
@ValidatorConstraint({ async: true })
export class isEmailAlreadyExist
implements ValidatorConstraintInterface {
@InjectRepository()
private readonly userRepo: UserRepo;
async validate(email: string) {
const user = await this.userRepo.findOne({ where: { email } });
if (user) return false;
return true;
}
}
export function IsEmailAlreadyExist(validationOptions?: ValidationOptions) {
return function (object: Object, propertyName: string) {
registerDecorator({
target: object.constructor,
propertyName: propertyName,
options: validationOptions,
constraints: [],
validator: isEmailAlreadyExist,
});
};
}
如果您正在注入依赖项,那么也应该在类验证器中注入依赖项。只需将以下内容添加到主文件:
import { Container } from 'typedi';
import * as classValidator from 'class-validator';
classValidator.useContainer(Container);
...
const schema = await buildSchema({
resolvers: [...],
container: Container,
});
然后您可以在输入类型中使用decorator
import { InputType, Field } from 'type-graphql';
import { IsEmailAlreadyExist } from '../../../utils/validators/isEmailAlreadyExist';
@InputType()
export class YourInput {
@Field()
@IsEmailAlreadyExist()
email: string;
}
import { InputType, Field } from 'type-graphql';
import { IsEmailAlreadyExist } from '../../../utils/validators/isEmailAlreadyExist';
@InputType()
export class YourInput {
@Field()
@IsEmailAlreadyExist()
email: string;
}