Commit a9467abb authored by Kulpybaev Ilyas's avatar Kulpybaev Ilyas

Lesson-92

parent c8515eeb
......@@ -20,7 +20,7 @@ export class AuthController {
const user = await this.service.signIn(signInUserDto);
res.send(user);
} catch (e) {
res.send((e as Error).message);
res.status(400).send({ error: { message: (e as Error).message } });
}
};
......@@ -40,7 +40,7 @@ export class AuthController {
res.send(user);
} catch (e) {
if ((e as { code: string }).code === 'ER_DUP_ENTRY') {
res.send({ error: { message: 'User already exists' } });
res.status(400).send({ error: { message: 'User already exists' } });
} else {
res.status(500).send({ error: { message: 'Oops something went wrong' } });
}
......
import { Expose } from 'class-transformer';
import { IsNotEmpty, IsNumberString, IsOptional, IsString } from 'class-validator';
import { IsTitleTV } from '@/validators/isTitleTV';
@Expose()
export class ProductDto {
@IsNotEmpty({ message: 'Продукт не может быть создан без названия!' })
@IsString({ message: 'Название должно быть строкой' })
@IsTitleTV('title', { message: 'The title is TV' })
title!: string;
@IsOptional()
......
......@@ -12,10 +12,10 @@ export class Product {
@Column()
price!: number;
@Column()
@Column({ nullable: true })
image?: string;
@Column()
@Column({ nullable: true })
description!: string;
@ManyToOne(() => Category)
......
import { registerDecorator, ValidationOptions, ValidationArguments } from 'class-validator';
import { ProductDto } from '@/dto/product.dto';
export function IsTitleTV(property: string, validationOptions?: ValidationOptions) {
return function (object: NonNullable<unknown>, propertyName: string) {
registerDecorator({
name: 'isTitleTv',
target: object.constructor,
propertyName: propertyName,
constraints: [property],
options: validationOptions,
validator: {
validate(value: string, args: ValidationArguments) {
const [relatedPropertyName] = args.constraints;
console.log(args.object);
console.log(value);
const relatedValue = (args.object as ProductDto)[relatedPropertyName as keyof ProductDto];
return relatedValue !== 'TV'; // you can return a Promise<boolean> here as well, if you want to make async validation
},
},
});
};
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment