Closed
Conversation
4a2de06 to
a9aa3e7
Compare
Contributor
|
Could we have It might be helpful for complex conditional schema. let's assume below scenario:
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())
});
// 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())
}); |
|
Starting testing on my lib. |
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Can someone verify if this makes things slower?