Added schma to validate

This commit is contained in:
Nicolai Ort 2021-08-12 19:39:10 +02:00
parent bb09b731aa
commit cfc49f3ec4
Signed by: niggl
GPG Key ID: 13AFA55AF62F269F
1 changed files with 16 additions and 6 deletions

View File

@ -31,26 +31,36 @@ fastify.get('/:id', async (req, res) => {
})
//Create new urls
fastify.post('/new', async (req, res) => {
const newUrlSchema = {
body: {
type: 'object',
properties: {
target: { type: 'string' },
shortcode: { type: 'string' },
}
}
};
fastify.post('/new', { newUrlSchema }, async (req, res) => {
const target = req.body?.target;
let shortcode = req.body?.shortcode;
if (!req.body?.target) {
if (!target) {
res.statusCode = 400;
return "Missing target";
}
if (!shortcode) {
shortcode = nanoid(12);
}
else{
else {
try {
const exists = await db.get(shortcode);
if(exists){
if (exists) {
res.statusCode = 400;
return "Shortcode already exists, please choose another code";
}
} catch (error) {
if(!error.notFound){
res.statusCode=500;
if (!error.notFound) {
res.statusCode = 500;
return "Internal Server Error."
}
}