joi
joi copied to clipboard
Validate and strip alternatives with links only.
Context
- node version: v16.15.0
- module version with issue: 17.6.0
- last module version without issue: ?
- environment (e.g. node, browser, native): node
- used with (e.g. hapi application, another framework, standalone, ...): hapi, standalone
- any other relevant information:
What are you trying to achieve or the steps to reproduce?
Have a set of alternative schemas (all) stripped correctly when using linked subschemas only.
validateSchema1
shows the expected behavior (link-less).
validateSchema3
shows the faulty behavior (links only).
validateSchema2
shows a workaround behavior (fake alternative).
import * as Joi from 'joi';
export const validateA = Joi.object({
a: Joi.any().required(),
}).id('validateA');
export const validateB = Joi.alternatives()
.try(Joi.object({ b: Joi.string().required() }))
.match('all')
.id('validateB');
export const validateSchema1 = Joi.alternatives(
validateA,
validateB
)
.match('all')
.id('validateSchema1');
export const validateSchema2 = Joi.alternatives(
Joi.object({}),
Joi.link('#validateA'),
Joi.link('#validateB')
)
.match('all')
.id('validateSchema2')
.shared(validateA)
.shared(validateB);
export const validateSchema3 = Joi.alternatives(
Joi.link('#validateA'),
Joi.link('#validateB')
)
.match('all')
.id('validateSchema3')
.shared(validateA)
.shared(validateB);
const v = {
a: 2,
b: 'b',
c: 'stripMe',
};
console.log(validateSchema1.validate(v, { stripUnknown: true }));
console.log(validateSchema2.validate(v, { stripUnknown: true }));
console.log(validateSchema3.validate(v, { stripUnknown: true }));
What was the result you got?
{ value: { a: 2, b: 'b' } }
{ value: { a: 2, b: 'b' } }
{ value: { b: 'b' } }
What result did you expect?
{ value: { a: 2, b: 'b' } }
{ value: { a: 2, b: 'b' } }
{ value: { a: 2, b: 'b' } }