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
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:
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:
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.
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.
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]...
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()
})
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With