How to use the optional function from joi

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

joi.optional is a method in the Joi library that allows a schema to optionally accept a value or be undefined.

7
8
9
10
11
12
13
14
15
    accountId: joi.string().required(),
    archiveInfo: joi.object().required(),
    requestId: joi.string().required(),

    originalMessage: joi.object(),
    reason: joi.optional(),
    date: joi.date().optional(),
    eTag: joi.string().optional(),
}).unknown(true);
fork icon18
star icon51
watch icon60

225
226
227
228
229
230
231
232
233
234
    .example('My Gmail App')
    .description('Application name')
    .when('app', {
        not: Joi.string().valid(...LEGACY_KEYS),
        then: Joi.required(),
        otherwise: Joi.optional().valid(false, null)
    }),
description: Joi.string().trim().empty('').max(1024).example('My cool app').description('Application description'),

title: Joi.string().empty('').trim().max(256).example('App title').description('Title for the application button'),
fork icon113
star icon0
watch icon16

+ 104 other calls in file

How does joi.optional work?

Sure! joi.optional is a method in the Joi library that allows a schema to optionally accept a value or be undefined. The joi.optional method takes a single argument, which is the schema to be made optional. It returns a new schema that accepts either the original schema or undefined. Here's an example of how to use joi.optional to make a schema optional: javascript Copy code {{{{{{{ const schema = Joi.object({ name: Joi.string(), age: Joi.optional(Joi.number()), }); const { error, value } = schema.validate({ name: 'John' }); console.log(value); // { name: 'John' } In this example, we create a new Joi.object schema with two properties: name and age. The name property is defined as a Joi.string schema, which means it must be a string. The age property is defined as an optional Joi.number schema, which means it can be either a number or undefined. We then validate an object with only the name property. Since the age property is optional, it does not need to be present in the object. The validate method of the schema is called with the object to validate. The error variable will be undefined if the object is valid, and the value variable will contain the validated object. In this case, the value variable will contain only the name property, since the age property was not provided in the input object. This code demonstrates how to use joi.optional to make a schema optional in the Joi library.

321
322
323
324
325
326
327
328
329
330
    pass: Joi.string()
        .allow('')
        .max(256)
        .when('accessToken', {
            is: Joi.exist().not(false, null),
            then: Joi.optional().valid(false, null),
            otherwise: Joi.required()
        }),
    accessToken: Joi.string().max(4 * 4096)
});
fork icon113
star icon0
watch icon16

+ 2 other calls in file

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

+ 39 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
const Joi = require("joi");

const schema = Joi.object({
  name: Joi.string(),
  age: Joi.optional(Joi.number()),
});

const { error, value } = schema.validate({ name: "John" });

console.log(value); // { name: 'John' }

In this example, we create a new Joi.object schema with two properties: name and age. The name property is defined as a Joi.string schema, which means it must be a string. The age property is defined as an optional Joi.number schema, which means it can be either a number or undefined. We then validate an object with only the name property. Since the age property is optional, it does not need to be present in the object. The validate method of the schema is called with the object to validate. The error variable will be undefined if the object is valid, and the value variable will contain the validated object. In this case, the value variable will contain only the name property, since the age property was not provided in the input object. This code demonstrates how to use joi.optional to make a schema optional in the Joi library.

460
461
462
463
464
465
466
467
468
469
  .boolean()
  .optional()
  .meta({ tags: ['Restricted'] })
  .when('status', {
    is: CASE_STATUS_TYPES.calendared,
    otherwise: joi.optional(),
    then: joi.invalid(true),
  })
  .description('Temporarily blocked from trial.'),
blockedDate: JoiValidationConstants.ISO_DATE.when('blocked', {
fork icon36
star icon72
watch icon19

+ 43 other calls in file

41
42
43
44
45
46
47
48
49
50
}),
accountName: Joi.string().label("Account Name").required(),
userType: Joi.number().label("User Type").valid(285, 6001).when("signup", {
  is: 1,
  then: Joi.required(),
  otherwise: Joi.optional(),
}),
businessName: Joi.string()
  .label("Business Name")
  .optional()
fork icon0
star icon0
watch icon1

+ 4 other calls in file