Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFR] Refactor validators to not return translated errors #3339

Merged
merged 42 commits into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
22e82f1
Refactor validators to not return translated errors
djhi Jun 15, 2019
5367385
Added a unstyled ValidationError displaying translated errors
djhi Jun 15, 2019
f6662b8
Fix validate in simple example
djhi Jun 15, 2019
bc6bb9c
Migrate TextInput
djhi Jun 15, 2019
1153e5b
Migrate LongTextInput
djhi Jun 15, 2019
21335c4
Migrate NumberInput
djhi Jun 15, 2019
b84039d
Migrate SelectInput
djhi Jun 15, 2019
d02160d
Fix ValidationError and rename types to avoid confusion
djhi Jun 19, 2019
acaa303
Introduce InputHelperText
djhi Jun 19, 2019
ed94b5b
Use InputHelperText in TextInput
djhi Jun 19, 2019
5370732
Refactor LongTextInput to use TextInput with different default props …
djhi Jun 19, 2019
e83df9c
Migrate SelectInput
djhi Jun 19, 2019
b60dd99
Migrate NumberInput
djhi Jun 19, 2019
89b0d26
Migrate SelectArrayInput
djhi Jun 19, 2019
aa3a745
Migrate RadioButtonGroupInput
djhi Jun 19, 2019
183a080
Migrated AutocompleteArrayInput
djhi Jun 19, 2019
42b94e2
Migrated AutocompleteInput
djhi Jun 19, 2019
52a54d1
Migrate DateInput
djhi Jun 19, 2019
65abc29
Migrated DateTimeInput
djhi Jun 19, 2019
32f98c1
Migrated FIleInput
djhi Jun 19, 2019
9a53443
Migrated NullableBooleanInput
djhi Jun 19, 2019
4a62948
Migrated BooleanInput
djhi Jun 19, 2019
9b02c03
Migrated SelectArrayInput
djhi Jun 19, 2019
d48eedd
Fix after rebase
djhi Jun 19, 2019
055c505
Fix BooleanInput tests
djhi Jun 19, 2019
8dc05ba
Fix ArrayInput tests
djhi Jun 19, 2019
d75deef
Restore unit test with watch mode
djhi Jun 19, 2019
3b7bcc6
Cleanup package.json
djhi Jun 19, 2019
46d5161
Review ValidationError
djhi Jun 19, 2019
15da162
Cleanup imports
djhi Jun 19, 2019
5dfc5d0
Linting
djhi Jun 19, 2019
b0c0e1e
Fix ArrayInput performances
djhi Jun 20, 2019
ee34da5
Review
djhi Jun 20, 2019
8d2ff10
Export InputHelperText
djhi Jun 20, 2019
2115caf
Migrate RichTextInput
djhi Jun 20, 2019
845965c
Fix SelectInput
djhi Jun 20, 2019
3fcf58e
Better ValidationError tests
djhi Jun 20, 2019
b0a37f2
Revert breaking change on BooleanInput
djhi Jun 20, 2019
5cff58f
Add documentation and upgrade
fzaninotto Jun 25, 2019
a1939d8
Remove translation of error arguments
fzaninotto Jun 25, 2019
f76fc1a
Add base test case for InputHelperText
fzaninotto Jun 25, 2019
f8ba430
Add deprecation annotation
fzaninotto Jun 25, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ test-unit: ## launch unit tests

test-unit-watch: ## launch unit tests and watch for changes
echo "Running unit tests..."; \
yarn -s test-unit; \
yarn -s test-unit --watch; \

