How to use the joi.when function in joi

To help you get started, we’ve selected a few 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 Twipped / joi-to-swagger / tests.js View on Github external
joi.array().items(joi.string(), joi.number()).meta({ swaggerIndex: 1 }).min(1).max(5),
		{
			type: 'array',
			items: { type: 'number', format: 'float' },
			minItems: 1,
			maxItems: 5,
		}
	);

	simpleTest(
		joi.alternatives(joi.string(), joi.number()).meta({ swaggerIndex: 1 }),
		{ type: 'number', format: 'float' }
	);

	simpleTest(
		joi.when('myRequiredField', {
			is: true,
			then: joi.string(),
			otherwise: joi.number(),
		}),
		{ type: 'string' }
	);

	simpleTest(
		joi.when('myRequiredField', {
			is: true,
			then: joi.string(),
			otherwise: joi.number(),
		}).meta({ swaggerIndex: 1 }),
		{ type: 'number', format: 'float' }
	);
github vudash / vudash / packages / widget-ci / src / server / validation.js View on Github external
const engineFactory = require('../engines/factory')
const Joi = require('joi')

module.exports = {
  repo: Joi.string().required().description('Repository Name'),
  user: Joi.string().required().description('Account/Organisation Name'),
  branch: Joi.string().optional().description('Branch name'),
  schedule: Joi.number().optional().default(60000).description('Update frequency (ms)'),
  provider: Joi.string().required().only(engineFactory.availableEngines).description('CI Provider name'),
  hideOwner: Joi.boolean().optional().default(false).description('Hide repo owner from display'),
  sounds: Joi.object({
    passed: Joi.string().optional().description('Sound to play on build pass'),
    failed: Joi.string().optional().description('Sound to play on build fail'),
    unknown: Joi.string().optional().description('Sound to play on unknown state')
  }).optional().description('Sounds to play when build status changes'),
  options: Joi.when('provider', {
    is: 'circleci',
    then: Joi.object({
      auth: Joi.string().required().description('CircleCI auth token')
    }).required(),
    otherwise: Joi.forbidden()
  })
}
github microsoft / pai / rest-server / src / config / template.js View on Github external
const templateTypes = ['data', 'dockerimage', 'job', 'script'];

