How to use the @hapi/joi.required function in @hapi/joi

To help you get started, we’ve selected a few @hapi/joi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github hapijs / hapi / test / route.js View on Github external
it('ignores validation on * route when request is GET', async () => {

        const server = Hapi.server();
        server.route({ method: '*', path: '/', handler: () => null, options: { validate: { payload: { a: Joi.required() } } } });
        const res = await server.inject('/');
        expect(res.statusCode).to.equal(204);
    });
github superchargejs / framework / test / integration / foundation / http / kernel.js View on Github external
async serialThrowsWithCustomFailAction (t) {
    const request = this.addRoute({
      method: 'GET',
      path: '/kernel-test-failAction',
      options: {
        handler: () => { return 'not called' },
        validate: {
          query: Joi.object({
            name: Joi.required()
          })
        },
        ext: {
          onPreResponse: {
            method: async (request, h) => {
              t.true(Object.keys(request.response.data).includes('name'))

              return h.continue
            }
          }
        }
      }
    })

    const response = await request.get('/kernel-test-failAction')
github ccarruitero / makemehapi / exercises / validation_using_joi_object / solution / solution.js View on Github external
(async () => {
  try {
    const server = Hapi.Server({
      host: 'localhost',
      port: Number(process.argv[2] || 8080)
    });

    server.route({
      method: 'POST',
      path: '/login',
      config: {
        handler: (request, h) => 'login successful',
        validate: {
          payload: Joi.object({
            isGuest: Joi.boolean().required(),
            username: Joi.string().when('isGuest', { is: false, then: Joi.required() }),
            password: Joi.string().alphanum(),
            accessToken: Joi.string().alphanum()
          }).options({ allowUnknown: true }).without('password', 'accessToken')
        }
      }
    });

    await server.start();
  } catch (error) {
    console.log(error);
  }
})();
github ccarruitero / makemehapi / exercises / validation_using_joi_object / solution_fr / solution.js View on Github external
(async () => {
  try {
    const server = Hapi.Server({
      host: 'localhost',
      port: Number(process.argv[2] || 8080)
    });

    server.route({
      method: 'POST',
      path: '/login',
      config: {
        handler: (request, h) => 'authentification réussie',
        validate: {
          payload: Joi.object({
            isGuest: Joi.boolean().required(),
            username: Joi.string().when('isGuest', { is: false, then: Joi.required() }),
            password: Joi.string().alphanum(),
            accessToken: Joi.string().alphanum()
          }).options({ allowUnknown: true }).without('password', 'accessToken')
        }
      }
    });

    await server.start();
  } catch (error) {
    console.log(error);
  }
})();
github bkimminich / juice-shop-ctf / lib / readConfigStream.js View on Github external
const yaml = require('js-yaml')
const Joi = require('@hapi/joi')
const options = require('./options')
const schema = Joi.object().keys({
  ctfFramework: Joi.string().optional().valid(options.ctfdFramework, options.fbctfFramework, options.rtbFramework),
  juiceShopUrl: [Joi.string().uri().required(), Joi.string().ip().required()],
  countryMapping: Joi.string().when('ctfFramework', { is: options.fbctfFramework, then: Joi.required(), otherwise: Joi.optional() }),
  ctfKey: Joi.string().required(),
  insertHints: Joi.any().valid('none', 'free', 'paid').required(),
  insertHintUrls: Joi.any().valid('none', 'free', 'paid').when('ctfFramework', { is: options.fbctfFramework, then: Joi.optional(), otherwise: Joi.required() })
})

const hintsMap = { none: 0, free: 1, paid: 2 }

function readConfigStream (stream) {
  return new Promise((resolve, reject) => {
    let data = ''
    stream.on('data', (chunk) => {
      data = data + chunk
    })
    stream.on('end', () => {
      try {
        yaml.safeLoadAll(data, (doc) => {
github scality / backbeat / lib / config.joi.js View on Github external
intervalS: joi.number().default(60),
        },
    },
    transport: transportJoi,
    s3: hostPortJoi.required(),
    queuePopulator: {
        cronRule: joi.string().required(),
        batchMaxRead: joi.number().default(10000),
        batchTimeoutMs: joi.number().default(9000),
        zookeeperPath: joi.string().required(),

        logSource: joi.alternatives().try(logSourcesJoi).required(),
        subscribeToLogSourceDispatcher: joi.boolean().default(false),
        bucketd: hostPortJoi
            .keys({ transport: transportJoi })
            .when('logSource', { is: 'bucketd', then: joi.required() }),
        dmd: hostPortJoi.keys({
            logName: joi.string().default('s3-recordlog'),
        }).when('logSource', { is: 'dmd', then: joi.required() }),
        mongo: joi.object({
            replicaSetHosts: joi.string().default('localhost:27017'),
            logName: joi.string().default('s3-recordlog'),
            writeConcern: joi.string().default('majority'),
            replicaSet: joi.string().default('rs0'),
            readPreference: joi.string().default('primary'),
            database: joi.string().default('metadata'),
            authCredentials: joi.object({
                username: joi.string().required(),
                password: joi.string().required(),
            }),
        }),
    },
github hagopj13 / node-express-mongoose-boilerplate / src / validations / user.validation.js View on Github external
role: Joi.string(),
    sortBy: Joi.string(),
    limit: Joi.number().integer(),
    page: Joi.number().integer(),
  }),
};

const getUser = {
  params: Joi.object().keys({
    userId: Joi.string().custom(objectId),
  }),
};

const updateUser = {
  params: Joi.object().keys({
    userId: Joi.required().custom(objectId),
  }),
  body: Joi.object()
    .keys({
      email: Joi.string().email(),
      password: Joi.string().custom(password),
      name: Joi.string(),
    })
    .min(1),
};

const deleteUser = {
  params: Joi.object().keys({
    userId: Joi.string().custom(objectId),
  }),
};
github hapijs / hapi / lib / config.js View on Github external
routes: Joi.object({
        prefix: Joi.string().pattern(/^\/.+/),
        vhost: internals.vhost
    })
        .default({})
});


internals.semver = Joi.string();


internals.plugin = internals.register.keys({
    options: Joi.any(),
    plugin: Joi.object({
        register: Joi.function().required(),
        name: Joi.string().when('pkg.name', { is: Joi.exist(), otherwise: Joi.required() }),
        version: Joi.string(),
        multiple: Joi.boolean().default(false),
        dependencies: [
            Joi.array().items(Joi.string()).single(),
            Joi.object().pattern(/.+/, internals.semver)
        ],
        once: true,
        requirements: Joi.object({
            hapi: Joi.string(),
            node: Joi.string()
        })
            .default(),
        pkg: Joi.object({
            name: Joi.string(),
            version: Joi.string().default('0.0.0')
        })
github hapijs / bell / lib / index.js View on Github external
hapi: '>=18.3.0'
    },

    register: function (server, options) {

        server.auth.scheme('bell', internals.implementation);
        server.expose('oauth', OAuth);
    }
};


internals.schema = Joi.object({
    provider: Joi.object({
        name: Joi.string().optional().default('custom'),
        protocol: Joi.string().valid('oauth', 'oauth2'),
        temporary: Joi.string().when('protocol', { is: 'oauth', then: Joi.required(), otherwise: Joi.forbidden() }),
        signatureMethod: Joi.string().valid('HMAC-SHA1', 'RSA-SHA1').when('protocol', { is: 'oauth', then: Joi.default('HMAC-SHA1'), otherwise: Joi.forbidden() }),
        auth: Joi.string().required(),
        useParamsAuth: internals.flexBoolean.default(false).when('protocol', { is: 'oauth2', then: Joi.optional(), otherwise: Joi.forbidden() }),
        token: Joi.string().required(),
        headers: Joi.object(),
        profile: Joi.func(),
        profileMethod: Joi.string().valid('get', 'post').default('get'),
        scope: Joi.alternatives().try(
            Joi.array().items(Joi.string()),
            Joi.func()
        ).when('protocol', { is: 'oauth2', otherwise: Joi.forbidden() }),
        scopeSeparator: Joi.string().when('protocol', { is: 'oauth2', otherwise: Joi.forbidden() })
    }).required(),
    password: Joi.string().required(),
    clientId: Joi.string().required(),
    clientSecret: Joi.alternatives().when('protocol', {
github GoogleChrome / workbox / packages / workbox-build / src / options / partials / generate.js View on Github external
headers: joi.object(),
      }).or('statuses', 'headers'),
      cacheName: joi.string(),
      expiration: joi.object().keys({
        maxEntries: joi.number().min(1),
        maxAgeSeconds: joi.number().min(1),
        purgeOnQuotaError: joi.boolean().default(defaults.purgeOnQuotaError),
      }).or('maxEntries', 'maxAgeSeconds'),
      networkTimeoutSeconds: joi.number().min(1),
      plugins: joi.array().items(joi.object()),
      fetchOptions: joi.object(),
      matchOptions: joi.object(),
    }).with('expiration', 'cacheName'),
  }).requiredKeys('urlPattern', 'handler')).when('navigationPreload', {
    is: true,
    then: joi.required(),
  }),
  skipWaiting: joi.boolean().default(defaults.skipWaiting),
  sourcemap: joi.boolean().default(defaults.sourcemap),
};