Skip to content

Commit 2bbc646

Browse files
codefactorsdirix
andauthored
fix: mapStateToCellProps should use translated error messages
mapStateToCellProps is now also able to translate error messages, enabling localized error messages in cells. Co-authored-by: Stefan Dirix <[email protected]>
1 parent 30a1851 commit 2bbc646

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

packages/core/src/util/cell.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
*/
2525

2626
import isEmpty from 'lodash/isEmpty';
27-
import union from 'lodash/union';
28-
import type { JsonFormsCellRendererRegistryEntry } from '../reducers';
27+
import { getErrorTranslator, JsonFormsCellRendererRegistryEntry } from '../reducers';
2928
import {
3029
getAjv,
3130
getConfig,
@@ -35,10 +34,7 @@ import {
3534
getTranslator,
3635
} from '../reducers';
3736
import type { AnyAction, Dispatch } from './type';
38-
import {
39-
formatErrorMessage,
40-
Resolve,
41-
} from './util';
37+
import { Resolve } from './util';
4238
import {
4339
isInherentlyEnabled,
4440
isVisible,
@@ -53,9 +49,12 @@ import {
5349
OwnPropsOfEnum,
5450
StatePropsOfScopedRenderer,
5551
} from './renderer';
52+
import {
53+
getCombinedErrorMessage,
54+
getI18nKeyPrefix,
55+
} from '../i18n';
5656
import type { JsonFormsState } from '../store';
5757
import type { JsonSchema } from '../models';
58-
import { getI18nKeyPrefix } from '../i18n';
5958

6059
export type { JsonFormsCellRendererRegistryEntry };
6160

@@ -148,9 +147,9 @@ export const mapStateToCellProps = (
148147
);
149148
}
150149

151-
const errors = formatErrorMessage(
152-
union(getErrorAt(path, schema)(state).map(error => error.message))
153-
);
150+
const t = getTranslator()(state);
151+
const te = getErrorTranslator()(state);
152+
const errors = getCombinedErrorMessage(getErrorAt(path, schema)(state), te, t, schema, uischema, path);
154153
const isValid = isEmpty(errors);
155154

156155
return {

packages/core/test/util/cell.test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ import { UPDATE_DATA, UpdateAction } from '../../src/actions';
3636
import configureStore from 'redux-mock-store';
3737
import {
3838
ControlElement,
39+
createAjv,
3940
JsonFormsState,
41+
JsonSchema,
4042
RuleEffect,
41-
UISchemaElement
43+
UISchemaElement,
44+
validate,
4245
} from '../../src';
4346
import { enumToEnumOptionMapper } from '../../src/util/renderer';
4447

@@ -270,6 +273,29 @@ test('mapStateToCellProps - id', t => {
270273
t.is(props.id, '#/properties/firstName');
271274
});
272275

276+
test('mapStateToCellProps - translated error', t => {
277+
const ownProps = {
278+
uischema: coreUISchema,
279+
id: '#/properties/firstName',
280+
path: 'firstName'
281+
};
282+
const state = createState(coreUISchema);
283+
const schema = state.jsonforms.core?.schema as JsonSchema;
284+
const data = state.jsonforms.core?.data as any;
285+
// mark firstName as required, delete the value from data, then get errors from ajv from the compiled schema
286+
schema.required = ["firstName"];
287+
delete data.firstName;
288+
const ajv = createAjv();
289+
const v = ajv.compile(schema);
290+
state.jsonforms.core!.errors = validate(v, data);
291+
// add a mock i18n state to verify that the error gets translated
292+
state.jsonforms.i18n = {
293+
translateError: error => `i18n-error:${error.keyword}`
294+
};
295+
const props = mapStateToCellProps(state, ownProps);
296+
t.is(props.errors, 'i18n-error:required');
297+
});
298+
273299
test('mapStateToEnumCellProps - set default options for dropdown list', t => {
274300
const uischema: ControlElement = {
275301
type: 'Control',

0 commit comments

Comments
 (0)