Skip to content

Commit 780c3d9

Browse files
codefactorsap-codefactor
authored andcommitted
fix: issue/2090 i18n usage on cell props
1 parent 3861749 commit 780c3d9

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-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: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@ import { UPDATE_DATA, UpdateAction } from '../../src/actions';
3636
import configureStore from 'redux-mock-store';
3737
import {
3838
ControlElement,
39+
createAjv,
40+
defaultErrorTranslator,
3941
JsonFormsState,
42+
JsonSchema,
4043
RuleEffect,
41-
UISchemaElement
44+
UISchemaElement,
45+
validate
4246
} from '../../src';
4347
import { enumToEnumOptionMapper } from '../../src/util/renderer';
4448

@@ -270,6 +274,29 @@ test('mapStateToCellProps - id', t => {
270274
t.is(props.id, '#/properties/firstName');
271275
});
272276

277+
test('mapStateToCellProps - translated error', t => {
278+
const ownProps = {
279+
uischema: coreUISchema,
280+
id: '#/properties/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.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+
translate: (id: string) => `i18n-key:${id}`
295+
};
296+
const props = mapStateToCellProps(state, ownProps);
297+
t.is(props.errors, 'i18n-error:required');
298+
});
299+
273300
test('mapStateToEnumCellProps - set default options for dropdown list', t => {
274301
const uischema: ControlElement = {
275302
type: 'Control',

0 commit comments

Comments
 (0)