Skip to content

Commit

Permalink
feat: use the alternate object index path syntax if the key contains …
Browse files Browse the repository at this point in the history
…dots (fixes #536) (#539)
  • Loading branch information
espenhw authored and jquense committed May 24, 2019
1 parent 7e94395 commit 13e8c76
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
21 changes: 21 additions & 0 deletions test/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 13e8c76

Please sign in to comment.