diff --git a/src/object.js b/src/object.js index fdd51102e..c624cb421 100644 --- a/src/object.js +++ b/src/object.js @@ -146,7 +146,10 @@ inherits(ObjectSchema, MixedSchema, { originalValue = originalValue || value; let validations = this._nodes.map(key => { - let path = makePath`${opts.path}.${key}`; + let path = + key.indexOf('.') === -1 + ? makePath`${opts.path}.${key}` + : makePath`${opts.path}["${key}"]`; let field = this.fields[key]; let innerOptions = { diff --git a/test/object.js b/test/object.js index 705067fb0..35db47246 100644 --- a/test/object.js +++ b/test/object.js @@ -504,6 +504,27 @@ describe('Object types', () => { } }); + it('should set the correct path with dotted keys', async () => { + let inst = object({ + 'dotted.str': string() + .required() + .nullable(), + nested: lazy(() => inst.default(undefined)), + }); + + let value = { + nested: { 'dotted.str': null }, + 'dotted.str': 'foo', + }; + + try { + await inst.validate(value, { strict: true }); + } catch (err) { + err.path.should.equal('nested["dotted.str"]'); + err.message.should.match(/required/); + } + }); + it('should resolve array sub types', async () => { let inst = object({ str: string()