Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

395
Visualizações
Is it necessary to use multiple Joi schema validations with Hapi?

I looked at an existing codebase and and I noticed that the codebase had two schema validations for what I feel a single schema could validate as the second schema is an offshoot of the first. See codebase below.

export const StudentSchema = Joi.object().keys({
    _id,
    name,
    dob,
    gender,
    grade
});

export const StudentUpdateSchema = Joi.object().keys({
    name,
    dob,
})

Now these schema are being used in the following routes:

//CREATE ROUTE
{
    method: 'POST',
    path: '/students/{id}',
    handler: async (request) => {
        const { id } = request.params;
        return Student.create(id, request.payload);
    },
    options: {
        ...sharedOptions,
        description: 'Enrolling a student',
        validate: {
            failAction,
            payload: StudentSchema,
            params: {
                studentId: Joi.objectId().required()
            },
        },
        response: {
            status: {
                200: StudentSchema,
                400: Joi.any(),
            },
        },
    },
},
// UPDATE ROUTE
{
        method: 'PUT',
        path: '/students/{id}',
        handler: async (request) => {
            const { id } = request.params;
            return Student.update(Id, { $set: request.payload });
        },
        options: {
            ...sharedOptions,
            description: 'Update student details',
            validate: {
                failAction,
                payload: StudentUpdateSchema,
                params: {
                    studentId: Joi.objectId().required(),
                },
            },
            response: {
                status: {
                    200: StudentSchema,
                    400: Joi.any(),
                    404: Joi.any(),
                },
            },
        },
    }

I am new to Hapi but have some experience with Express and Mongoose and I am inclined to re-writing these validations to use ONLY the StudentSchema in both the 'POST' and 'PUT' method since the StudentSchema contains all that is required to handle both the create and update functionality.

Can anyone with experience in HapiJS validate the pros/cons of using a single schema object (in this case StudentSchema) or should I continue the current paradigm of using the two schemas?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I would argue this design pattern is the better practice, On a partial update you should have a fitting schema to represent the incoming object better. However you can avoid the two schemas while preserving the concept behind it by extending the first one using optionalKeys.

const createSchema = Joi.object().keys({
    _id:  Joi.objectId(),
    name: Joi.string().required(),
    dob: Joi.string().required(),
    gender: Joi.string().required(),
    grade: Joi.number().required()
});

const updateSchema = createSchema.optionalKeys("gender", "grade", "_id");

Joi.validate({name: "this fails"}, createSchema); // error: required fields missing
Joi.validate({name: "this works"}, updateSchema);

This way you have a full schema that protects you while also allowing a partial field update.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda