Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate Enum String with Joi latest version?

How do I validate Enum String?

I used to go with this as suggested from here: https://github.com/hapijs/joi/issues/1449

enum UserRole {
  Admin = 'admin',
  Staff = 'staff'
}

const validator = {
  create: Joi.object().keys({
    first_name: Joi.string().min(1),
    last_name: Joi.string()
      .min(1)
      .required(),
    password: Joi.string()
      .regex(/^[\x20-\x7E]+$/)
      .min(8)
      .max(72)
      .required(),
    role: Joi.string()
      .valid([UserRole.Admin, UserRole.Staff])
      .optional(),
    is_active: Joi.boolean().optional()
  })
};

But now , Error: Method no longer accepts array arguments: valid

like image 891
Rario Avatar asked Oct 21 '19 13:10

Rario


People also ask

How do I validate the data in Joi?

The validation is done using the Joi.validate () method, with the following signature: data: the data to validate which in our case is req.body. schema: the schema with which to validate the data. options: an object that specifies the validation options. Here are the validation options we used:

How to check if a string is valid for an enum?

For this, we can create an annotation that checks if the String is valid for a specific enum. This annotation can be added to a String field and we can pass any enum class. Let's define the ValueOfEnumValidator to check whether the String (or any CharSequence) is contained in the enum:

Can I share code between enum validators?

While the annotation has to be specific for a certain enum, we can of course share code between different validators. 5. Validating That a String Matches a Value of an Enum Instead of validating an enum to match a String, we could also do the opposite. For this, we can create an annotation that checks if the String is valid for a specific enum.

How to check Joi version in command prompt?

After installing multer you can check your joi version in command prompt using the command. After that, you can just create a folder and add a file for example index.js, To run this file you need to run the following command. Requiring module: You need to include joi module in your file by using these lines.


2 Answers

I could make it work using Joi.any().valid() like this, hope works for you.

const Joi = require("@hapi/joi");

const roles = ["admin", "staff"];

const schema = Joi.object({
  first_name: Joi.string().min(1),
  last_name: Joi.string()
    .min(1)
    .required(),
  password: Joi.string()
    .regex(/^[\x20-\x7E]+$/)
    .min(8)
    .max(72)
    .required(),
  role: Joi.any().valid(...roles),
  is_active: Joi.boolean().optional()
});

Examples:

when a valid role is used no error.

const { error, value } = schema.validate({
  first_name: "Magnus",
  last_name: "Carlsen",
  password: "chess/1234",
  role: "staff"
});

console.log(error); //undefined

when no role is used no error.

const { error, value } = schema.validate({
  first_name: "Magnus",
  last_name: "Carlsen",
  password: "chess/1234"
});

console.log(error); //undefined

when a different role is used it gives validation error.

const { error, value } = schema.validate({
  first_name: "Magnus",
  last_name: "Carlsen",
  password: "chess/1234",
  role: "unknown"
});

console.log(error); // Error [ValidationError]: "role" must be one of [admin, staff]...

like image 193
SuleymanSah Avatar answered Oct 05 '22 09:10

SuleymanSah


enum UserRole {
  Admin = 'admin',
  Staff = 'staff'
}

const validator = {
  create: Joi.object().keys({
    first_name: Joi.string().min(1),
    last_name: Joi.string()
      .min(1)
      .required(),
    password: Joi.string()
      .regex(/^[\x20-\x7E]+$/)
      .min(8)
      .max(72)
      .required(),
    role: Joi.string()
      .valid(UserRole.Admin, UserRole.Staff) // do not use an array
      .optional(),
    is_active: Joi.boolean().optional()
  })
};
like image 20
Gonzalo A. Avatar answered Oct 05 '22 09:10

Gonzalo A.