Skip to content

Reference above parent. Closes #1650#1651

Closed
hueniverse wants to merge 2 commits intov15from
parents
Closed

Reference above parent. Closes #1650#1651
hueniverse wants to merge 2 commits intov15from
parents

Conversation

@hueniverse
Copy link
Contributor

@hueniverse hueniverse commented Nov 14, 2018

Can someone verify if this makes things slower?

@hueniverse hueniverse added feature New functionality or improvement breaking changes Change that can breaking existing code labels Nov 14, 2018
@Marsup Marsup force-pushed the master branch 2 times, most recently from 4a2de06 to a9aa3e7 Compare November 19, 2018 21:17
@Marsup Marsup added this to the 15.0.0 milestone Nov 19, 2018
@Marsup Marsup changed the base branch from master to v15 November 19, 2018 22:19
@rokoroku
Copy link
Contributor

Could we have Joi.ref(null) ( or Joi.ref('.')) or something that returns the referenced context value itself?

It might be helpful for complex conditional schema. let's assume below scenario:

  1. Thing was simple when we just have single condition
Joi.object({
  type: Joi.string().valid('number', 'string', 'boolean'),
  requireDefault: Joi.boolean().default(false),
  defaultValue: Joi.when('type', { is: 'number', then: Joi.number().default(1) })
    .when('type', { is: 'string', then: Joi.string().default('string') })
    .when('type', { is: 'boolean', then: Joi.boolean().default(true) })
    .try(Joi.strip())
});
  1. When we have multiple conditions, we have to write complex schema like below:
// solution 1, best choice but got another complexity
Joi.object({
  type: Joi.string().valid('number', 'string', 'boolean'),
  requireDefault: Joi.boolean().default(false),
  defaultValue: Joi.when('requireDefault', {
    is: true,
    then: Joi.when('..type', { is: 'number', then: Joi.number().default(1) })
      .when('..type', { is: 'string', then: Joi.string().default('string') })
      .when('..type', { is: 'boolean', then: Joi.boolean().default(true) }),
    otherwise: Joi.strip()
  })
});

// solution 2, readability is getting low 
// it also changes the base schema to 'alternatives' and breaks Joi.reach(...) functionality
Joi.object({
  type: Joi.string().valid('number', 'string', 'boolean'),
  requireDefault: Joi.boolean().default(false)
})
  .when(Joi.object({ type: 'number', requireDefault: true }).options({ presence: 'required', allowUnknown: true }), {
    then: { defaultValue: Joi.number().default(1) }
  })
  .when(Joi.object({ type: 'string', requireDefault: true }).options({ presence: 'required', allowUnknown: true }), {
    then: { defaultValue: Joi.string().default('string') }
  })
  .when(Joi.object({ type: 'boolean', requireDefault: true }).options({ presence: 'required', allowUnknown: true }), {
    then: { defaultValue: Joi.boolean().default(true) },
    otherwise: { defaultValue: Joi.strip() }
  });

So, rather than writing complex conditions or changing it's base type, we can achieve single schema condition if we have self-reference feature:

// suggest:
// if we have self-returning reference e.g. Joi.ref('.'),
// we can reduce complexity without changing base schema
Joi.object({
  type: Joi.string().valid('number', 'string', 'boolean'),
  requireDefault: Joi.boolean().default(false),
  defaultValue: Joi.when('.', {
    is: Joi.object({ type: 'number', requireDefault: true }).options({ presence: 'required', allowUnknown: true }),
    then: Joi.number().default(1)
  })
    .when('.', {
      is: Joi.object({ type: 'number', requireDefault: true }).options({ presence: 'required', allowUnknown: true }),
      then: Joi.string().default('string')
    })
    .when('.', {
      is: Joi.object({ type: 'number', requireDefault: true }).options({ presence: 'required', allowUnknown: true }),
      then: Joi.boolean().default(true)
    })
    .try(Joi.strip())
});

@LucasBadico
Copy link

Starting testing on my lib.

@hueniverse hueniverse closed this Apr 2, 2019
@hueniverse hueniverse deleted the parents branch May 31, 2019 08:37
@hueniverse hueniverse self-assigned this Jun 16, 2019
@hueniverse hueniverse added the v16 label Aug 10, 2019
@lock
Copy link

lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

breaking changes Change that can breaking existing code feature New functionality or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants