Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

180
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda