Skip to content
Advertisement

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?

Advertisement

Answer

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)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement