Skip to content

Commit 10768ff

Browse files
authored
feat: add ability to use custom patternError (#224)
1 parent 80b560f commit 10768ff

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/lib/kit/utils/__tests__/common.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,36 @@ describe('kit/utils/common', () => {
131131
expect(prepareSpec({properties: {prop: {type: 'NUMBER'}}} as any)).toMatchObject({
132132
properties: {prop: {type: 'number'}},
133133
});
134+
135+
const getErrorMessage = (regexp?: string) => {
136+
return `My custom error message for ${regexp}`;
137+
};
138+
139+
expect(prepareSpec({pattern: '[a-zA-Z0-9]'} as any, false, getErrorMessage)).toMatchObject({
140+
pattern: '[a-zA-Z0-9]',
141+
patternError: 'My custom error message for [a-zA-Z0-9]',
142+
});
143+
144+
expect(
145+
prepareSpec(
146+
{
147+
properties: {
148+
test: {
149+
pattern: '[a-zA-Z0-9]',
150+
},
151+
},
152+
} as any,
153+
false,
154+
getErrorMessage,
155+
),
156+
).toMatchObject({
157+
properties: {
158+
test: {
159+
pattern: '[a-zA-Z0-9]',
160+
patternError: 'My custom error message for [a-zA-Z0-9]',
161+
},
162+
},
163+
});
134164
});
135165

136166
test('isCorrectSizeParams', () => {

src/lib/kit/utils/common.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import isNil from 'lodash/isNil';
44
import isObject from 'lodash/isObject';
55
import isObjectLike from 'lodash/isObjectLike';
66
import isString from 'lodash/isString';
7+
import isEmpty from 'lodash/isEmpty';
78

89
import {
910
FormValue,
@@ -63,6 +64,7 @@ export const isNotEmptyValue = (value: FormValue | undefined, spec: Spec | undef
6364
export const prepareSpec = <Type extends Spec>(
6465
spec: Type,
6566
parseJsonDefaultValue?: boolean,
67+
overridePatternError?: (pattern?: string) => string,
6668
): Type => {
6769
if (isObjectLike(spec)) {
6870
const result: Record<string, any> = cloneDeep(spec);
@@ -140,8 +142,12 @@ export const prepareSpec = <Type extends Spec>(
140142
}
141143
}
142144

145+
if (!isEmpty(result.pattern) && isEmpty(result.patternError) && overridePatternError) {
146+
result.patternError = overridePatternError(result.pattern);
147+
}
148+
143149
if (result.items) {
144-
result.items = prepareSpec(result.items, parseJsonDefaultValue);
150+
result.items = prepareSpec(result.items, parseJsonDefaultValue, overridePatternError);
145151
}
146152

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

164170
if (isObjectLike(result.properties)) {
165171
Object.keys(result.properties).forEach((key) => {
166-
result.properties[key] = prepareSpec(result.properties[key], parseJsonDefaultValue);
172+
result.properties[key] = prepareSpec(
173+
result.properties[key],
174+
parseJsonDefaultValue,
175+
overridePatternError,
176+
);
167177
});
168178
}
169179

0 commit comments

Comments
 (0)