How to use the required function from joi

Find comprehensive JavaScript joi.required code examples handpicked from public code repositorys.

joi.required is a method in the Joi library that is used to specify a schema validation rule that a particular key or value is required to be present in the input data.

1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
descriptionFormat: Joi.string(),
metadata: Joi.array()
  .items(
    Joi.object().keys({
      name: Joi.string().required(),
      value: Joi.required(),
    })
  )
  .unique((a, b) => a.name === b.name),
timelineTemplateId: Joi.string(), // Joi.optionalId(),
fork icon44
star icon17
watch icon25

+ 27 other calls in file

15
16
17
18
19
20
21
22
23
24
}))

/* eslint-disable prettier/prettier */
const OAUTH2_STR = Joi.string().when('OAUTH2_BYPASS_SSO', {
  is: false,
  then: Joi.required(),
})
const OAUTH2_URI = Joi.string()
  .uri()
  .when('OAUTH2_BYPASS_SSO', { is: false, then: Joi.required() })
fork icon8
star icon8
watch icon16

+ 5 other calls in file

How does joi.required work?

joi.required is a validation rule in the Joi library for Node.js that specifies that a given key or value must be present and cannot be undefined, null or empty. If a field is marked as required and the provided value is not valid, Joi will return an error indicating that the field is required.

2
3
4
5
6
7
8
9
10
11
12
13


const blueprintUsuario = Joi.object().keys({
  usuario: Joi.string().alphanum().min(3).max(30).required(),
  password: Joi.string().min(6).max(200).required(),
  id_cargo: Joi.required(),
  id_usuario: Joi.required(),
});


let validarUsuario = (req, res, next) => {
  let resultado = blueprintUsuario.validate(req.body, {
fork icon0
star icon1
watch icon1

+ 9 other calls in file

3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
    .example('R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=')
    .description('Base64 formatted attachment file')
    .when('reference', {
        is: Joi.exist().not(false, null),
        then: Joi.forbidden(),
        otherwise: Joi.required()
    }),

contentType: Joi.string().lowercase().max(256).example('image/gif'),
contentDisposition: Joi.string().lowercase().valid('inline', 'attachment'),
fork icon113
star icon0
watch icon16

+ 11 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const Joi = require("joi");

const schema = Joi.object({
  username: Joi.string().required(),
  password: Joi.string().required(),
});

const { error, value } = schema.validate({
  username: "john",
  password: "1234",
});

if (error) {
  console.log(error.message);
} else {
  console.log(value);
}

In this example, joi.required() is used to specify that the username and password properties in the Joi.object() schema are required. If the input object passed to the schema.validate() method does not contain these required properties, an error will be returned.

1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
description: Joi.string(),
privateDescription: Joi.string(),
descriptionFormat: Joi.string(),
metadata: Joi.array().items(Joi.object().keys({
  name: Joi.string().required(),
  value: Joi.required()
})).unique((a, b) => a.name === b.name),
timelineTemplateId: Joi.string(), // Joi.optionalId(),
phases: Joi.array().items(Joi.object().keys({
  phaseId: Joi.id(),
fork icon44
star icon17
watch icon25

+ 2 other calls in file

133
134
135
136
137
138
139
140
141
142
applicationForWaiverOfFilingFeeFile: joi
  .object()
  .when('petitionPaymentStatus', {
    is: PAYMENT_STATUS.WAIVED,
    otherwise: joi.optional().allow(null),
    then: joi.required(),
  }),
applicationForWaiverOfFilingFeeFileSize:
  JoiValidationConstants.MAX_FILE_SIZE_BYTES.when(
    'applicationForWaiverOfFilingFeeFile',
fork icon36
star icon72
watch icon19

+ 31 other calls in file

9
10
11
12
13
14
15
16
17
18




module.exports = searchApiValidations;




// .when("addressIds",{is:Joi.undefined,then: Joi.required()
fork icon1
star icon0
watch icon1

+ 2 other calls in file

97
98
99
100
101
102
103
104
105
106
107
});


const questionValSchema = Joi.object().keys({
  options: Joi.array().items(
    Joi.object({
      optionLabel: Joi.required(),
      isTrue: Joi.boolean().required(),
      placeholder: Joi.any(),
      lastSelectNumber: Joi.number(),
      isSelected: Joi.boolean(),
fork icon0
star icon0
watch icon1

84
85
86
87
88
89
90
91
92
93
},
{
  path: "/api/user/updata",
  schema: joi.object({
    key: joi.string().required(),
    value: joi.required(),
  }),
},
{
  path: "/api/blog/getUserBlog",
fork icon0
star icon0
watch icon1

+ 14 other calls in file

1
2
3
4
5
6
7
8
9
10
11


const userValidate = (data, type) => {
	if (type === 'signup') {
		const userSchema = Joi.object({
			name: Joi.string().min(2).max(25).required(),
			// avatar: Joi.required(),
			email: Joi.string().email().lowercase().required(),
			password: Joi.string().min(6).max(32).required(),
		});
		return userSchema.validate(data);
fork icon0
star icon0
watch icon1

+ 3 other calls in file

9
10
11
12
13
14
15
16
17
18
19
 */


// 用户名的验证规则
const title = joi.string().required()
// 通知类型验证规则
const type = joi.required()
// 新增讯息通知的验证规则对象
exports.add_notice_schema = {
  // 表示需要对 req.body 中的数据进行验证
  body: {
fork icon0
star icon0
watch icon1

7
8
9
10
11
12
13
14
15
16
17
18
  database: process.env.DB_DATABASE,
});


const joi = require("joi");
const schema = joi.object({
  id: joi.required(),
});


exports.todoGetId = (req, res) => {
  const {id} = req.params;
fork icon0
star icon0
watch icon1

+ 3 other calls in file

980
981
982
983
984
985
986
987
988
989
    return Joi.validate(user, schema);
}


function idValidation(id) {
    const schema = Joi.object().keys({
        user_id: Joi.required(),
    });
    return Joi.validate(id, schema);
}
fork icon0
star icon0
watch icon1

+ 4 other calls in file

9
10
11
12
13
14
15
16
17
18
}

loginSchema() {
    return Joi.object().keys({
        email: Joi.string().email().insensitive().required(),
        password: Joi.required(),
    });
}

signUpSchema() {
fork icon0
star icon0
watch icon1

+ 7 other calls in file

7
8
9
10
11
12
13
14
15
16
     password     : joi.string().min(6).max(12).required(),
     accesstype   : joi.string().allow(null).required(),
     createdby    : joi.string().allow(null).required(),
     createdon    : joi.date().raw().required(),
     isdeleted    : joi.required(),
     selctdMldId  : joi.required(),
     selctdTileId : joi.required(),
}),
searchUserSchema : joi.object({
    username       : joi.string().max(50).allow(null),
fork icon0
star icon0
watch icon0

+ 5 other calls in file

50
51
52
53
54
55
56
57
58
59
  role: Joi.string().min(3).max(30).required(),
  email: Joi.string().email().required(),
  password: Joi.string()
    .regex(/[a-zA-Z0-9]{3,30}/)
    .required(),
  // image: Joi.required()
});

const data = {
  username: "abcd1234",
fork icon0
star icon0
watch icon0

+ 4 other calls in file