Skip to content

.pattern doesn't respect .valid when using .ref #1748

@crazyfox55

Description

@crazyfox55

Context

const schema = Joi.object({
	Strengths: Joi.object().pattern(
		Joi.string().valid(Joi.ref('$StrengthOptions')),
		Joi.number().integer().greator(0).allow(0)
	).default({})
})

What are you trying to achieve or the steps to reproduce ?

Trying to validate that an object only has keys from a specific set. However the set is dynamic. The valid function works if just using a static list i.e ['coding', 'reviewing'].

const strengthNames: string[] = *Call to get names of possible strengths*
const options: Joi.ValidationOptions = {
	abortEarly: false,
	convert: true,
	allowUnknown: false,
	context: {
		StrengthOptions: strengthNames
	}
}
Joi.validate(
	someObj,
	schema,
	options,
	(errors: ValidationError, value:any) => {
		if(errors){
			console.log(error);
		}
	}
);

Which result you had ?

{
message: '"coding" is not allowed',
path: [ 'Strengths', 'coding' ],
type: 'object.allowUnknown',
context: { child: 'coding', value: 3, key: 'coding', label: 'coding' }
}

What did you expect ?

"coding" should be allowed it is in the strengthNames array.
context of the error should also contatin valids: [ array ]
type of error should be 'any.allowOnly'

Metadata

Metadata

Assignees

Labels

bugBug or defect

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions