How to use the forbidden function from joi
Find comprehensive JavaScript joi.forbidden code examples handpicked from public code repositorys.
joi.forbidden is a method in the Joi validation library that disallows a specific key from appearing in an object schema.
GitHub: arangodb/arangodb
66 67 68 69 70 71 72 73 74 75
thumbnail: joi.string().optional(), // Compatibility engines: ( joi.object().optional() .pattern(RE_EMPTY, joi.forbidden()) .pattern(RE_NOT_EMPTY, joi.string().required()) ), // Index redirect
+ 11 other calls in file
GitHub: Twipped/joi-to-swagger
70 71 72 73 74 75 76 77 78 79 80
function schemaForAlternatives (alternatives, existingComponents, newComponentsByRef, mode) { let swaggers = []; for (const joiSchema of alternatives) { const { swagger, components } = parse(joiSchema, merge({}, existingComponents || {}, newComponentsByRef || {})); if (!swagger) continue; // swagger is falsy if joi.forbidden() if (get(joiSchema, '_flags.presence') === 'required') { swagger['x-required'] = true; } merge(newComponentsByRef, components || {});
+ 19 other calls in file
How does joi.forbidden work?
joi.forbidden works by disallowing a specific key from appearing in an object schema. When defining a schema with Joi, you can use the .forbidden method to specify a key that should not be allowed to appear in the schema. If an object that includes the forbidden key is validated against the schema, the validation will fail. For example, a schema that forbids the password key from appearing in an object could be defined as follows: javascript Copy code {{{{{{{ const Joi = require('joi'); const schema = Joi.object({ username: Joi.string(), email: Joi.string().email(), password: Joi.forbidden() }); In this example, the Joi.forbidden method disallows the password key from appearing in the schema. If an object that includes a password key is validated against this schema, the validation will fail. Note that joi.forbidden is part of the Joi library, which is a validation library for Node.js and the browser.
GitHub: nelsongomes/ts-openapi
284 285 286 287 288 289 290 291 292 293
const key = child.key; const { swagger: parsedSwagger, components } = parse( child.schema, combinedComponents ); /*if (!swagger) { // swagger is falsy if joi.forbidden() return; }*/ merge(newComponentsByRef, components || {});
+ 14 other calls in file
GitHub: postalsys/emailengine
3235 3236 3237 3238 3239 3240 3241 3242 3243 3244
.default(false) .description('If true, then includes attachments in forwarded message') .when('action', { is: 'forward', then: Joi.optional(), otherwise: Joi.forbidden() }) .label('ForwardAttachments'), ignoreMissing: Joi.boolean() .truthy('Y', 'true', '1')
+ 107 other calls in file
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
const Joi = require("joi"); const schema = Joi.object({ username: Joi.string(), email: Joi.string().email(), password: Joi.forbidden(), }); const validObject = { username: "john", email: "john@example.com" }; const invalidObject = { username: "john", email: "john@example.com", password: "secret", }; const { error: validError, value: validValue } = schema.validate(validObject); const { error: invalidError, value: invalidValue } = schema.validate(invalidObject); console.log(validError); // null console.log(validValue); // { username: 'john', email: 'john@example.com' } console.log(invalidError); // ValidationError: "password" is not allowed console.log(invalidValue); // undefined
In this example, we define a schema that disallows a password key from appearing in an object. We use the Joi.forbidden method to disallow the password key in the schema. We then validate two objects using the schema: one with only allowed keys ('username' and 'email'), and one with an additional key ('password') that is disallowed. 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 password key is not allowed. Note that joi.forbidden can be used to disallow any key in an object schema, and can be combined with other Joi methods to create more complex validation rules.
35 36 37 38 39 40 41 42 43 44 45
var queueCache = {}; var jobCache = {}; function validate (data, schema) { if (!schema) { schema = joi.forbidden(); } var raw = data; var isTuple = Boolean( schema._meta && schema._meta.some((meta) => meta.isTuple)
+ 3 other calls in file
29 30 31 32 33 34 35 36 37 38
//? Joi Schema for User const authSchema = Joi.object({ email: Joi.string().email().label("Email").when("signup", { is: 1, then: Joi.required(), otherwise: Joi.forbidden(), }), accessKey: Joi.string().label("Access Key").required(), loginWithOther: Joi.number().label("Login with Other").when("signup", { is: 1,
+ 9 other calls in file
96 97 98 99 100 101 102 103 104 105
for (const joiSchema of alternatives) { const { swagger, components } = parse( joiSchema, merge({}, existingComponents || {}, newComponentsByRef || {}) ) if (!swagger) continue // swagger is falsy if joi.forbidden() if (get(joiSchema, '_flags.presence') === 'required') { swagger['x-required'] = true } merge(newComponentsByRef, components || {})
+ 2 other calls in file
287 288 289 290 291 292 293 294 295 296
var children = get(schema, '_inner.children') || []; children.forEach((child) => { var key = child.key; var prop = exports(child.schema, combinedComponents); if (!prop.swagger) { // swagger is falsy if joi.forbidden() return; } merge(newComponentsByRef, prop.components || {});
joi.string is the most popular function in joi (40578 examples)