Joi Validation Conflict with Multiple HTTP Methods on Same Express Route

i am trying to implement a Rest API but Joi validation conflicts if i create separate routers for different http methods for the same route, like below.

Router 1:
Path: ‘/’,
Method: Get,
Joi Schema: ‘Schema 1’,

Router 2:
Path: ‘/’,
Method: Post,
Joi Schema: ‘Schema 2’,

Router 3:
Path: ‘/’,
Method: Patch,
Joi Schema: ‘Schema 3’,

Main Router:
Path: ‘path-name’
Use: Router 1,
Use: Router 2,
Use: Router 3,

The problem occurs in the Router 3’s joi validation.

Here is the corresponding files.

Get.mjs – Router 1

import express from 'express';
import { necessarySchemas } from '../../schemas.mjs';
import { Validator } from '../../validator.mjs';
 
const router = express.Router();
router.use(Validator.validateAuthorizationBearerJwt(authorizationSchema));

router.get('/', async (req, res) => {
    // code here
});

router.get('/:id', Validator.validate(schema1, { params: true }), async (req, res) => {
    // code here
});

export default router;

Post.mjs – Router 2

import express from 'express';
import { necessarySchemas } from '../../schemas.mjs';
import { Validator } from '../../validator.mjs';

const router = express.Router();
router.use(Validator.validateAuthorizationBearerJwt(authorizationSchema));
router.use(Validator.validate(schema2))

router.post('/', async (req, res) => {
    // code here
});

export default router;

Patch.mjs – Router 3

import express from 'express';
import { necessarySchemas } from '../../schemas.mjs';
import { Validator } from '../../validator.mjs';

const router = express.Router();
router.use(Validator.validateAuthorizationBearerJwt(authorizationSchema));
router.use(Validator.validate(schema3));

router.patch('/:id', Validator.validate(schema4, { params: true }), async (req, res) => {
    // code here
});


export default router;

Router.mjs – Main Router

import express from 'express';
import get from './get.mjs';
import post from './post.mjs';
import patch from './patch.mjs';

const router = express.Router();

router.use('/', get);
router.use('/', post);
router.use('/', patch);

export default router; 

Here are the schemas

export const schema1 = Joi.object({
    id: Joi.number().min(0).required(),
});

export const schema2 = Joi.object({
    shareCode: Joi.string().length(6),
});

export const schema3 = Joi.object({
    name: Joi.string().min(6).max(50).required(),
});

export const schema4 = Joi.object({
    id: Joi.number().min(0).required(),
});

Validator

validate(schema, source = { body: true, params: false, query: false }) {
        return (req, res, next) => {
            var result = null;

            if (source.body) {
                result = schema.validate(req.body);
            } else if (source.params) {
                result = schema.validate(req.params);
            } else if (source.query) {
                result = schema.validate(req.query);
            }

            if (result.error) {
                return res.status(400).json({
                    message: result.error.details[0].message,
                });
            }

            next();
        };
    }

What happens is that if i send a patch request and add the field ‘name’ in the body i get this response

{
    "message": ""name" is not allowed"
}

and if i delete that field i get

{
    "message": ""name" is required"
}

Removing post router from the Main Router gets rid of the problem.
Also if change the .use to .post and .patch like below is also a fix

router.use('/', get);
router.post('/', post);
router.patch('/', patch);

My question is why this problem occurs in the first place? Express can handle http requests by the http method that is used so joi validation shouldn’t conflict right? but it does for some reason.

Since i can not remove post router can you explain why using it the way on top works?

Also are there any other solutions to this except for using different path names?

Thanks in advance!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật