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 cfp validation schema and mongo schema #546

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
12 changes: 12 additions & 0 deletions server/schema/cfps/CfpsSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ const CfpsSchema = new Schema(
type: String,
require: true,
},
deadline: {
type: Date,
required: true,
},
topics: {
type: [String],
required: true,
},
guidelines: {
type: String,
required: true,
},
},
{
timestamps: true,
Expand Down
12 changes: 6 additions & 6 deletions server/src/cpfs/cfp.router.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { validationSchema } from '../validator-schema/validationSchema.js';
import { cfpValidationSchema } from '../validator-schema/cfpValidation.js';
import { CfpService } from './cfp.service.js';
import express from 'express';
import { checkSchema, validationResult } from 'express-validator';
Expand All @@ -18,7 +18,7 @@ cfpRouter.get('/', async (_, res) => {
// get cfp by id
cfpRouter.get(
'/id/:id',
checkSchema(validationSchema.idSchema),
checkSchema(cfpValidationSchema.idSchema),
async (req, res) => {
try {
const errors = validationResult(req);
Expand All @@ -44,7 +44,7 @@ cfpRouter.get(
// get cfp by slug
cfpRouter.get(
'/slug/:slug',
checkSchema(validationSchema.slugSchema),
checkSchema(cfpValidationSchema.slugSchema),
async (req, res) => {
try {
const errors = validationResult(req);
Expand All @@ -70,7 +70,7 @@ cfpRouter.get(
// add cfp
cfpRouter.post(
'/create',
checkSchema(validationSchema.createSchema),
checkSchema(cfpValidationSchema.createCfpSchema),
async (req, res) => {
try {
const errors = validationResult(req);
Expand All @@ -95,7 +95,7 @@ cfpRouter.post(
// update cfp
cfpRouter.post(
'/update/:id',
checkSchema(validationSchema.createSchema),
checkSchema(cfpValidationSchema.createCfpSchema),
async (req, res) => {
try {
const errors = validationResult(req);
Expand All @@ -121,7 +121,7 @@ cfpRouter.post(
// delete cfp by id
cfpRouter.post(
'/delete',
checkSchema(validationSchema.deleteSchema),
checkSchema(cfpValidationSchema.deleteSchema),
async (req, res) => {
try {
const errors = validationResult(req);
Expand Down
154 changes: 154 additions & 0 deletions server/src/validator-schema/cfpValidation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
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 createCfpSchema = {
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 },

address: {
in: ['body'],
isObject: true,
notEmpty: {
errorMessage: 'address cannot be empty',
},
},
'address.isOnline': {
in: ['body'],
isBoolean: {
errorMessage: 'Invalid value for isOnline',
},
notEmpty: {
errorMessage: 'isOnline cannot be empty',
},
},
'address.location': {
in: ['body'],
custom: {
options: (value, { req }) => {
if (!req.body.address.isOnline && !value) {
throw new Error('location is required when isOnline is false');
}
return true;
},
},
},
image: {
in: ['body'],
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,
},
date: {
in: ['body'],
isDate: {
errorMessage: 'Invalid date value',
},
notEmpty: {
errorMessage: 'date 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',
},
},
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,
},
link: {
in: ['body'],
isURL: true,
notEmpty: true,
},
deadline: {
in: ['body'],
isDate: true,
notEmpty: true,
},
topics: {
in: ['body'],
isArray: true,
notEmpty: true,
},
guidelines: {
in: ['body'],
isString: true,
notEmpty: true,
},
};

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

export const cfpValidationSchema = {
idSchema,
slugSchema,
createCfpSchema,
deleteSchema,
};
Loading