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 validate against elements in an array?

I am using validate to validate input, and I need to validate an input that has to be one of the numbers in my arr.

const arr = [1, 2, 4, 5, 9, 14];

const p = {
  System: {
    type: Number,
    enum: arr,
    required: true
  }
};

If I try enum: arr, then it takes the entire array and not just one of the elements.

Question

Is it possible to get validate to use one of the numbers from arr?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

import Schema from 'validate'
const arr = [1, 2, 4, 5, 9, 14];

const p_schema = new Schema({
  System: {
    type: Number,
    enum: [...arr],
    required: true
  }
});
let p = {
    System: 9
}

const errors = p_schema.validate(p)
about 4 years ago · Juan Pablo Isaza Relatório

0

When you specify enum rule, the value of System must be one of the values in the array. The problem with your code however is that you didn't call Scheme from the validate package, so here what you need to change:

import Schema from "validate";

const arr = [1, 2, 4, 5, 9, 14];
const v = new Schema({
  System: {
    type: Number,
    enum: arr,
    required: true,
  },
});

// For testing
const error = v.validate({ System: 3 });

about 4 years ago · Juan Pablo Isaza 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