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

Fork object with option already applied #2292

Closed
optii opened this issue Feb 4, 2020 · 1 comment
Closed

Fork object with option already applied #2292

optii opened this issue Feb 4, 2020 · 1 comment
Assignees
Labels
bug Bug or defect
Milestone

Comments

@optii
Copy link

optii commented Feb 4, 2020

Support plan

  • which support plan is this issue covered by? (e.g. Community, Core, Plus, or Enterprise): Comunity
  • is this issue currently blocking your project? (yes/no): yes
  • is this issue affecting a production system? (yes/no): yes

Context

  • node version: 12.14.0
  • module version with issue: 17.1.0
  • last module version without issue: we were previously working with joi 15 so don't think it is that relevant as fork didn't exist
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): hapi application & https://github.com/hapipal/schwifty
  • any other relevant information:

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

Try to run fork to apply .optional to all keys of an Joi.object (https://github.com/hapipal/schwifty/blob/cffa382e9411e404c5524c69ed39f028e24506a7/lib/model.js#L146) however
it seems that if .forkis run on a schema that already has optional applied that there is an error thrown. See reproduction below
https://runkit.com/optii/5e399335239b800013ea2b06

const joi = require("@hapi/joi")

const schema = joi.object({
  a: joi.string().optional(),
  b: joi.string()
});
 
 const bOptional = schema.fork(['b'], (schema) =>  schema.optional());
 const allOptional = schema.fork(['a', 'b'], (schema) => schema.optional());

What was the result you got?

TypeError: Cannot read property '$_mutateRebuild' of undefined

What result did you expect?

I expected for joi to ignore the fact that the schema hadn't changed or to throw a more friendlier type of error, the first option being preferred and seems in keeping with the fact that Joi.string().optional().optional() works

@optii optii added the support Questions, discussions, and general support label Feb 4, 2020
hueniverse added a commit that referenced this issue Feb 12, 2020
@hueniverse hueniverse added this to the 17.1.1 milestone Feb 12, 2020
@hueniverse hueniverse added bug Bug or defect and removed support Questions, discussions, and general support labels Feb 12, 2020
@gerpedrosa
Copy link

Could you apply this fix in version 16?
At the moment I can't use version 17 in my project because I need to keep support for Node.js 10

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

No branches or pull requests

4 participants