diff --git a/lib/modify.js b/lib/modify.js index 6f148484..6e3b0186 100755 --- a/lib/modify.js +++ b/lib/modify.js @@ -157,7 +157,7 @@ internals.fork = function (schema, id, replacement) { const each = (item, { key }) => { - if (id === (item._flags.id || key)) { + if (id === item._flags.id || id === key) { return replacement; } }; diff --git a/test/extend.js b/test/extend.js index 9cec5131..9c4d6f83 100755 --- a/test/extend.js +++ b/test/extend.js @@ -1298,6 +1298,17 @@ describe('extension', () => { expect(schema.$_getFlag('_hasNumbers')).to.be.true(); }); + it('fork in schema using id fails once rules applied on base schema are not being followed', () => { + + const innerSchema = Joi.object({}).id('myId'); + const baseSchema = Joi.object({ + myField: innerSchema + }); + const strictSchema = baseSchema.fork('myField', (keySchema) => keySchema.required()); + const { error } = strictSchema.validate({}); + expect(error).to.be.an.error('"myField" is required'); + }); + it('supports fork', () => { const custom = Joi.extend((joi) => {