Joi: How to validate Enum

Created on 7 Mar 2018  路  11Comments  路  Source: sideway/joi

Hi

How can I validate enum data?

describe('Routes: Users', () => {
  const Users = app.datasource.models.Users;

  const defaultUser = {
    id: 22,
    idType: 1,
    login:'',
    password: '',
    password_md5: '',
    name: '',
    email: '',
    status: 1,
    deleted: 0,
    updatedBy: 0,
    updated: '',
    addedBy: 4,
    added: ''
  };

  beforeEach(done => {
    Users
    .destroy({ where: {} })
    .then(() => Users.create(defaultUser))
    .then(() => {
      done();
    });
  });

  describe('GET /users', () => {
    it('should validate a list of users', done => {
      request
      .get('/users')
      .end((err, res) => {
        const usersList = Joi.array().items(Joi.object().keys({
          id: Joi.number(),
          idType: Joi.number(),
          login: Joi.string(),
          password: Joi.string(),
          password_md5: Joi.string(),
          name: Joi.string(),
          email: Joi.string(),
          status: Joi.enum(1), ******************************
          deleted: Joi.enum(0), ******************************
          updatedBy: Joi.number(),
          updated: Joi.date().iso(),
          addedBy: Joi.number(),
          added: Joi.date().iso(),
        }));

        joiAssert(res.body, usersList);
        done(err);
      });
    });
  });

Thanks

support

Most helpful comment

How I validate enum values is by using the valid() or invalid() methods.

For example:

provider: Joi.string().valid([PROVIDERENUM.A, PROVIDERENUM.B]).required()

or I use invalid() if there are more acceptable options than unacceptable for code brevity.

provider: Joi.string().invalid([PROVIDERENUM.DEFAULT]).required()

All 11 comments

I have no idea what you're asking, please be more explicit.

Sorry,
Let me try to explain better.

I鈥檓 using Joi to validate my models.
Like:
For string - Joi.string
For Number -Joi.number

And I need something to validate enum, because Joi.enum don鈥檛 work

Let em know if you need more explanation.

Thanks

What is an enum for you? Your enums look like numbers.

@fsabreu do you mean boolean values?

if status can be 0 or 1 and deleted is 0 or 1, then it's Joi.boolean() is what you're looking for.

const Joi = require('joi');

const schema = Joi.boolean().truthy([1, '1']).falsy([0, '0']);

console.log(Joi.validate(0, schema));
console.log(Joi.validate(1, schema));
console.log(Joi.validate(2, schema));
console.log(Joi.validate('0', schema));
console.log(Joi.validate('1', schema));
console.log(Joi.validate('2', schema));

How I validate enum values is by using the valid() or invalid() methods.

For example:

provider: Joi.string().valid([PROVIDERENUM.A, PROVIDERENUM.B]).required()

or I use invalid() if there are more acceptable options than unacceptable for code brevity.

provider: Joi.string().invalid([PROVIDERENUM.DEFAULT]).required()

Thank you @fluxsauce @yuschick for the answers that's will certainly help me.

How I validate enum values is by using the valid() or invalid() methods.

For example:

provider: Joi.string().valid([PROVIDERENUM.A, PROVIDERENUM.B]).required()

or I use invalid() if there are more acceptable options than unacceptable for code brevity.

provider: Joi.string().invalid([PROVIDERENUM.DEFAULT]).required()

Or even easier:

provider: Joi.string().valid(Object.values(PROVIDERENUM)).required()

In this way you have to export non-const enum to make it work.

Seems there has been API change getting error Error: Method no longer accepts array arguments: valid Seems no longer works Object.values()

You have to spread it.

@Marsup is right. See here:

// ...
const enum = ["value", "lorem", "ipsum"];
//..
Joi.string().valid(...enum)
// ...

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JbIPS picture JbIPS  路  4Comments

mohamadresaaa picture mohamadresaaa  路  3Comments

farwayer picture farwayer  路  3Comments

sergibondarenko picture sergibondarenko  路  3Comments

kevbook picture kevbook  路  4Comments