Skip to content

Commit 52a34bb

Browse files
committed
fix(builder): added ability to specify a custom error message
The builders didn't allow to specify custom error message, while such ability was present into the JSON config file. BREAKING CHANGE: Now the isIn rule builder requires the build method to be called
1 parent 8df6882 commit 52a34bb

File tree

7 files changed

+117
-20
lines changed

7 files changed

+117
-20
lines changed

src/example/nestedRulesSample-fluent.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ let data = {
1515
const validator = validatorBuilder()
1616
.newRule()
1717
.withField('platform')
18-
.validate(RuleBuilder.isIn.withValues('MOBILE', 'DESKTOP'))
18+
.validate(RuleBuilder.isIn.withValues('MOBILE', 'DESKTOP').build())
1919
.validate(RuleBuilder.required())
2020
.newRule()
2121
.withFieldValueConstraint('platform', 'mobile')
2222
.withField('os')
23-
.validate(RuleBuilder.isIn.withValues('ANDROID', 'iOS', 'WINDOWS'))
23+
.validate(RuleBuilder.isIn.withValues('ANDROID', 'iOS', 'WINDOWS').build())
2424
.validate(RuleBuilder.required())
2525
.withField('programming_lang')
2626
.validate(
@@ -30,29 +30,31 @@ const validator = validatorBuilder()
3030
RuleBuilder.composite
3131
.all()
3232
.withSubRule(RuleBuilder.exactValue.withPathAndValue('os', 'ANDROID'))
33-
.withSubRule(RuleBuilder.isIn.withValues('JAVA', 'KOTLIN'))
33+
.withSubRule(RuleBuilder.isIn.withValues('JAVA', 'KOTLIN').build())
3434
.build()
3535
)
3636
.withSubRule(
3737
RuleBuilder.composite
3838
.all()
3939
.withSubRule(RuleBuilder.exactValue.withPathAndValue('os', 'iOS'))
40-
.withSubRule(RuleBuilder.isIn.withValues('SWIFT', 'OBJECTIVE-C'))
40+
.withSubRule(
41+
RuleBuilder.isIn.withValues('SWIFT', 'OBJECTIVE-C').build()
42+
)
4143
.build()
4244
)
4345
.withSubRule(
4446
RuleBuilder.composite
4547
.all()
4648
.withSubRule(RuleBuilder.exactValue.withPathAndValue('os', 'WINDOWS'))
47-
.withSubRule(RuleBuilder.isIn.withValues('C#'))
49+
.withSubRule(RuleBuilder.isIn.withValues('C#').build())
4850
.build()
4951
)
5052
.build()
5153
)
5254
.newRule()
5355
.withFieldValueConstraint('platform', 'DESKTOP')
5456
.withField('os')
55-
.validate(RuleBuilder.isIn.withValues('WINDOWS', 'LINUX'))
57+
.validate(RuleBuilder.isIn.withValues('WINDOWS', 'LINUX').build())
5658
.withField('programming_lang')
5759
.validate(
5860
RuleBuilder.composite
@@ -61,14 +63,16 @@ const validator = validatorBuilder()
6163
RuleBuilder.composite
6264
.all()
6365
.withSubRule(RuleBuilder.exactValue.withPathAndValue('os', 'WINDOWS'))
64-
.withSubRule(RuleBuilder.isIn.withValues('JAVA', 'C/C++', 'C#'))
66+
.withSubRule(
67+
RuleBuilder.isIn.withValues('JAVA', 'C/C++', 'C#').build()
68+
)
6569
.build()
6670
)
6771
.withSubRule(
6872
RuleBuilder.composite
6973
.all()
7074
.withSubRule(RuleBuilder.exactValue.withPathAndValue('os', 'LINUX'))
71-
.withSubRule(RuleBuilder.isIn.withValues('JAVA', 'C/C++'))
75+
.withSubRule(RuleBuilder.isIn.withValues('JAVA', 'C/C++').build())
7276
.build()
7377
)
7478
.build()

src/rules/builders/CompositeRuleBuilder.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ export const builder = {
2020
return {withSubRule, build};
2121
};
2222

