-
Notifications
You must be signed in to change notification settings - Fork 815
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validation for author and book data
- Loading branch information
Showing
9 changed files
with
88 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
import { MemoryAuthor } from '../models/Author/MemoryAuthor.js' | ||
import { validateAuthor } from '../utils/authorSchema.js' | ||
|
||
export const getAll = (req, res) => { | ||
const authors = MemoryAuthor.getAll() | ||
res.status(200).json(authors) | ||
} | ||
|
||
export const create = (req, res) => { | ||
const author = MemoryAuthor.create(req.body) | ||
const result = validateAuthor(req.body) | ||
|
||
if (!result.success) { | ||
const error = JSON.parse(result.error.message) | ||
return res.status(400).json({ error }) | ||
} | ||
|
||
const author = MemoryAuthor.create(result.data) | ||
res.status(201).json(author) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import z from 'zod' | ||
|
||
const authorSchema = z.object({ | ||
name: z.string({ | ||
required_error: 'Name is required', | ||
invalid_type_error: 'Name must be a string' | ||
}).min(1).max(100) | ||
}) | ||
|
||
export function validateAuthor (author) { | ||
return authorSchema.safeParse(author) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import z from 'zod' | ||
|
||
const bookSchema = z.object({ | ||
title: z.string({ | ||
required_error: 'Title is required', | ||
invalid_type_error: 'Title must be a string' | ||
}).min(1).max(100), | ||
chapters: z.number({ | ||
required_error: 'Chapters is required', | ||
invalid_type_error: 'Chapters must be a number' | ||
}).min(1), | ||
pages: z.number({ | ||
required_error: 'Pages is required', | ||
invalid_type_error: 'Pages must be a number' | ||
}).min(1), | ||
authorsIds: z.array(z.number({ | ||
required_error: 'Authors is required', | ||
invalid_type_error: 'Authors must be a number' | ||
})).min(1) | ||
}) | ||
|
||
export function validateBook (book) { | ||
return bookSchema.safeParse(book) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.