Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .storybook/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './withTheme';
export * from './withLang';
16 changes: 16 additions & 0 deletions .storybook/decorators/withLang.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';

import type {Decorator} from '@storybook/react';
import {Lang, configure} from '../../src';

export const withLang: Decorator = (Story, context) => {
const lang = context.globals.lang;

React.useEffect(() => {
configure({
lang: lang as Lang,
});
}, [lang]);

return <Story key={lang} {...context} />;
};
31 changes: 29 additions & 2 deletions .storybook/preview.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {withTheme} from './decorators';
import {withTheme, withLang} from './decorators';

export const decorators = [withTheme];
export const decorators = [withTheme, withLang];

export const parameters = {
actions: {argTypesRegex: '^on[A-Z].*'},
Expand All @@ -11,3 +11,30 @@ export const parameters = {
},
},
};

export const globalTypes = {
theme: {
defaultValue: 'light',
toolbar: {
title: 'Theme',
icon: 'mirror',
items: [
{value: 'light', right: '☼', title: 'Light'},
{value: 'dark', right: '☾', title: 'Dark'},
{value: 'light-hc', right: '☼', title: 'High Contrast Light (beta)'},
{value: 'dark-hc', right: '☾', title: 'High Contrast Dark (beta)'},
],
},
},
lang: {
defaultValue: 'en',
toolbar: {
title: 'Language',
icon: 'globe',
items: [
{value: 'en', right: '🇬🇧', title: 'En'},
{value: 'ru', right: '🇷🇺', title: 'Ru'},
],
},
},
}
15 changes: 10 additions & 5 deletions src/lib/kit/validators/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ export interface GetArrayValidatorParams extends CommonValidatorParams {
export const getArrayValidator = (params: GetArrayValidatorParams = {}) => {
const {ignoreRequiredCheck, ignoreMaxLengthCheck, ignoreMinLengthCheck, customErrorMessages} =
params;
const errorMessages = {...ErrorMessages, ...customErrorMessages};

return (spec: ArraySpec, value?: ArrayValue) => {
const errorMessages = {...ErrorMessages, ...customErrorMessages};

const valueLength = value?.length || 0;

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

export const getBooleanValidator = (params: GetBooleanValidatorParams = {}) => {
const {ignoreRequiredCheck, customErrorMessages} = params;
const errorMessages = {...ErrorMessages, ...customErrorMessages};

return (spec: BooleanSpec, value?: boolean) => {
const errorMessages = {...ErrorMessages, ...customErrorMessages};

if (!ignoreRequiredCheck && spec.required && !value) {
return errorMessages.REQUIRED;
}
Expand Down Expand Up @@ -95,10 +97,11 @@ export const getNumberValidator = (params: GetNumberValidatorParams = {}) => {
ignoreZeroStart,
customErrorMessages,
} = params;
const errorMessages = {...ErrorMessages, ...customErrorMessages};

// eslint-disable-next-line complexity
return (spec: NumberSpec, value: string | number = '') => {
const errorMessages = {...ErrorMessages, ...customErrorMessages};

const stringValue = String(value);

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

export const getObjectValidator = (params: GetObjectValidatorParams = {}) => {
const {ignoreRequiredCheck, customErrorMessages} = params;
const errorMessages = {...ErrorMessages, ...customErrorMessages};

return (spec: ObjectSpec, value?: ObjectValue) => {
const errorMessages = {...ErrorMessages, ...customErrorMessages};

if (!ignoreRequiredCheck && spec.required && !value) {
return errorMessages.REQUIRED;
}
Expand All @@ -194,10 +198,11 @@ export const getStringValidator = (params: GetStringValidatorParams = {}) => {
ignoreRegExpCheck,
customErrorMessages,
} = params;
const errorMessages = {...ErrorMessages, ...customErrorMessages};

// eslint-disable-next-line complexity
return (spec: StringSpec, value = '') => {
const errorMessages = {...ErrorMessages, ...customErrorMessages};

const valueLength = value?.length;

if (!ignoreRequiredCheck && spec.required && !valueLength) {
Expand Down