23-
const build = (): RuleConfig => ({
23+
const build = (errorMessage?: string): RuleConfig => ({
2424
type: 'COMPOSITE',
2525
algorithm: 'any',
26+
errorMessage,
2627
subRules,
2728
});
2829

@@ -38,9 +39,10 @@ export const builder = {
3839
return {withSubRule, build};
3940
};
4041

41-
const build = (): RuleConfig => ({
42+
const build = (errorMessage?: string): RuleConfig => ({
4243
type: 'COMPOSITE',
4344
algorithm: 'all',
45+
errorMessage,
4446
subRules,
4547
});
4648

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import {RuleConfig} from '../../config/RuleConfig';
22

33
export const builder = {
4-
withValue: (value: string | number): RuleConfig => ({
4+
withValue: (value: string | number, errorMessage?: string): RuleConfig => ({
55
type: 'EXACT_VALUE',
66
value,
7+
errorMessage,
78
}),
8-
withPathAndValue: (path: string, value: string | number): RuleConfig => ({
9+
withPathAndValue: (
10+
path: string,
11+
value: string | number,
12+
errorMessage?: string
13+
): RuleConfig => ({
914
type: 'EXACT_VALUE',
1015
path,
1116
value,
17+
errorMessage,
1218
}),
1319
};

src/rules/builders/IsInBuilder.ts

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,84 @@
11
import {RuleConfig} from '../../config/RuleConfig';
22

3-
export const builder = {
4-
withValues: (...values: string[]): RuleConfig => ({
5-
type: 'isIn',
6-
values: values.join(),
7-
}),
3+
export interface IsInBuilderInterface {
4+
withValues(...values: string[]): IsInBuilderFinalInterface;
5+
withValue(value: string): IsInBuilderFinalInterface;
6+
}
7+
8+
export interface IsInBuilderFinalInterface {
9+
withValues(...values: string[]): IsInBuilderFinalInterface;
10+
withValue(value: string): IsInBuilderFinalInterface;
11+
build(errorMessage?: string): RuleConfig;
12+
}
13+
14+
function createWithValueFunction(
15+
rule: RuleConfig
16+
): (value: string) => IsInBuilderFinalInterface {
17+
const withValue = (value: string): IsInBuilderFinalInterface => {
18+
(rule.values as string[]).push(value);
19+
20+
return {
21+
withValues: createWithValuesFunction(rule),
22+
withValue: createWithValueFunction(rule),
23+
build: createBuildFunction(rule),
24+
};
25+
};
26+
return withValue;
27+
}
28+
29+
function createWithValuesFunction(
30+
rule: RuleConfig
31+
): (value: string) => IsInBuilderFinalInterface {
32+
const withValues = (...values: string[]): IsInBuilderFinalInterface => {
33+
(rule.values as string[]) = [...(rule.values as string[]), ...values];
34+
35+
return {
36+
withValues: createWithValuesFunction(rule),
37+
withValue: createWithValueFunction(rule),
38+
build: createBuildFunction(rule),
39+
};
40+
};
41+
42+
return withValues;
43+
}
44+
45+
function createBuildFunction(
46+
rule: RuleConfig
47+
): (errorMessage: string) => RuleConfig {
48+
const build = (errorMessage?: string) => {
49+
return {
50+
type: rule.type,
51+
values: (rule.values as string[]).join(),
52+
errorMessage,
53+
};
54+
};
55+
56+
return build;
57+
}
58+
59+
export const builder: IsInBuilderInterface = {
60+
withValues: (...values: string[]): IsInBuilderFinalInterface => {
61+
const rule: RuleConfig = {
62+
type: 'isIn',
63+
values: values || [],
64+
};
65+
66+
return {
67+
withValues: createWithValuesFunction(rule),
68+
withValue: createWithValueFunction(rule),
69+
build: createBuildFunction(rule),
70+
};
71+
},
72+
withValue: (value: string): IsInBuilderFinalInterface => {
73+
const rule: RuleConfig = {
74+
type: 'isIn',
75+
values: [value],
76+
};
77+
78+
return {
79+
withValues: createWithValuesFunction(rule),
80+
withValue: createWithValueFunction(rule),
81+
build: createBuildFunction(rule),
82+
};
83+
},
884
};

src/rules/builders/LengthRuleBuilder.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@ import * as LengthRule from '../LengthRule';
22
import {RuleConfig} from '../../config/RuleConfig';
33

44
export const builder = {
5-
withLength: (length: number): RuleConfig => ({type: LengthRule.NAME, length}),
5+
withLength: (length: number, errorMessage?: string): RuleConfig => ({
6+
type: LengthRule.NAME,
7+
length,
8+
errorMessage,
9+
}),
610
};
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import {RuleConfig} from '../../config/RuleConfig';
22

33
export const builder = {
4-
withPattern: (pattern: string): RuleConfig => ({type: 'matches', pattern}),
4+
withPattern: (pattern: string, errorMessage?: string): RuleConfig => ({
5+
type: 'matches',
6+
pattern,
7+
errorMessage,
8+
}),
59
};
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {RuleConfig} from '../../config/RuleConfig';
22

33
export const builder = {
4-
required: (path?: string): RuleConfig => ({
4+
required: (path?: string, errorMessage?: string): RuleConfig => ({
55
type: 'REQUIRED',
66
path,
7+
errorMessage,
78
}),
89
};

0 commit comments

Comments
 (0)