Skip to content

Commit

Permalink
Fixed dataPath parsing (fixes #593).
Browse files Browse the repository at this point in the history
  • Loading branch information
radekmie committed Sep 18, 2019
1 parent f349ea2 commit a0655ed
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Next

- **Fixed:** Parsing of `dataPath` in `JSONSchemaBridge`. [\#593](https://github.com/vazco/uniforms/issues/593)

## [v2.4.0](https://github.com/vazco/uniforms/tree/v2.4.0) (2019-08-28)

- **Added:** Default labels in `GraphQLSchemaBridge`. [\#577](https://github.com/vazco/uniforms/issues/577)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,35 @@ describe('JSONSchemaBridge', () => {
required: ['type']
}
]
},
complexNames: {
type: 'object',
properties: {
a: {
type: 'array',
items: {
type: 'object',
properties: {
b: {
type: 'object',
properties: {
'c-d': {
type: 'array',
items: {
type: 'object',
properties: {
"f/'g": { type: 'string', pattern: '[0-9]{5}' },
'h/"i': { type: 'string', pattern: '[0-9]{5}' },
'j\'/"k': { type: 'string', pattern: '[0-9]{5}' }
}
}
}
}
}
}
}
}
}
}
},
required: ['dateOfBirth']
Expand Down Expand Up @@ -170,6 +199,32 @@ describe('JSONSchemaBridge', () => {
expect(bridge.getError('x.y', error)).toEqual(error.details[0]);
expect(bridge.getError('y.x', error)).toEqual(undefined);
});

it('works with correct error (complex data paths)', () => {
const pairs = [
["a.0.b.c-d.0.f/'g", ".complexNames.a[0].b['c-d'][0]['f/\\'g']"],
['a.0.b.c-d.0.h/"i', ".complexNames.a[0].b['c-d'][0]['h/\"i']"],
['a.0.b.c-d.0.j\'/"k~', ".complexNames.a[0].b['c-d'][0]['j\\'/\"k~']"]
];

pairs.forEach(([name, dataPath]) => {
const error = { details: { dataPath } };
expect(bridge.getError(name, error)).toEqual(error.details[0]);
});
});

it('works with correct error (complex data paths - JSON pointers)', () => {
const pairs = [
["a.0.b.c-d.0.f/'g", "/complexNames/a/0/b/c-d/0/f~1'g"],
['a.0.b.c-d.0.h/"i', '/complexNames/a/0/b/c-d/0/h~1"i'],
['a.0.b.c-d.0.j\'/"k~', '/complexNames/a/0/b/c-d/0/j\'~1"k~0']
];

pairs.forEach(([name, dataPath]) => {
const error = { details: { dataPath } };
expect(bridge.getError(name, error)).toEqual(error.details[0]);
});
});
});

describe('#getErrorMessage', () => {
Expand Down Expand Up @@ -499,7 +554,8 @@ describe('JSONSchemaBridge', () => {
'invalid',
'personalData',
'salary',
'shippingAddress'
'shippingAddress',
'complexNames'
]);
});

Expand Down
18 changes: 17 additions & 1 deletion packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ const extractValue = (...xs) =>
x === false || x === null ? '' : x !== true && x !== undefined ? x : y
);

const pathToName = path => {
if (path[0] === '.')
path = path
.replace(/\['(.+?)'\]/g, '.$1')
.replace(/\[(.+?)\]/g, '.$1')
.replace(/\\'/g, "'");
else
path = path
.replace(/\//g, '.')
.replace(/~0/g, '~')
.replace(/~1/g, '/');

return path.slice(1);
};

const toHumanLabel = label => upperFirst(lowerCase(label));

export default class JSONSchemaBridge extends Bridge {
Expand All @@ -71,7 +86,8 @@ export default class JSONSchemaBridge extends Bridge {
error.details &&
error.details.find &&
error.details.find(detail => {
const path = detail.dataPath.substring(1);
const path = pathToName(detail.dataPath);

return (
name === path ||
(rootName === path && baseName === detail.params.missingProperty)
Expand Down

0 comments on commit a0655ed

Please sign in to comment.