Skip to content

Commit

Permalink
fix: issue/2090 i18n usage on cell props
Browse files Browse the repository at this point in the history
  • Loading branch information
codefactor authored and i832513 committed Feb 20, 2023
1 parent 3861749 commit 37e9e02
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
19 changes: 9 additions & 10 deletions packages/core/src/util/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
*/

import isEmpty from 'lodash/isEmpty';
import union from 'lodash/union';
import type { JsonFormsCellRendererRegistryEntry } from '../reducers';
import { getErrorTranslator, JsonFormsCellRendererRegistryEntry } from '../reducers';
import {
getAjv,
getConfig,
Expand All @@ -35,10 +34,7 @@ import {
getTranslator,
} from '../reducers';
import type { AnyAction, Dispatch } from './type';
import {
formatErrorMessage,
Resolve,
} from './util';
import { Resolve } from './util';
import {
isInherentlyEnabled,
isVisible,
Expand All @@ -53,9 +49,12 @@ import {
OwnPropsOfEnum,
StatePropsOfScopedRenderer,
} from './renderer';
import {
getCombinedErrorMessage,
getI18nKeyPrefix,
} from '../i18n';
import type { JsonFormsState } from '../store';
import type { JsonSchema } from '../models';
import { getI18nKeyPrefix } from '../i18n';

export type { JsonFormsCellRendererRegistryEntry };

Expand Down Expand Up @@ -148,9 +147,9 @@ export const mapStateToCellProps = (
);
}

const errors = formatErrorMessage(
union(getErrorAt(path, schema)(state).map(error => error.message))
);
const t = getTranslator()(state);
const te = getErrorTranslator()(state);
const errors = getCombinedErrorMessage(getErrorAt(path, schema)(state), te, t, schema, uischema, path);
const isValid = isEmpty(errors);

return {
Expand Down
29 changes: 28 additions & 1 deletion packages/core/test/util/cell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ import { UPDATE_DATA, UpdateAction } from '../../src/actions';
import configureStore from 'redux-mock-store';
import {
ControlElement,
createAjv,
defaultErrorTranslator,
JsonFormsState,
JsonSchema,
RuleEffect,
UISchemaElement
UISchemaElement,
validate
} from '../../src';
import { enumToEnumOptionMapper } from '../../src/util/renderer';

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

test('mapStateToCellProps - translated error', t => {
const ownProps = {
uischema: coreUISchema,
id: '#/properties/firstName'
};
const state = createState(coreUISchema);
const schema = state.jsonforms.core?.schema as JsonSchema;
const data = state.jsonforms.core?.data as any;
// mark firstName as required, delete the value from data, then get errors from ajv from the compiled schema
schema.required = ["firstName"];
delete data.firstName;
const ajv = createAjv();
const v = ajv.compile(schema);
state.jsonforms.errors = validate(v, data);
// add a mock i18n state to verify that the error gets translated
state.jsonforms.i18n = {
translateError: (error) => `i18n-error:${error.keyword}`,
translate: (id: string) => `i18n-key:${id}`
};
const props = mapStateToCellProps(state, ownProps);
t.is(props.errors, 'i18n-error:required');
});

test('mapStateToEnumCellProps - set default options for dropdown list', t => {
const uischema: ControlElement = {
type: 'Control',
Expand Down

0 comments on commit 37e9e02

Please sign in to comment.