0

Is it possible to use an enum for the property names of a Joi schema?

For example, given the following string-constants.enum.js file:

export default {
    INPUT_BUSINESS_NAME: 'business-name',
    INPUT_ADDRESS_LINE_1: 'address-line-1'
    // etc...
}

What I want to do is:

const stringConstants = require('path/to/string-constants.enum')
const Joi = require('@hapi/joi')

const businessDetailsSchema = {
    [stringConstants.INPUT_BUSINESS_NAME]: Joi.string().required(),
    [stringConstants.INPUT_ADDRESS_LINE_1]: Joi.string().required(),
    //  etc...
}

However, when a fieldset that includes 'business-name' or 'address-line-1' fields is validated it throws an error saying that the fields are't defined in the schema.

If I use this though:

const bname = 'business-name'
const add1 = 'address-line-1'
const businessDetailsSchema = {
    [bname]: Joi.string().required(),
    [add1]: Joi.string().required(),
    //  etc...
}

Then it works OK - so it seems to be an issue with reading the values from an imported enum. Thanks

Nick Roper
  • 127
  • 1
  • 3
  • 13

1 Answers1

0

Fixed it - needed to use import instaed of require to bring the enums in.

Works now

Nick Roper
  • 127
  • 1
  • 3
  • 13