joi

Multiple Joi validation types

泄露秘密 提交于 2020-06-25 09:33:07
问题 I search a lot but nothing found to allow multiple type validation in Joi Link: https://github.com/hapijs/joi I would like to use something like this: validate: { type: joi.or([ joi.string(), joi.array(), ]) }; 回答1: Try: validate: { type: joi.alternatives().try(joi.string(), joi.array()) } or: validate: { type: [joi.string(), joi.array()] } See: https://github.com/hapijs/joi/blob/v10.1.0/API.md#alternatives 回答2: export const saveDeviceCommandsSchema = { devices: [ Joi.array().items(Joi.string

Joi validation - how to require or optional field based on another key exists in array?

孤人 提交于 2020-06-12 18:57:49
问题 I have this Joi schema: let schema = {}; let stations = { contact: { first_name: Joi.string().min(2).max(10).regex(Regex.alphabeta, 'alphabeta').allow("").error(JoiCustomErrors), last_name: Joi.string().min(2).max(10).regex(Regex.alphabeta, 'alphabeta').allow("").error(JoiCustomErrors), phone: Joi.string().min(10).max(10).regex(Regex.num, 'num').allow("").error(JoiCustomErrors), }, address: { place: Joi.string().min(2).max(10).regex(Regex.alphanum, 'alphanum').required().error(JoiCustomErrors

Joi validation - how to require or optional field based on another key exists in array?

こ雲淡風輕ζ 提交于 2020-06-12 18:55:10
问题 I have this Joi schema: let schema = {}; let stations = { contact: { first_name: Joi.string().min(2).max(10).regex(Regex.alphabeta, 'alphabeta').allow("").error(JoiCustomErrors), last_name: Joi.string().min(2).max(10).regex(Regex.alphabeta, 'alphabeta').allow("").error(JoiCustomErrors), phone: Joi.string().min(10).max(10).regex(Regex.num, 'num').allow("").error(JoiCustomErrors), }, address: { place: Joi.string().min(2).max(10).regex(Regex.alphanum, 'alphanum').required().error(JoiCustomErrors

Joi validation - how to require or optional field based on another key exists in array?

天涯浪子 提交于 2020-06-12 18:54:24
问题 I have this Joi schema: let schema = {}; let stations = { contact: { first_name: Joi.string().min(2).max(10).regex(Regex.alphabeta, 'alphabeta').allow("").error(JoiCustomErrors), last_name: Joi.string().min(2).max(10).regex(Regex.alphabeta, 'alphabeta').allow("").error(JoiCustomErrors), phone: Joi.string().min(10).max(10).regex(Regex.num, 'num').allow("").error(JoiCustomErrors), }, address: { place: Joi.string().min(2).max(10).regex(Regex.alphanum, 'alphanum').required().error(JoiCustomErrors

Hapi/Joi validation with nested object

妖精的绣舞 提交于 2020-05-23 08:07:09
问题 I have the following validation on one of my routes: payload: { keywordGroups: Joi.array().items(Joi.object().keys({ language: Joi.string().required(), containsAny: Joi.array().items(Joi.string()).default([]).when('containsAll', { is: [], then: Joi.required() }), containsAll: Joi.array().items(Joi.string()).default([]).when('containsAny', { is: [], then: Joi.required() }), notContainsAll: Joi.array().items(Joi.string()).default([]), notContainsAny: Joi.array().items(Joi.string()).default([])

Is using Joi for validation on top of Mongoose good practice?

送分小仙女□ 提交于 2020-05-12 12:02:08
问题 I'm developing a RESTful API with Node.js, Mongoose and Koa and I'm a bit stuck on what are the best practices when it comes to schemas and input validation. Currently I have both a Mongoose and Joi schema for each resource. The Mongoose schema only includes the basic info about the specific resource. Example: const UserSchema = new mongoose.Schema({ email: { type: String, lowercase: true, }, firstName: String, lastName: String, phone: String, city: String, state: String, country: String, });

Nodejs - Joi Check if string is in a given list

元气小坏坏 提交于 2020-05-12 11:16:26
问题 I'm using Joi package for server side Validation. I want to check if a given string is in a given list or if it is not in a given list.(define black list or white list for values) sth like an "in" or "notIn" function.how can I do that? var schema = Joi.object().keys({ firstname: Joi.string().in(['a','b']), lastname : Joi.string().notIn(['c','d']), }); 回答1: How about: var schema = Joi.object().keys({ firstname: Joi.string().valid(['a','b']), lastname : Joi.string().invalid(['c','d']), });

Nodejs - Joi Check if string is in a given list

有些话、适合烂在心里 提交于 2020-05-12 11:15:58
问题 I'm using Joi package for server side Validation. I want to check if a given string is in a given list or if it is not in a given list.(define black list or white list for values) sth like an "in" or "notIn" function.how can I do that? var schema = Joi.object().keys({ firstname: Joi.string().in(['a','b']), lastname : Joi.string().notIn(['c','d']), }); 回答1: How about: var schema = Joi.object().keys({ firstname: Joi.string().valid(['a','b']), lastname : Joi.string().invalid(['c','d']), });

How to allow any other key in Joi [duplicate]

折月煮酒 提交于 2020-02-23 08:42:08
问题 This question already has an answer here : Joi object validation: How to validate values with unknown key names? (1 answer) Closed 2 years ago . I have a simple requirement. I tried to search on the internet as well as documentation but failed. So here is what I want to achieve: I have a schema: const schema = Joi.object().keys({ a: Joi.string().required(), b: Joi.string().required() }); Now, How do I configure it such that any other key in the object would be allowed? With this schema, it

Using JOI How to define recursive array of objects validation with n depth

被刻印的时光 ゝ 提交于 2020-01-24 05:28:07
问题 let obj = Joi.object().keys({ "id": Joi.string().required(), "array": Joi.array().items(obj).required()//array contains multiple }); is there any way to define recursive array validation in JOI obj.array contains n number of obj 回答1: Recursive schemas can be achieved using Joi's lazy(fn) function. The following example from the documentation can be adapted to your schema however I'm not sure how you'd be able to define a max depth. const Person = Joi.object({ firstName: Joi.string().required(