How to use the array function from joi

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

joi.array is a method in the Joi validation library used to specify validation rules for arrays in JavaScript.

11
12
13
14
15
16
17
18
19
20
})
  .label('Sum')
  .description('json body for sum');

const listModel = Joi.object({
  items: Joi.array().items(sumModel),
  count: Joi.number().required().example('1'),
  pageSize: Joi.number().required().example('10'),
  page: Joi.number().required().example('1'),
  pageCount: Joi.number().required().example('1')
fork icon425
star icon901
watch icon27

+ 3 other calls in file

59
60
61
62
63
64
65
66
67
68
jwtPrivateKey: joi.string().required(),
jwtPublicKey: joi.string().required(),
jwtQueueServicePublicKey: joi.string().required(),
authCheckById: JOI_BOOLEAN.default(true),
whitelist: joi.array().default([]),
allowList: joi.array().default([]),
admins: joi.array().default([]),
sdAdmins: joi.array().default([]),
bell: joi.object().required(),
scm: joi.object().required(),
fork icon163
star icon972
watch icon65

+ 15 other calls in file

How does joi.array work?

joi.array() is a function in the Joi validation library that allows you to define a schema for an array type input, specifying the types and requirements of its elements. When validating data, this function will check that the input is an array and that each element meets the requirements defined in the schema.

17
18
19
20
21
22
23
24
25
26

internals.configSchema = Joi.object().keys({
  hashKey   : Joi.string().required(),
  rangeKey  : Joi.string(),
  tableName : Joi.alternatives().try(Joi.string(), Joi.func()),
  indexes   : Joi.array().items(internals.secondaryIndexSchema),
  schema    : Joi.object(),
  timestamps : Joi.boolean().default(false),
  createdAt  : Joi.alternatives().try(Joi.string(), Joi.boolean()),
  updatedAt  : Joi.alternatives().try(Joi.string(), Joi.boolean())
fork icon87
star icon292
watch icon8

+ 15 other calls in file

91
92
93
94
95
96
97
98
99
100
query: Joi.object({}),
payload: Joi.object({
    name: Joi.string().required(),
    timeout: Joi.number().integer(),
    wait: Joi.number().integer(),
    ignore: Joi.array(),
    actions: Joi.array().items(Joi.string()),
    comment: Joi.string(),
    username: Joi.string().allow(''),
    password: Joi.string().allow(''),
fork icon60
star icon144
watch icon179

+ 3 other calls in file

Ai Example

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

const schema = Joi.array().items(Joi.number().integer().min(0));
const result = schema.validate([1, 2, 3, 4]);

if (result.error) {
  console.log(result.error.details);
} else {
  console.log("Validation successful");
}

In this example, we define a schema that uses Joi.array() to validate an array of numbers. We use the .items() method to specify that each element in the array should be a positive integer. We then call the validate() method on the schema and pass in an array of numbers. If the validation fails, the result object will contain an error property with information about the validation failure. If the validation succeeds, the result object will not contain an error property.

100
101
102
103
104
105
106
107
108
109
    }
});

internals.Joi = internals.Joi.extend({
    type: 'array',
    base: Joi.array(),
    messages: {
        'array.sorted': 'entries are not sorted by {{name}}'
    },
    rules: {
fork icon44
star icon264
watch icon26

+ 5 other calls in file

8
9
10
11
12
13
14
15
16
17
{ type: 'date-time', base: Joi.date().iso() },
{
    type: 'file',
    base: Joi.object({
        value: Joi.binary().required(true),
        consumes: Joi.array().items(
            Joi.string().regex(/multipart\/form-data|application\/x-www-form-urlencoded/)
        ).required(true),
        in: Joi.string().regex(/formData/).required(true)
    })
fork icon75
star icon207
watch icon21

13
14
15
16
17
18
19
20
21
22
branch: joi.string().default('master'),
entities: joi
  .array()
  .items(
    joi.string(),
    joi.array().items(joi.string(), joi.string()),
    joi.object().keys({
      i: joi.string().required(),
      o: joi.string().required(),
      options: joi.object().keys({
fork icon2
star icon37
watch icon2

+ 5 other calls in file

195
196
197
198
199
200
201
202
203

mstream.put("/api/v1/admin/users", async (req, res) => {
  const schema = Joi.object({
    username: Joi.string().required(),
    password: Joi.string().required(),
    vpaths: Joi.array().items(Joi.string()).required(),
    admin: Joi.boolean().optional().default(false)
  });
  const input = joiValidate(schema, req.body);
fork icon170
star icon0
watch icon45

+ 3 other calls in file

12
13
14
15
16
17
18
19
20
21
    type: Joi.string().label('The the Joi-type that categorizes the error').required()
});

const SCHEMA_OUTPUT = Joi.object()
    .keys({
        errors: Joi.array()
            .items(TEMPLATE_ERROR)
            .label('Array of errors encountered while validating the given template')
            .required(),
        // since a template could be parseable but invalid, the contents are unpredictable
fork icon15
star icon5
watch icon21

+ 5 other calls in file

139
140
141
142
143
144
145
146
147
148
'/webhooks',
tools.responseWrapper(async (req, res) => {
    res.charSet('utf-8');

    const schema = Joi.object().keys({
        type: Joi.array().items(Joi.string().trim().max(128).lowercase()).required(),
        user: Joi.string().hex().lowercase().length(24),
        url: Joi.string()
            .uri({ scheme: [/smtps?/, /https?/], allowRelative: false, relativeOnly: false })
            .required(),
fork icon245
star icon0
watch icon54

+ 3 other calls in file

62
63
64
65
66
67
68
69
70
    relsTemplate: joi.boolean().default(true),
    autoApi: joi.boolean().default(true),
    apiPath: joi.string().allow('').default('/api'),
    apiAuth: joi.alternatives().try(joi.boolean().allow(false),joi.object()).default(false),
    apiServerLabel: joi.string(),
    mediaTypes: joi.array().includes(joi.string()).single().default([HAL_MIME_TYPE]),
    requireHalJsonAcceptHeader: joi.boolean().default(false),
    marked: joi.object().default({})
};
fork icon23
star icon0
watch icon1

+ 3 other calls in file

7
8
9
10
11
12
13
14
15
const Joi = require('joi');
const util = require('util');
const WebSocket = require('final-rpc').WebSocket;

const API_CALL_ARRAY =
    Joi.array().items(Joi.object({
        name: Joi.string(),
        args: Joi.array().optional()
    }));
fork icon1
star icon21
watch icon6

+ 3 other calls in file

47
48
49
50
51
52
53
54
55
56
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(),
  license: joi.string().optional(),
  repository: (
  joi.object().optional()
    .keys({
fork icon816
star icon0
watch icon338

+ 7 other calls in file

208
209
210
211
212
213
214
215
216
217
res.charSet('utf-8');

const schema = Joi.object().keys({
    user: Joi.string().hex().lowercase().length(24).required(),
    description: Joi.string().trim().max(255).required(),
    scopes: Joi.array()
        .items(
            Joi.string()
                .valid(...consts.SCOPES, '*')
                .required()
fork icon245
star icon0
watch icon54

39
40
41
42
43
44
45
46
47
48
    })
    .allow('')
    .example('https://myservice.com/imap/webhooks')
    .description('Webhook URL'),

webhookEvents: Joi.array().items(Joi.string().max(256).example('messageNew')),

webhooksCustomHeaders: Joi.array()
    .items(
        Joi.object({
fork icon117
star icon0
watch icon16

+ 15 other calls in file

6
7
8
9
10
11
12
13
14
15

const oauthSchema = Joi.object({
    web: Joi.object({
        client_id: Joi.string().required(),
        client_secret: Joi.string().required(),
        redirect_uris: Joi.array().min(1).required(),
    }).unknown().required(),
});
const firebaseConfSchema = Joi.object({
    type: Joi.string().required(),
fork icon61
star icon86
watch icon0

+ 3 other calls in file

918
919
920
921
922
923
924
925
926
927
id: Joi.optionalId(),
selfService: Joi.boolean(),
selfServiceCopilot: Joi.string(),
confidentialityType: Joi.string(),
directProjectId: Joi.number(),
typeIds: Joi.array().items(Joi.optionalId()),
trackIds: Joi.array().items(Joi.optionalId()),
types: Joi.array().items(Joi.string()),
tracks: Joi.array().items(Joi.string()),
typeId: Joi.optionalId(),
fork icon44
star icon17
watch icon25

+ 475 other calls in file

86
87
88
89
90
91
92
93
94
95
96
}


createAttachment.schema = {
  currentUser: Joi.any(),
  challengeId: Joi.id(),
  attachments: Joi.array()
    .items(
      Joi.object().keys({
        name: Joi.string().required(),
        url: Joi.string().uri().required(),
fork icon44
star icon17
watch icon25

1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
},

failAction,

payload: Joi.object({
    accounts: Joi.array()
        .items(Joi.string().max(256))
        .default([])
        .example(['account-id-1', 'account-id-2'])
        .description('Request reconnect for listed accounts')
fork icon113
star icon0
watch icon16

+ 23 other calls in file

1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
try {
  const { error, value } = Joi.object({
    departSejourMotif: Joi.string().required(),
    departSejourAt: Joi.string().required(),
    departSejourMotifComment: Joi.string().optional().allow(null, ""),
    ids: Joi.array().items(Joi.string().required()).required(),
  })
    .unknown()
    .validate({ ...req.params, ...req.body }, { stripUnknown: true });
  if (error) {
fork icon4
star icon8
watch icon4

+ 9 other calls in file