How to use the allow function from joi

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

joi.allow specifies the allowed values for a schema property.

583
584
585
586
587
588
589
590
591
592
  .allow(null)
  .description('Litigation costs for the case.'),
mailingDate: JoiValidationConstants.STRING.max(25)
  .when('isPaper', {
    is: true,
    otherwise: joi.allow(null).optional(),
    then: joi.required(),
  })
  .description('Date that petition was mailed to the court.'),
noticeOfAttachments: joi
fork icon36
star icon72
watch icon19

+ 7 other calls in file

30
31
32
33
34
35
36
37
38
39
40
const joiToJsonSchema = require('joi-to-json-schema');
const tokenize = require('@avocadodb/foxx/router/tokenize');


const MIME_JSON = 'application/json; charset=utf-8';
const DEFAULT_ERROR_SCHEMA = joi.object().keys({
  error: joi.allow(true).required(),
  errorNum: joi.number().integer().optional(),
  errorMessage: joi.string().optional(),
  code: joi.number().integer().optional()
});
fork icon3
star icon0
watch icon0

How does joi.allow work?

joi.allow is a validation method provided by the joi library in Node.js which is used to specify the allowed values for a schema property. The joi.allow method takes one or more arguments which represent the allowed values for the schema property. If the value of the property matches any of the allowed values, the validation succeeds, otherwise, it fails. For example, the following code creates a schema with a property status that only allows values "active" or "inactive": php Copy code {{{{{{{ class="!whitespace-pre hljs language-php">const Joi = require('joi'); const schema = Joi.object({ status: Joi.string().allow('active', 'inactive') }); In this example, Joi.string() is used to create a string schema and allow is used to specify the allowed values for the status property. The joi.allow method can also be used with other validation methods such as Joi.number(), Joi.boolean(), and so on.

Ai Example

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

const schema = Joi.object({
  name: Joi.string().required(),
  age: Joi.number().integer().min(18).max(99),
  gender: Joi.string().valid("male", "female", "other").allow(null),
});

const user = {
  name: "John Doe",
  age: 25,
  gender: null,
};

const { error, value } = schema.validate(user);

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

In this example, we define a schema object using Joi.object, which has three properties: name, age, and gender. The gender property is defined using Joi.string().valid('male', 'female', 'other').allow(null), which specifies that the value can be one of the strings "male", "female", or "other", or it can be null. We then define a user object that matches the schema, and call schema.validate(user) to validate the object against the schema. If the object is valid, value will contain the validated object, otherwise error will contain an error object with details on the validation failure.