Commit db7d5af1 authored by Рахметова Альбина's avatar Рахметова Альбина

Merge branch '#22' into 'dev'

реализавана валидация на запросы

See merge request !23
parents 0701dfb0 03dd82c3
......@@ -44,7 +44,7 @@ export class PostsController {
private addPost = async (req: Request, res: Response): Promise<void> => {
const post = req.body
post.image = req.file ? req.file.filename : ''
post.image = req.file ? req.file.filename : req.body.image
const response = await this.service.addPost(post)
res.send(response)
}
......
......@@ -45,7 +45,17 @@ export class Mongo implements IDataBase {
}
public getPostById = async (id: string): Promise<IResponse<IPost | undefined>> => {
try {
if (!id || !id.trim() || !mongoose.Types.ObjectId.isValid(id)) {
const response: IResponse<undefined> = {
status: EStatuses.NOT_OK,
result: undefined,
message: `Invalid post id: ${id}`
};
return response;
}
const data = await Post.findById(id)
const response: IResponse<IPost> = {
status: EStatuses.OK,
result: data as any,
......@@ -84,6 +94,14 @@ export class Mongo implements IDataBase {
}
public deletePostById = async (id: string): Promise<IResponse<IPost | undefined>> => {
try {
if (!id || !id.trim() || !mongoose.Types.ObjectId.isValid(id)) {
const response: IResponse<undefined> = {
status: EStatuses.NOT_OK,
result: undefined,
message: `Invalid item id: ${id}`
};
return response;
}
const data = await Post.findOneAndDelete({ _id: id })
const response: IResponse<IPost> = {
status: EStatuses.OK,
......
import { object, string, TypeOf, } from 'zod';
export const PostSchema = object({
body: object({
title: string({
required_error: 'Title is required',
}),
description: string().optional(),
image: string().optional(),
}).refine((data) => {
if (!data.description || !data.image) {
throw new Error('At least one of description or image is required');
}
if (data.description && data.image) {
return {
description: data.description,
image: data.image,
};
}
return true;
})
})
export type PostInput = TypeOf<typeof PostSchema>['body'];
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