How to use the only function from joi

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

joi.only is a method in the Joi validation library that restricts the allowed values for a schema to a specific set of values.

43
44
45
46
47
48
49
50
51
52
configTypes.password = configTypes.string;
configTypes.int = configTypes.integer;
configTypes.bool = configTypes.boolean;

const manifestSchema = {
  $schema: joi.only(CANONICAL_SCHEMA).default(CANONICAL_SCHEMA),
  // FoxxStore metadata
  name: joi.string().regex(/^[-_a-z][-_a-z0-9]*$/i).optional(),
  version: joi.string().optional(),
  keywords: joi.array().optional(),
fork icon816
star icon0
watch icon338

+ 3 other calls in file

How does joi.only work?

joi.only works by restricting the allowed values for a schema to a specific set of values. When defining a schema with Joi, you can use the .only method to specify a set of allowed values for a given key. If a value for that key is provided during validation that is not one of the allowed values, the validation will fail. For example, a schema that only allows the values 'male' and 'female' for a gender key could be defined as follows: javascript Copy code {{{{{{{ const Joi = require('joi'); const schema = Joi.object({ gender: Joi.only('male', 'female') }); In this example, the Joi.only method restricts the allowed values for the gender key to 'male' and 'female'. When the schema is used to validate an object, any object that does not have a gender key or has a gender value that is not 'male' or 'female' will fail validation. Note that joi.only is part of the Joi library, which is a validation library for Node.js and the browser.

Ai Example

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

const validStatusValues = ["open", "in progress", "closed"];

const schema = Joi.object({
  status: Joi.only(...validStatusValues).required(),
});

const validObject = { status: "open" };
const invalidObject = { status: "pending" };

const { error: validError, value: validValue } = schema.validate(validObject);
const { error: invalidError, value: invalidValue } =
  schema.validate(invalidObject);

console.log(validError); // null
console.log(validValue); // { status: 'open' }

console.log(invalidError); // ValidationError: "status" must be one of [open, in progress, closed]
console.log(invalidValue); // undefined

In this example, we define a schema that allows only three specific values for a status key: 'open', 'in progress', and 'closed'. We create a validStatusValues array with the allowed values, and use the Joi.only method to restrict the allowed values for the status key in the schema. We then validate two objects using the schema: one with a valid status value ('open'), and one with an invalid status value ('pending'). The result of the validation is logged to the console, showing that the first object passes validation with no errors, while the second object fails with a ValidationError that indicates the status value is not allowed. Note that joi.only can be used to restrict the allowed values for any schema key, and can be combined with other Joi methods to create more complex validation rules.