Skip to content
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

coercions during alternative match leads to unexpected results #2579

Open
nlundquist opened this issue Mar 11, 2021 · 1 comment · May be fixed by #2581
Open

coercions during alternative match leads to unexpected results #2579

nlundquist opened this issue Mar 11, 2021 · 1 comment · May be fixed by #2581

Comments

@nlundquist
Copy link

nlundquist commented Mar 11, 2021

Support plan

  • is this issue currently blocking your project? (yes/no): yes
  • is this issue affecting a production system? (yes/no): yes

Context

  • node version: 14
  • module version with issue: 17.x
  • last module version without issue: n/a
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): standalone
  • any other relevant information:

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

Coercions occurring during an alternative match do not take precedence over the uncoerced values during the subschema response value merging, resulting in uncoerced properties overwriting coerced ones.

// coercion works as expected when not inside `alternatives`
joi.object({ org_id: joi.number().required() }).unknown().validate({ org_id: '5', foo: 'bar' }, { convert: true })
// === { value: { org_id: 5, foo: 'bar' } }

// coercion fails when inside `alternatives`
joi.alternatives().try(
   joi.object({ org_id: joi.number().required() }).unknown(),
   joi.object({ foo: joi.string().required() }).unknown()
).match('all').validate({ org_id: '5', foo: 'bar' }, { convert: true })
// === { value: { org_id: '5', foo: 'bar' } }

What was the result you got?

org_id: '5'

What result did you expect?

org_ig: 5

@nlundquist
Copy link
Author

the only straightforward resolution i can come up with is conditionally merging the matched subschema values only when their properties vary from the original value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant