Skip to content

Fork object with option already applied #2292

@optii

Description

@optii

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

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