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
Joi.object().xor() throws with "must be a string or a reference" when passed a reference #2471
Comments
@cdhowie Well spotted! I will label it as a bug for now but the impact of it is low. |
I think you misunderstand what I'm trying to accomplish with this code. In other words, the attribute name that is required comes from the validated object, not the schema, but it comes from a known key. That is why I was trying to use #2532 implies that this is simply not supported -- which is fine. If this is a feature that would be considered for inclusion, I can open a new issue as a feature request. Currently a two-phase approach is required where the value of |
@cdhowie Sorry I'm a bit confused on what the input data should look like. Can you please provide some inputs of what should pass, what shouldn't and expected outputs? |
@cdhowie Given the information, I'd assume you want the string that const Joi = require('./lib');
const child = Joi.object().required();
const matches = Joi.array().items(
Joi.valid(Joi.ref('....defaultTag')).required(),
Joi.any(), // Object keys are always string so no need to validate
);
const schema = Joi.object({
defaultTag: Joi.string().required(),
tags: Joi.object()
.pattern(/./, child, { matches })
});
schema.validate({
defaultTag: 'test',
tags: { test: {}, a: {} },
}); // Pass
schema.validate({
defaultTag: 'test',
tags: { test: {} },
}); // Pass
schema.validate({
defaultTag: 'test',
tags: { a: {} },
}); // Fail (missing test)
Hope it solves your issue. Edit: The pull request is still valid. |
Yes, this is perfect. I'd never noticed the
Right, I never meant to imply that the pull request doesn't solve this particular issue. I was just trying to figure out if I needed to open a feature request to accomplish my original goal, and it looks like that's not necessary. |
@cdhowie So the issue itself is resolved, but the bug it discovers is still unfixed. I will leave it open until my PR is merged. |
Support plan
Context
What are you trying to achieve or the steps to reproduce?
I am trying to make a schema that will validate an object with any key (
Joi.object().pattern(/./, valueSchema)
) but where a specific key is required, but which key comes from the object being validated. For example:However, requiring
value.tags[value.defaultTag]
to be present. There doesn't seem to be a built-in way to accomplish this; I can't find a way to use a ref as an object key. (An API likeJoi.object().key(Joi.ref('foo'), Joi.number().required())
, for example, would permit this, but I can't find anything like this. Anyway, this is beside the point.)The simplest approach seemed to be to (ab)use
.xor()
by giving it a single ref (Joi.ref('defaultTag')
). However, the.xor()
call results in a confusing/contradictory exception. It boils down to this:What was the result you got?
What result did you expect?
Either
.xor()
should accept a reference, or the error message should not indicate that a reference is acceptable.The text was updated successfully, but these errors were encountered: