Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example of schema validation test with Joi
- Loading branch information
1 parent
c90616a
commit 5ed5cce
Showing
6 changed files
with
44 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const toMatchSchema = (received, schema) => { | ||
const { error } = schema.validate(received, { presence: 'required' }) | ||
const pass = !error | ||
|
||
if (pass) { | ||
return { | ||
pass: true | ||
} | ||
} | ||
|
||
return { | ||
message: () => `${error}`, | ||
pass: false | ||
} | ||
} | ||
|
||
module.exports = { | ||
toMatchSchema | ||
} |
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,13 @@ | ||
const Joi = require('joi') | ||
|
||
const PRODUCT_SCHEMA = Joi.object({ | ||
nome: Joi.string(), | ||
preco: Joi.number(), | ||
descricao: Joi.string(), | ||
quantidade: Joi.number(), | ||
_id: Joi.string() | ||
}) | ||
|
||
module.exports = { | ||
PRODUCT_SCHEMA | ||
} |
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