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

feat: add ability to use custom patternError #224

Merged
merged 2 commits into from
Aug 20, 2024
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
30 changes: 30 additions & 0 deletions src/lib/kit/utils/__tests__/common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,36 @@ describe('kit/utils/common', () => {
expect(prepareSpec({properties: {prop: {type: 'NUMBER'}}} as any)).toMatchObject({
properties: {prop: {type: 'number'}},
});

const getErrorMessage = (regexp?: string) => {
return `My custom error message for ${regexp}`;
};

expect(prepareSpec({pattern: '[a-zA-Z0-9]'} as any, false, getErrorMessage)).toMatchObject({
pattern: '[a-zA-Z0-9]',
patternError: 'My custom error message for [a-zA-Z0-9]',
});

expect(
prepareSpec(
{
properties: {
test: {
pattern: '[a-zA-Z0-9]',
},
},
} as any,
false,
getErrorMessage,
),
).toMatchObject({
properties: {
test: {
pattern: '[a-zA-Z0-9]',
patternError: 'My custom error message for [a-zA-Z0-9]',
},
},
});
});

test('isCorrectSizeParams', () => {
Expand Down
14 changes: 12 additions & 2 deletions src/lib/kit/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import isNil from 'lodash/isNil';
import isObject from 'lodash/isObject';
import isObjectLike from 'lodash/isObjectLike';
import isString from 'lodash/isString';
import isEmpty from 'lodash/isEmpty';

import {
FormValue,
Expand Down Expand Up @@ -63,6 +64,7 @@ export const isNotEmptyValue = (value: FormValue | undefined, spec: Spec | undef
export const prepareSpec = <Type extends Spec>(
spec: Type,
parseJsonDefaultValue?: boolean,
overridePatternError?: (pattern?: string) => string,
): Type => {
if (isObjectLike(spec)) {
const result: Record<string, any> = cloneDeep(spec);
Expand Down Expand Up @@ -140,8 +142,12 @@ export const prepareSpec = <Type extends Spec>(
}
}

if (!isEmpty(result.pattern) && isEmpty(result.patternError) && overridePatternError) {
result.patternError = overridePatternError(result.pattern);
}

if (result.items) {
result.items = prepareSpec(result.items, parseJsonDefaultValue);
result.items = prepareSpec(result.items, parseJsonDefaultValue, overridePatternError);
}

if (result.maximum === 0 && result.minimum === 0) {
Expand All @@ -163,7 +169,11 @@ export const prepareSpec = <Type extends Spec>(

if (isObjectLike(result.properties)) {
Object.keys(result.properties).forEach((key) => {
result.properties[key] = prepareSpec(result.properties[key], parseJsonDefaultValue);
result.properties[key] = prepareSpec(
result.properties[key],
parseJsonDefaultValue,
overridePatternError,
);
});
}

Expand Down
Loading