Skip to content

Commit

Permalink
Add validation for author and book data
Browse files Browse the repository at this point in the history
  • Loading branch information
fedekrenn committed Jan 2, 2024
1 parent f94f28a commit 06569fa
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 9 deletions.
2 changes: 0 additions & 2 deletions live-coding/02.1-api-node/calls.http
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ POST http://localhost:3000/books
content-type: application/json

{
"id": 1,
"title": "Harry Potter y la gallina partida",
"chapters": 13,
"pages": 356,
Expand All @@ -27,6 +26,5 @@ POST http://localhost:3000/authors
content-type: application/json

{
"id": 1,
"name": "J.K. Rowling"
}
10 changes: 9 additions & 1 deletion live-coding/02.1-api-node/src/controllers/author.js
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)
}
8 changes: 8 additions & 0 deletions live-coding/02.1-api-node/src/controllers/book.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MemoryBook } from '../models/Book/MemoryBook.js'
import { validateBook } from '../utils/bookSchema.js'

export const getAll = (req, res) => {
const books = MemoryBook.getAll()
Expand All @@ -7,6 +8,13 @@ export const getAll = (req, res) => {

export const create = (req, res) => {
try {
const result = validateBook(req.body)

if (!result.success) {
const error = JSON.parse(result.error.message)
return res.status(400).json({ error })
}

const book = MemoryBook.create(req.body)
res.status(201).json(book)
} catch (error) {
Expand Down
9 changes: 7 additions & 2 deletions live-coding/02.1-api-node/src/models/Author/MemoryAuthor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ export class MemoryAuthor {
}

static create (author) {
authors.push(author)
return author
const newAuthor = {
id: authors.length + 1,
...author
}

authors.push(newAuthor)
return newAuthor
}
}
11 changes: 8 additions & 3 deletions live-coding/02.1-api-node/src/models/Book/MemoryBook.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export class MemoryBook {

static getPagesAverage (bookId) {
const book = books.find(book => book.id === bookId)
const { id, chapters, pages } = book

if (!book) throw new Error('Book not found')
const { id, chapters, pages } = book

const pageAverage = parseFloat((pages / chapters).toFixed(2))

Expand All @@ -42,7 +42,12 @@ export class MemoryBook {
throw new Error('Author not found')
}

books.push(book)
return book
const newBook = {
id: books.length + 1,
...book
}

books.push(newBook)
return newBook
}
}
12 changes: 12 additions & 0 deletions live-coding/02.1-api-node/src/utils/authorSchema.js
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)
}
24 changes: 24 additions & 0 deletions live-coding/02.1-api-node/src/utils/bookSchema.js
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)
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@
"bugs": {
"url": "https://github.com/midudev/pruebas-tecnicas/issues"
},
"homepage": "https://github.com/midudev/pruebas-tecnicas#readme"
"homepage": "https://github.com/midudev/pruebas-tecnicas#readme",
"dependencies": {
"zod": "3.22.4"
}
}
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 06569fa

Please sign in to comment.