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

179
Visualizações
Possible to have `validate` fail on not specified elements?

The below returns success, even though obj contains an element that is not in the schema.

Question

Is it possible to have validate fail, when it sees elements that are not specified in the schema?

Jest have this expect(obj1).toEqual(obj2).

If Validate can't do it, what options do I then have to detect unwanted elements?

const Schema = require("validate");

const obj = {good: "1", bad: "2"};

const user = new Schema({
  good: {
    type: String,
    required: true,
  }
});

const r = user.validate(obj);
console.log(r);
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Yes, there is a strict option on the Schema, from the documentation:

Schema

A Schema defines the structure that objects should be validated against.

Parameters
  • obj schema definition
  • opts options
    • opts.typecast

    • opts.strip

    • opts.strict

      Validation fails when object contains properties not defined in the schema (optional, default false)

So you'll need something like:

const options = { strict: true };

const user = new Schema({
  good: {
    type: String,
    required: true,
  }
}, options);
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