joi

How to add custom validator function in Joi?

大城市里の小女人 提交于 2021-02-18 11:24:13
问题 I have Joi schema and want to add a custom validator for validating data which isn't possible with default Joi validators. Currently, I'm using the version 16.1.7 of Joi const method = (value, helpers) => { // for example if the username value is (something) then it will throw an error with flowing message but it throws an error inside (value) object without error message. It should throw error inside the (error) object with a proper error message if (value === "something") { return new Error

Joi Validation Regex or pattern

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-16 20:23:49
问题 I want to joi use regex pattern which define in variable I have a variable pattern which contains regex i.e pattern = "/^[0-9+]{7}-[0-9+]{1}$/" and this pattern send to Joi module and want to confirm module.exports = { save: { body: { match: Joi.string().regex(pattern).required } } } I know validation work if I use this module.exports = { save: { body: { match: Joi.string().regex(/^[0-9+]{7}-[0-9+]{1}$/).required } } } But in my case every time regex will different. So I can not use above

Joi deep nested recursive array of alternative objects validation

冷暖自知 提交于 2021-02-11 18:19:38
问题 I have following json I am trying to validate using @hapi/Joi 16+. It largely works up to 3 levels. But after that it wouldn't validate the alternative files . folder schema get validated fine. I am hoping for optimised solution that would validate deep nested array. In here ONLY folders suppose to have optional children . Files schema should NOT have children . Hence I am using alternatives. const data = [{ id: '1', //UUID type: 'folder', children: [{ id: '2', //UUID type: 'folder', children

Joi deep nested recursive array of alternative objects validation

时光总嘲笑我的痴心妄想 提交于 2021-02-11 18:18:12
问题 I have following json I am trying to validate using @hapi/Joi 16+. It largely works up to 3 levels. But after that it wouldn't validate the alternative files . folder schema get validated fine. I am hoping for optimised solution that would validate deep nested array. In here ONLY folders suppose to have optional children . Files schema should NOT have children . Hence I am using alternatives. const data = [{ id: '1', //UUID type: 'folder', children: [{ id: '2', //UUID type: 'folder', children

How do I override empty rule with messages in JOI 17?

老子叫甜甜 提交于 2021-02-11 12:19:56
问题 I read Node.js + Joi how to display a custom error messages? and am still having trouble. I have a schema like this: const create = validator.object().keys({ app: validator .string() .required() .valid(...ARRAY_OF_VALUES) .messages({'any.unknown':'Must pass valid code'}) }); An update of the question above points at https://github.com/sideway/joi/blob/master/API.md#list-of-errors for the valid error types. I test with a value of invalid! and still see the default error message. I have tried

Joi: Automatic validation of function arguments

佐手、 提交于 2021-02-10 05:36:47
问题 I saw a codebase which was using joi library like this: function f(a, b) { // ... } f.schema = { a: Joi.string().uuid().required(), b: Joi.number() } And then the f.schema property wasn't referenced anywhere else. Is there some framework which performs automatic validation of function arguments using the schema property? Googling this didn't bring up anything. 回答1: I don't think it is possible to do exactly what you are showing here, since overloading of function call is impossible in

Joi Nested schema

不羁的心 提交于 2021-02-08 12:16:46
问题 I am trying to create nested schema in joi and it is throwing error [Error: Object schema cannot be a joi schema] var nestedSchema = joi.object({ b: joi.number() }); var base = joi.object({ a: joi.string(), nestedData:joi.object(nestedSchema) }); How should i define nested schema in joi? 回答1: You could use object.keys API var nestedSchema = joi.object().keys({ b: joi.number() }); var base = joi.object({ a: joi.string(), nestedData: nestedSchema }); 回答2: Although Francesco's answer works,

How to extend a module from npm using TypeScript?

自古美人都是妖i 提交于 2021-02-07 13:25:43
问题 I'm using joi and @types/joi with TypeScript. Joi has an extend method which allows extending joi by returning a new instance without modifying original joi library. I created an extended instance with it. To create definition for this extended instance, I tried Module Augmentation as described here using code below: declare module 'joi' { // Add a new Schema type which has noChildren() method. interface CustomSchema extends ObjectSchema { noChildren(): this; } } However, as expected, this

hapijs joi validation , validate greater than from sum of other property

爱⌒轻易说出口 提交于 2021-01-28 20:26:50
问题 I want to validate a field 'familymemberCount' it should be greater than equal to other fields. I tried below code, but this not allow to use ' + ' operator with Ref. How do we validate with sum of other values ? export const familyMemberRulesSchema = Joi.object({ relationMembers: Joi.object({ motherCount: Joi.number().integer().min(0).max(5).optional(), fatherCount: Joi.number().integer().min(0).max(5).optional(), childrenCount: Joi.number().integer().min(0).max(5).optional() }),

Mark all properties as required or not Joi

徘徊边缘 提交于 2021-01-07 03:09:01
问题 I'm building an Express API and using @hapi/joi for validation. However I find myself in the following situation: if I want to validate a new user, all the properties in the schema have to be required, but if the client wants to modify a user, these properties must be optional since only the client knows what properties it will modify. So if I have this schema for new users : function validateUser(user) { const schema = Joi.object().keys({ name: Joi.string().required(), lastName: Joi.string()