How to use the assert function from joi

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

joi.assert is a function used for asserting that a given value meets the specified schema requirements.

1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
      name: 'test',
      type: 'test',
      pushEndpointExpired: false,
    },
  ];
  Joi.assert(res, route.options.response.schema);
});

it('should allow returning approximateLastAccessTime', () => {
  const route = getRoute(makeRoutes({}), '/account/devices');
fork icon206
star icon482
watch icon43

+ 2 other calls in file

216
217
218
219
220
221
222
223
224
225
  merge(config, defaultConfig, this.pkg().availityWorkflow, developerConfig);
}

// Validate workflow.js settings
try {
  Joi.assert(config, schema);
  this.configuration = config;
} catch (error) {
  const details = JSON.stringify(error.details, null, 2);
  const message = `Your workflow.js settings are invalid. See details below:\n${details}`;
fork icon17
star icon26
watch icon33

+ 8 other calls in file

How does joi.assert work?

joi.assert is a function in the Joi validation library that validates input data against a given schema and throws an error if the input data does not conform to the schema.

51
52
53
54
55
56
57
58
59
 *   @param {String} config.key key for the HAWK header
 *   @param {Number} config.timeout positive integer of the number of milliseconds to wait for a server to send response headers (passed as parameter of the same name to https://github.com/request/request)
 * @return {IPReputationServiceClient}
 */
var client = function(config) {
  Joi.assert(config, clientConfigSchema);

  var useStrictSSL = (url.parse(config.serviceUrl).protocol === 'https:');
  this.timeout = config.timeout || 30 * 1000;
fork icon8
star icon5
watch icon14

408
409
410
411
412
413
414
415
416
417
if (!fs.existsSync(RCFILE) || !fs.statSync(RCFILE).isFile(RCFILE))
    throw new AppErr(`'${RCFILE}' does not exist or is not a file`);
const snesExports = require("./exports");
if (!snesExports || !isPlainObject(snesExports))
    throw new AppErr("eslint-plugin-rc does not have valid 'exports.js'");
joi.assert(snesExports, joi.object({
    configs: joi.object({
        servicenow: joi.object({
            settings: joi.object({
                ootbTables: joi.object()
fork icon0
star icon0
watch icon1

Ai Example

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

const schema = Joi.object({
  username: Joi.string().alphanum().min(3).max(30).required(),
  password: Joi.string().pattern(new RegExp("^[a-zA-Z0-9]{3,30}$")).required(),
  repeat_password: Joi.ref("password"),
  access_token: [Joi.string(), Joi.number()],
  birth_year: Joi.number().integer().min(1900).max(2013),
  email: Joi.string().email({
    minDomainSegments: 2,
    tlds: { allow: ["com", "net"] },
  }),
})
  .with("username", "birth_year")
  .xor("password", "access_token")
  .with("password", "repeat_password");

const input = {
  username: "John",
  password: "password",
  repeat_password: "password",
  birth_year: 1980,
  email: "john@example.com",
};

// validate input against schema
Joi.assert(input, schema);