Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update hackathon schema and mongo schema #547

Merged
merged 1 commit into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions server/schema/hackathon/hackathonSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const HackathonSchema = new Schema(
type: String,
require: true,
},
address: {
mode: {
type: { isOnline: Boolean, location: String },
required: true,
},
Expand All @@ -43,10 +43,10 @@ const HackathonSchema = new Schema(
type: Date,
require: true,
},
mode: {
type: String, // offline or online
require: true,
},
// mode: {
// type: String, // offline or online
// require: true,
// },
rewards: {
type: [RewardSchema],
required: false,
Expand Down
12 changes: 6 additions & 6 deletions server/src/hackathons/hackathon.router.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { validationSchema } from '../validator-schema/validationSchema.js';
import { hackathonValidationSchema } from '../validator-schema/hackathonValidation.js';
import { HackathonService } from './hackathon.service.js';
import express from 'express';
import { checkSchema, validationResult } from 'express-validator';
Expand All @@ -20,7 +20,7 @@ hackathonRouter.get('/', async (_, res) => {

hackathonRouter.get(
'/id/:id',
checkSchema(validationSchema.idSchema),
checkSchema(hackathonValidationSchema.idSchema),
async (req, res) => {
try {
const errors = validationResult(req);
Expand All @@ -46,7 +46,7 @@ hackathonRouter.get(

hackathonRouter.get(
'/slug/:slug',
checkSchema(validationSchema.slugSchema),
checkSchema(hackathonValidationSchema.slugSchema),
async (req, res) => {
try {
const errors = validationResult(req);
Expand All @@ -71,7 +71,7 @@ hackathonRouter.get(
//create hackathon
hackathonRouter.post(
'/create',
checkSchema(validationSchema.createSchema),
checkSchema(hackathonValidationSchema.createHackathonSchema),
async (req, res) => {
try {
const errors = validationResult(req);
Expand All @@ -95,7 +95,7 @@ hackathonRouter.post(
//update hackathon
hackathonRouter.post(
'/update/:id',
checkSchema(validationSchema.createSchema),
checkSchema(hackathonValidationSchema.createHackathonSchema),
async (req, res) => {
try {
const errors = validationResult(req);
Expand All @@ -122,7 +122,7 @@ hackathonRouter.post(

hackathonRouter.post(
'/delete',
checkSchema(validationSchema.deleteSchema),
checkSchema(hackathonValidationSchema.deleteSchema),
async (req, res) => {
try {
const errors = validationResult(req);
Expand Down
196 changes: 196 additions & 0 deletions server/src/validator-schema/hackathonValidation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
const idSchema = {
id: {
in: ['params'],
isString: {
errorMessage: 'Invalid id',
},
notEmpty: {
errorMessage: 'id cannot be empty',
},
},
};

const slugSchema = {
slug: {
in: ['params'],
isString: {
errorMessage: 'Invalid slug',
},
notEmpty: {
errorMessage: 'Slug cannot be empty',
},
},
};
const createHackathonSchema = {
name: {
in: ['body'],
isLength: {
options: { min: 2 },
errorMessage: 'Name must be at least 2 characters long',
},
notEmpty: {
errorMessage: 'Name cannot be empty',
},
},
slug: { in: ['body'], isString: true, notEmpty: true },
organizer: { in: ['body'], isString: true, notEmpty: true },
description: { in: ['body'], isString: true, notEmpty: true },

mode: {
in: ['body'],
isObject: true,
notEmpty: {
errorMessage: 'mode cannot be empty',
},
},
'mode.isOnline': {
in: ['body'],
isBoolean: {
errorMessage: 'Invalid value for isOnline',
},
notEmpty: {
errorMessage: 'isOnline cannot be empty',
},
},
'mode.location': {
in: ['body'],
custom: {
options: (value, { req }) => {
if (!req.body.mode.isOnline && !value) {
throw new Error('location is required when isOnline is false');
}
return true;
},
},
},
image: {
in: ['body'],
isString: {
errorMessage: 'Invalid image value',
},
// custom: {
// options: (value) => {
// if (!/\.(png|jpg|jpeg)$/.test(value)) {
// throw new Error('Image must have a valid extension png, jpg, jpeg');
// }
// return true;
// },
// },
optional: true,
},
deadline: {
in: ['body'],
isDate: {
errorMessage: 'Invalid deadline value',
},
notEmpty: {
errorMessage: 'deadline cannot be empty',
},
},
// mode: {
// in: ['body'],
// isString: {
// errorMessage: 'Invalid mode value',
// },
// notEmpty: {
// errorMessage: 'mode cannot be empty',
// },
// },
rewards: {
in: ['body'],
isObject: true,
notEmpty: {
errorMessage: 'rewards cannot be empty',
},
},
'rewards.title': {
in: ['body'],
isString: {
errorMessage: 'Invalid rewards title value',
},
notEmpty: {
errorMessage: 'rewards title cannot be empty',
},
},
'rewards.prize': {
in: ['body'],
isString: {
errorMessage: 'Invalid rewards prize value',
},
notEmpty: {
errorMessage: 'rewards prize cannot be empty',
},
},
size: {
in: ['body'],
isNumeric: {
errorMessage: 'Invalid size value',
},
optional: true,
},
eligibility: {
in: ['body'],
isBoolean: {
errorMessage: 'Invalid eligibility value',
},
notEmpty: {
errorMessage: 'eligibility cannot be empty',
},
},
duration: {
in: ['body'],
isNumeric: {
errorMessage: 'Invalid duration value',
},
notEmpty: {
errorMessage: 'duration cannot be empty',
},
},
tags: {
in: ['body'],
isArray: {
errorMessage: 'Tags must be an array',
options: { min: 1 },
},
notEmpty: {
errorMessage: 'Tags array must not be empty',
},
},
link: {
in: ['body'],
isURL: true,
notEmpty: true,
},
date: {
in: ['body'],
isDate: true,
notEmpty: true,
},
logo: {
in: ['body'],
custom: {
options: (value) => {
if (!/\.(png|jpg|jpeg)$/.test(value)) {
throw new Error('Logo must have a valid extension png, jpg, jpeg');
}
return true;
},
},
optional: true,
},
};

const deleteSchema = {
id: {
in: ['body'],
isString: true,
optional: true,
},
};

export const hackathonValidationSchema = {
idSchema,
slugSchema,
createHackathonSchema,
deleteSchema,
};
Loading