const templateInputSchema = Joi.object().keys({
  protocol_version: Joi.string()
    .allow('')
    .default('v2'),
  name: Joi.string()
    .required(),
  type: Joi.string()
    .valid(templateTypes),
  contributor: Joi.string()
    .required(),
  description: Joi.string()
    .allow('')
    .default(''),
  version: Joi.when('type', {
      is: 'job',
      then: Joi.string(),
      otherwise: Joi.string().required(),
    }),
  uri: Joi.when('type', {
      is: 'job',
      then: Joi.forbidden(),
      otherwise: Joi.string().required(),
    }),
  usage: Joi.when('type', {
    is: 'script',
    then: Joi.object(),
    otherwise: Joi.forbidden(),
  }),
  prerequisites: Joi.when('type', {
    is: 'job',
github microsoft / pai / rest-server / src / config / template.js View on Github external
version: Joi.when('type', {
      is: 'job',
      then: Joi.string(),
      otherwise: Joi.string().required(),
    }),
  uri: Joi.when('type', {
      is: 'job',
      then: Joi.forbidden(),
      otherwise: Joi.string().required(),
    }),
  usage: Joi.when('type', {
    is: 'script',
    then: Joi.object(),
    otherwise: Joi.forbidden(),
  }),
  prerequisites: Joi.when('type', {
    is: 'job',
    then: Joi.array().required(),
    otherwise: Joi.forbidden(),
  }),
  tasks: Joi.when('type', {
    is: 'job',
    then: Joi.array().required(),
    otherwise: Joi.forbidden(),
  }),
  parameters: Joi
    .when('type', {
      is: 'data',
      then: Joi.forbidden(),
    })
    .when('type', {
      is: 'dockerimage',
github jsdmc / react-redux-router-crud-example / components / pages / ContractForm.js View on Github external
constructor(props, context) {
    super(props, context);
    
    this.actions = bindActionCreators(ContractActions, this.props.dispatch);

    //TODO: modify validation for nested properties
    this.validatorTypes = {
      id: Joi.number().required().label('Contract Id'),
      description: Joi.when('finished', { is: true, then: Joi.string().min(5).max(25).required() }).label('Description'),
      finished: Joi.boolean().label('Finished')
    }

    this.state = this.props.contract ? this.props.contract : this.initialState;
  }
github microsoft / pai / rest-server / src / config / template.js View on Github external
contributor: Joi.string()
    .required(),
  description: Joi.string()
    .allow('')
    .default(''),
  version: Joi.when('type', {
      is: 'job',
      then: Joi.string(),
      otherwise: Joi.string().required(),
    }),
  uri: Joi.when('type', {
      is: 'job',
      then: Joi.forbidden(),
      otherwise: Joi.string().required(),
    }),
  usage: Joi.when('type', {
    is: 'script',
    then: Joi.object(),
    otherwise: Joi.forbidden(),
  }),
  prerequisites: Joi.when('type', {
    is: 'job',
    then: Joi.array().required(),
    otherwise: Joi.forbidden(),
  }),
  tasks: Joi.when('type', {
    is: 'job',
    then: Joi.array().required(),
    otherwise: Joi.forbidden(),
  }),
  parameters: Joi
    .when('type', {
github freeCodeCamp / freeCodeCamp / curriculum / schema / challengeSchema.js View on Github external
function getSchemaForLang(lang) {
  let schema = Joi.object().keys({
    block: Joi.string(),
    blockId: Joi.objectId(),
    challengeOrder: Joi.number(),
    challengeType: Joi.number()
      .min(0)
      .max(9)
      .required(),
    checksum: Joi.number(),
    dashedName: Joi.string(),
    description: Joi.when('challengeType', {
      is: challengeTypes.step,
      then: Joi.string().allow(''),
      otherwise: Joi.string().required()
    }),
    fileName: Joi.string(),
    files: Joi.array().items(
      Joi.object().keys({
        key: Joi.string(),
        ext: Joi.string(),
        name: Joi.string(),
        head: [
          Joi.array().items(Joi.string().allow('')),
          Joi.string().allow('')
        ],
        tail: [
          Joi.array().items(Joi.string().allow('')),
github robertu7 / Oneline / routes / helper / schema / helpers / srcLink.js View on Github external
const Joi = require('joi');

const linkSchema = require('./link');

module.exports = Joi.when('provider', {
    is       : 'instagram',
    then     : linkSchema,
    otherwise: Joi.forbidden(),
});
github Ethercast / serverless-blockstream / src / util / joi-schema.ts View on Github external
const JoiBlockWithTransactions = JoiBlock.keys({
  transactions: array().items(JoiTransaction).required()
});

const JoiTransactionReceipt = object({
  transactionHash: hex256.required(),
  transactionIndex: hex.required(),
  blockNumber: hex.required(),
  blockHash: hex256.required(),
  cumulativeGasUsed: hex.required(),
  gasUsed: hex.required(),
  logs: array().items(JoiLog).required(),
  contractAddress: address.allow(null).required(),
  from: address.required(),
  to: when(
    'contractAddress',
    {
      is: null,
      then: address,
      otherwise: allow(null)
    }
  ).required(),
  logsBloom: hex.required(),
  status: alternatives().valid('0x0', '0x1')
});

export function mustBeValidLog(log: Log): Log {
  return validate(log, JoiLog);
}

export function mustBeValidBlockWithTransactionHashes(block: BlockWithTransactionHashes): BlockWithTransactionHashes {
github robertu7 / Oneline / routes / helper / schema / helpers / media.js View on Github external
video_url: linkSchema.optional(),
            ratio    : Joi.number().required(),
        })).optional(),
    })
    .when('provider', {
        is  : 'weibo',
        then: Joi.array().items(Joi.object({
            type     : Joi.string().valid('gif', 'photo').required(),
            image_url: linkSchema,
        })).optional(),
        otherwise: Joi.forbidden(),
    })
);

exports.images = (
    Joi
    .when('provider', {
        is  : 'unsplash',
        then: Joi.object({
            small: linkSchema,
            large: linkSchema,
            ratio: Joi.number().required(),
        }).required(),
        otherwise: Joi.forbidden(),
    })
);