Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!