joi

Joi validation multiple conditions

╄→гoц情女王★ 提交于 2019-12-04 17:43:32
问题 I have the following schema: var testSchema = Joi.object().keys({ a: Joi.string(), b: Joi.string(), c: Joi.string().when('a', {'is': 'avalue', then: Joi.string().required()}) }); but I would like to add a condition on c field definition so that it is required when: a == 'avalue' AND b=='bvalue' How can I do that? 回答1: You can concatenate two when rules: var schema = { a: Joi.string(), b: Joi.string(), c: Joi.string().when('a', { is: 'avalue', then: Joi.string().required() }).concat(Joi.string

How to validate key order in javascript object using Joi?

元气小坏坏 提交于 2019-12-04 07:14:41
问题 I want to validate object that keys should be in asending order e.g. // valid because all keys are in order var object = { 0: 'alok', 1: 'singh', 2: 'mahor' } // not valid because all keys are not in order var object = { 2: 'alok', 0: 'singh', 1: 'mahor' } // not valid because all keys are not in order var object = { 1: 'alok', 2: 'singh', 0: 'mahor' } // this is not capable of checking key order var schema = Joi.object({ 0: Joi.string(), 1: Joi.string(), 2: Joi.string() }); how can I

How to validate nested object whose keys should match with outer objects another key whose value is array using Joi?

余生长醉 提交于 2019-12-04 06:21:27
问题 I have object which I want to validate. // valid object because all values of keys are present in details object var object = { details: { key1: 'stringValue1', key2: 'stringValue2', key3: 'stringValue3' }, keys: ['key1', 'key2', 'key3'] } // invalid object as key5 is not present in details var object = { details: { key4: 'stringValue4' }, keys: ['key4', 'key5'] } // invalid object as key5 is not present and key8 should not exist in details var object = { details: { key4: 'stringValue4', key8

How to use enum values with Joi String validation

北战南征 提交于 2019-12-03 12:33:02
I am using Joi validator for my HTTP requests. There I have a parameter called type . I need to make sure that the possible values for the parameter are either "ios" or "android". How can I do that? body : { device_key : joi.string().required(), type : joi.string().required() } You can use valid . const schema = Joi.object().keys({ type: Joi.string().valid('ios', 'android'), }); const myObj = { type: 'none' }; const result = Joi.validate(myObj, schema); console.log(result); This gives an error ValidationError: child "type" fails because ["type" must be one of [ios, android]] 来源: https:/

Joi validation of array

折月煮酒 提交于 2019-12-03 08:12:45
问题 trying to validate that an array has zero or more strings in one case and that it has zero or more objects in another , struggling with Joi docs :( validate: { headers: Joi.object({ 'content-type': "application/vnd.api+json", accept: "application/vnd.api+json" }).options({ allowUnknown: true }), payload : Joi.object().keys({ data : Joi.object().keys({ type: Joi.any().allow('BY_TEMPLATE').required(), attributes: Joi.object({ to : Joi.string().email().required(), templateId : Joi.string()

Joi validation of array

元气小坏坏 提交于 2019-12-03 02:04:09
trying to validate that an array has zero or more strings in one case and that it has zero or more objects in another , struggling with Joi docs :( validate: { headers: Joi.object({ 'content-type': "application/vnd.api+json", accept: "application/vnd.api+json" }).options({ allowUnknown: true }), payload : Joi.object().keys({ data : Joi.object().keys({ type: Joi.any().allow('BY_TEMPLATE').required(), attributes: Joi.object({ to : Joi.string().email().required(), templateId : Joi.string().required(), categories : Joi.array().items( //trying to validate here that each element is a string),

Using Joi, require one of two fields to be non empty

℡╲_俬逩灬. 提交于 2019-12-01 02:54:38
If I have two fields, I'd just like to validate when at least one field is a non empty string, but fail when both fields are empty strings. Something like this does not validate var schema = Joi.object().keys({ a: Joi.string(), b: Joi.string() }).or('a', 'b'); When validating against {a: 'aa', b: ''} The or condition only tests for the presence of either key a or b , but does test whether the condition for a or b is true. Joi.string() will fail for empty strings. Here is gist with some test cases to demonstrate http://requirebin.com/?gist=84c49d8b81025ce68cfb Code below worked for me. I used

Using Joi, require one of two fields to be non empty

拜拜、爱过 提交于 2019-11-30 22:53:03
问题 If I have two fields, I'd just like to validate when at least one field is a non empty string, but fail when both fields are empty strings. Something like this does not validate var schema = Joi.object().keys({ a: Joi.string(), b: Joi.string() }).or('a', 'b'); When validating against {a: 'aa', b: ''} The or condition only tests for the presence of either key a or b , but does test whether the condition for a or b is true. Joi.string() will fail for empty strings. Here is gist with some test

How to validate array of objects using Joi?

孤者浪人 提交于 2019-11-30 16:52:24
I am getting an array of objects to backend, where each object contains a service name. The structure looks like below [{"serviceName":"service1"}, {"serviceName":"service2"},..] when I get the array at backend, I want to validate that every object in the array has serviceName property. I had written the following code, but even though I pass valid array, I am getting validation error. var Joi = require('joi'); var service = Joi.object().keys({ serviceName: Joi.string().required() }); var services = Joi.array().ordered(service); var test = Joi.validate([{serviceName:'service1'},{serviceName:

Is there a way to validate dynamic key names in a Joi schema?

十年热恋 提交于 2019-11-28 01:21:49
Is there a way I can validate a value like this with Joi so that I can verify it is an object with zero or more keys (of any name) and that each have values of either a string, number or boolean? { dynamicallyNamedKey1: 'some value', dynamicallyNamedKey2: 4 } You're going to want to use Joi 's object().pattern() method. It's specifically for validating objects with unknown keys. To match against one or more datatypes on a single key you'll need alternatives().try() (or simply pass an array of Joi types). So the rule to match your needs would be: Joi.object().pattern(/^/, Joi.alternatives().try