Skip to content

Commit 1f3f81c

Browse files
authored
fix(validation): impossible to switch the language for validation errors (#173)
* fix localization during validation * add Lang and Theme switcher to storybook
1 parent b437ff5 commit 1f3f81c

File tree

4 files changed

+56
-7
lines changed

4 files changed

+56
-7
lines changed

.storybook/decorators/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './withTheme';
2+
export * from './withLang';

.storybook/decorators/withLang.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react';
2+
3+
import type {Decorator} from '@storybook/react';
4+
import {Lang, configure} from '../../src';
5+
6+
export const withLang: Decorator = (Story, context) => {
7+
const lang = context.globals.lang;
8+
9+
React.useEffect(() => {
10+
configure({
11+
lang: lang as Lang,
12+
});
13+
}, [lang]);
14+
15+
return <Story key={lang} {...context} />;
16+
};

.storybook/preview.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import {withTheme} from './decorators';
1+
import {withTheme, withLang} from './decorators';
22

3-
export const decorators = [withTheme];
3+
export const decorators = [withTheme, withLang];
44

55
export const parameters = {
66
actions: {argTypesRegex: '^on[A-Z].*'},
@@ -11,3 +11,30 @@ export const parameters = {
1111
},
1212
},
1313
};
14+
15+
export const globalTypes = {
16+
theme: {
17+
defaultValue: 'light',
18+
toolbar: {
19+
title: 'Theme',
20+
icon: 'mirror',
21+
items: [
22+
{value: 'light', right: '☼', title: 'Light'},
23+
{value: 'dark', right: '☾', title: 'Dark'},
24+
{value: 'light-hc', right: '☼', title: 'High Contrast Light (beta)'},
25+
{value: 'dark-hc', right: '☾', title: 'High Contrast Dark (beta)'},
26+
],
27+
},
28+
},
29+
lang: {
30+
defaultValue: 'en',
31+
toolbar: {
32+
title: 'Language',
33+
icon: 'globe',
34+
items: [
35+
{value: 'en', right: '🇬🇧', title: 'En'},
36+
{value: 'ru', right: '🇷🇺', title: 'Ru'},
37+
],
38+
},
39+
},
40+
}

src/lib/kit/validators/validators.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ export interface GetArrayValidatorParams extends CommonValidatorParams {
2727
export const getArrayValidator = (params: GetArrayValidatorParams = {}) => {
2828
const {ignoreRequiredCheck, ignoreMaxLengthCheck, ignoreMinLengthCheck, customErrorMessages} =
2929
params;
30-
const errorMessages = {...ErrorMessages, ...customErrorMessages};
3130

3231
return (spec: ArraySpec, value?: ArrayValue) => {
32+
const errorMessages = {...ErrorMessages, ...customErrorMessages};
33+
3334
const valueLength = value?.length || 0;
3435

3536
if (!ignoreRequiredCheck && spec.required && !_.isArray(value)) {
@@ -60,9 +61,10 @@ export interface GetBooleanValidatorParams extends CommonValidatorParams {}
6061

6162
export const getBooleanValidator = (params: GetBooleanValidatorParams = {}) => {
6263
const {ignoreRequiredCheck, customErrorMessages} = params;
63-
const errorMessages = {...ErrorMessages, ...customErrorMessages};
6464

6565
return (spec: BooleanSpec, value?: boolean) => {
66+
const errorMessages = {...ErrorMessages, ...customErrorMessages};
67+
6668
if (!ignoreRequiredCheck && spec.required && !value) {
6769
return errorMessages.REQUIRED;
6870
}
@@ -95,10 +97,11 @@ export const getNumberValidator = (params: GetNumberValidatorParams = {}) => {
9597
ignoreZeroStart,
9698
customErrorMessages,
9799
} = params;
98-
const errorMessages = {...ErrorMessages, ...customErrorMessages};
99100

100101
// eslint-disable-next-line complexity
101102
return (spec: NumberSpec, value: string | number = '') => {
103+
const errorMessages = {...ErrorMessages, ...customErrorMessages};
104+
102105
const stringValue = String(value);
103106

104107
if (!ignoreRequiredCheck && spec.required && !stringValue.length) {
@@ -165,9 +168,10 @@ export interface GetObjectValidatorParams extends CommonValidatorParams {}
165168

166169
export const getObjectValidator = (params: GetObjectValidatorParams = {}) => {
167170
const {ignoreRequiredCheck, customErrorMessages} = params;
168-
const errorMessages = {...ErrorMessages, ...customErrorMessages};
169171

170172
return (spec: ObjectSpec, value?: ObjectValue) => {
173+
const errorMessages = {...ErrorMessages, ...customErrorMessages};
174+
171175
if (!ignoreRequiredCheck && spec.required && !value) {
172176
return errorMessages.REQUIRED;
173177
}
@@ -194,10 +198,11 @@ export const getStringValidator = (params: GetStringValidatorParams = {}) => {
194198
ignoreRegExpCheck,
195199
customErrorMessages,
196200
} = params;
197-
const errorMessages = {...ErrorMessages, ...customErrorMessages};
198201

199202
// eslint-disable-next-line complexity
200203
return (spec: StringSpec, value = '') => {
204+
const errorMessages = {...ErrorMessages, ...customErrorMessages};
205+
201206
const valueLength = value?.length;
202207

203208
if (!ignoreRequiredCheck && spec.required && !valueLength) {

0 commit comments

Comments
 (0)