Skip to content

Commit

Permalink
Fixed handling of required in JSONSchemBridge (fixes #554).
Browse files Browse the repository at this point in the history
  • Loading branch information
radekmie authored and kestarumper committed Jul 16, 2019
1 parent e3bc39a commit cd5050d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [v2.2.0](https://github.com/vazco/uniforms/tree/v2.2.0) (2019-07-12)

- **Added:** Support for `labelClassName` in `uniforms-bootstrap3` and `uniforms-bootstrap4` themes. [\#548](https://github.com/vazco/uniforms/issues/548)
- **Fixed:** Handling of `required` validation in `JSONSchemaBridge`. [\#554](https://github.com/vazco/uniforms/issues/554)

## [v2.1.0](https://github.com/vazco/uniforms/tree/v2.1.0) (2019-06-18)

- **Added:** Support for `@material-ui/core@4`. [\#542](https://github.com/vazco/uniforms/issues/542)
Expand Down
46 changes: 39 additions & 7 deletions packages/uniforms-bridge-json-schema/__tests__/JSONSchemaBridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,45 @@ describe('JSONSchemaBridge', () => {
expect(bridge.getError('age', { invalid: true })).not.toBeTruthy();
});

it('works with correct error', () => {
expect(
bridge.getError('age', { details: [{ dataPath: '.age' }] })
).toEqual({ dataPath: '.age' });
expect(
bridge.getError('age', { details: [{ dataPath: '.field' }] })
).not.toBeTruthy();
it('works with correct error (data path)', () => {
const error = { details: [{ dataPath: '.x' }] };
expect(bridge.getError('x', error)).toEqual(error.details[0]);
expect(bridge.getError('y', error)).toEqual(undefined);
});

it('works with correct error (data path at root)', () => {
const error = {
details: [
{
dataPath: '',
keyword: 'required',
message: "should have required property 'x'",
params: { missingProperty: 'x' },
schemaPath: '#/required'
}
]
};

expect(bridge.getError('x', error)).toEqual(error.details[0]);
expect(bridge.getError('y', error)).toEqual(undefined);
});

it('works with correct error (data path of parent)', () => {
const error = {
details: [
{
dataPath: '.x',
keyword: 'required',
message: "should have required property 'y'",
params: { missingProperty: 'y' },
schemaPath: '#/properties/x/required'
}
]
};

expect(bridge.getError('x.x', error)).toEqual(undefined);
expect(bridge.getError('x.y', error)).toEqual(error.details[0]);
expect(bridge.getError('y.x', error)).toEqual(undefined);
});
});

Expand Down
14 changes: 11 additions & 3 deletions packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,21 @@ export default class JSONSchemaBridge extends Bridge {
}

getError(name, error) {
const nameParts = joinName(null, name);
const rootName = joinName(nameParts.slice(0, -1));
const baseName = nameParts[nameParts.length - 1];

return (
error &&
error.details &&
error.details.find &&
error.details.find(
detail => detail.dataPath && detail.dataPath.substring(1) === name
)
error.details.find(detail => {
const path = detail.dataPath.substring(1);
return (
name === path ||
(rootName === path && baseName === detail.params.missingProperty)
);
})
);
}

Expand Down

0 comments on commit cd5050d

Please sign in to comment.