Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
crazyfox55 opened this issue Mar 12, 2019 · 3 comments
Closed

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

crazyfox55 opened this issue Mar 12, 2019 · 3 comments
Assignees
Labels
bug Bug or defect
Milestone

Comments

@crazyfox55
Copy link

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'

@WesTyler WesTyler added bug hunt bug Bug or defect and removed bug hunt labels Mar 13, 2019
@WesTyler
Copy link
Contributor

Confirmed and found a fix. PR incoming

@crazyfox55
Copy link
Author

That is great news! Thanks.

@Marsup Marsup added this to the 15.0.1 milestone Apr 12, 2019
@Marsup Marsup closed this as completed Apr 12, 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
bug Bug or defect
Projects
None yet
Development

No branches or pull requests

3 participants