test-e2e: ## launch end-to-end tests
@if [ "$(build)" != "false" ]; then \
Expand Down
4 changes: 2 additions & 2 deletions examples/simple/src/posts/PostCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ const PostCreate = ({ permissions, ...props }) => (
const errors = {};
['title', 'teaser'].forEach(field => {
if (!values[field]) {
errors[field] = ['Required field'];
errors[field] = 'Required field';
}
});

if (values.average_note < 0 || values.average_note > 5) {
errors.average_note = ['Should be between 0 and 5'];
errors.average_note = 'Should be between 0 and 5';
}

return errors;
Expand Down
80 changes: 0 additions & 80 deletions packages/ra-core/package-lock.json

This file was deleted.

45 changes: 45 additions & 0 deletions packages/ra-core/src/form/ValidationError.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React from 'react';
import { render, cleanup } from 'react-testing-library';

import ValidationError from './ValidationError';

const translate = jest.fn(key => key);

jest.mock('../i18n', () => ({
useTranslate: () => translate,
}));

describe('ValidationError', () => {
afterEach(() => {
cleanup();
translate.mockClear();
});

it('It renders the error message translated if it is a string', () => {
const { getByText } = render(
<ValidationError error="ra.validation.required" />
);

expect(getByText('ra.validation.required')).toBeTruthy();
expect(translate).toHaveBeenCalledWith('ra.validation.required', {
djhi marked this conversation as resolved.
Show resolved Hide resolved
_: 'ra.validation.required',
});
});

it('It renders the error message translated if it is an object, with all its arguments translated as well', () => {
const { getByText } = render(
<ValidationError
error={{
message: 'ra.validation.minValue',
args: { value: 10 },
}}
/>
);

expect(getByText('ra.validation.minValue')).toBeDefined();
expect(translate).toHaveBeenCalledWith('ra.validation.minValue', {
value: '10',
});
expect(translate).toHaveBeenCalledWith('10', { _: '10' });
});
});
50 changes: 50 additions & 0 deletions packages/ra-core/src/form/ValidationError.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React, { FunctionComponent } from 'react';
import {
ValidationErrorMessage,
ValidationErrorMessageWithArgs,
} from './validate';
import { useTranslate } from '../i18n';
import { Translate } from '../types';

interface Props {
error: ValidationErrorMessage;
djhi marked this conversation as resolved.
Show resolved Hide resolved
}

const translateArrayArgValue = (translate: Translate, values: any[]) =>
values
.map(value => translate(value.toString(), { _: value.toString() }))
.join(', ');

const translateArgValue = (translate: Translate, argValue: any) => {
if (Array.isArray(argValue)) {
return translateArrayArgValue(translate, argValue);
}

return translate(argValue.toString(), { _: argValue.toString() });
};

const translateArgs = (translate: Translate, args: { [key: string]: any }) =>
fzaninotto marked this conversation as resolved.
Show resolved Hide resolved
Object.keys(args).reduce(
(acc, key) => ({
...acc,
[key]: translateArgValue(translate, args[key]),
}),
{}
);

const ValidationError: FunctionComponent<Props> = ({ error }) => {
const translate = useTranslate();

if ((error as ValidationErrorMessageWithArgs).message) {
const { message, args } = error as ValidationErrorMessageWithArgs;
const { _, ...allArgsButDefault } = args;

const translatedArgs = translateArgs(translate, allArgsButDefault);

return <>{translate(message, translatedArgs)}</>;
}

return <>{translate(error as string, { _: error })}</>;
};

export default ValidationError;
2 changes: 2 additions & 0 deletions packages/ra-core/src/form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import FormField from './FormField';
import formMiddleware from './formMiddleware';
import getDefaultValues from './getDefaultValues';
import withDefaultValue from './withDefaultValue';
import ValidationError from './ValidationError';

export {
addField,
Expand All @@ -12,6 +13,7 @@ export {
formMiddleware,
getDefaultValues,
withDefaultValue,
ValidationError,
};
export { isRequired } from './FormField';
export * from './validate';
Expand Down
13 changes: 4 additions & 9 deletions packages/ra-core/src/form/validate.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assert from 'assert';

import {
required,
minLength,
Expand All @@ -12,12 +13,12 @@ import {
} from './validate';

describe('Validators', () => {
const translate = x => x;
const test = (validator, inputs, message) =>
assert.deepEqual(
inputs
.map(input => validator(input, null, { translate }))
.filter(m => m === message),
.map(input => validator(input, null))
.filter(error => error === message || error.message === message)
.map(error => (error && error.message ? error.message : error)),
Array(...Array(inputs.length)).map(() => message)
);
describe('required', () => {
Expand All @@ -42,7 +43,6 @@ describe('Validators', () => {
args: undefined,
value: null,
values: null,
translate,
});
});
});
Expand Down Expand Up @@ -71,7 +71,6 @@ describe('Validators', () => {
args: { min: 5 },
value: '12',
values: null,
translate,
});
});
});
Expand Down Expand Up @@ -100,7 +99,6 @@ describe('Validators', () => {
args: { max: 10 },
value: '12345678901',
values: null,
translate,
});
});
});
Expand All @@ -125,7 +123,6 @@ describe('Validators', () => {
args: { min: 10 },
value: 0,
values: null,
translate,
});
});
});
Expand Down Expand Up @@ -154,7 +151,6 @@ describe('Validators', () => {
args: { max: 10 },
value: '11',
values: null,
translate,
});
});
});
Expand All @@ -176,7 +172,6 @@ describe('Validators', () => {
args: undefined,
value: 'foo',
values: null,
translate,
});
});
});
Expand Down
Loading