Commit a9467abb authored by Kulpybaev Ilyas's avatar Kulpybaev Ilyas

Lesson-92

parent c8515eeb
...@@ -20,7 +20,7 @@ export class AuthController { ...@@ -20,7 +20,7 @@ export class AuthController {
const user = await this.service.signIn(signInUserDto); const user = await this.service.signIn(signInUserDto);
res.send(user); res.send(user);
} catch (e) { } 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 { ...@@ -40,7 +40,7 @@ export class AuthController {
res.send(user); res.send(user);
} catch (e) { } catch (e) {
if ((e as { code: string }).code === 'ER_DUP_ENTRY') { 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 { } else {
res.status(500).send({ error: { message: 'Oops something went wrong' } }); res.status(500).send({ error: { message: 'Oops something went wrong' } });
} }
......
import { Expose } from 'class-transformer'; import { Expose } from 'class-transformer';
import { IsNotEmpty, IsNumberString, IsOptional, IsString } from 'class-validator'; import { IsNotEmpty, IsNumberString, IsOptional, IsString } from 'class-validator';
import { IsTitleTV } from '@/validators/isTitleTV';
@Expose() @Expose()
export class ProductDto { export class ProductDto {
@IsNotEmpty({ message: 'Продукт не может быть создан без названия!' }) @IsNotEmpty({ message: 'Продукт не может быть создан без названия!' })
@IsString({ message: 'Название должно быть строкой' }) @IsString({ message: 'Название должно быть строкой' })
@IsTitleTV('title', { message: 'The title is TV' })
title!: string; title!: string;
@IsOptional() @IsOptional()
......
...@@ -12,10 +12,10 @@ export class Product { ...@@ -12,10 +12,10 @@ export class Product {
@Column() @Column()
price!: number; price!: number;
@Column() @Column({ nullable: true })
image?: string; image?: string;
@Column() @Column({ nullable: true })
description!: string; description!: string;
@ManyToOne(() => Category) @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