|
1 | 1 | import {RuleConfig} from '../../config/RuleConfig';
|
2 | 2 |
|
| 3 | +export interface RequiredRuleBuilder { |
| 4 | + withPath: (path: string) => RequiredRuleBuilder; |
| 5 | + withErrorMessage: (errorMessage: string) => RequiredRuleBuilder; |
| 6 | + build: () => RuleConfig; |
| 7 | +} |
| 8 | + |
| 9 | +const createWithPath = ( |
| 10 | + rule: RuleConfig |
| 11 | +): ((path: string) => RequiredRuleBuilder) => { |
| 12 | + const withPath = (path: string) => { |
| 13 | + rule.path = path; |
| 14 | + return { |
| 15 | + withPath: createWithPath(rule), |
| 16 | + withErrorMessage: createWithErrorMessage(rule), |
| 17 | + build: createBuild(rule), |
| 18 | + }; |
| 19 | + }; |
| 20 | + |
| 21 | + return withPath; |
| 22 | +}; |
| 23 | + |
| 24 | +const createWithErrorMessage = ( |
| 25 | + rule: RuleConfig |
| 26 | +): ((path: string) => RequiredRuleBuilder) => { |
| 27 | + const withErrorMessage = (errorMessage: string) => { |
| 28 | + rule.errorMessage = errorMessage; |
| 29 | + return { |
| 30 | + withPath: createWithPath(rule), |
| 31 | + withErrorMessage: createWithErrorMessage(rule), |
| 32 | + build: createBuild(rule), |
| 33 | + }; |
| 34 | + }; |
| 35 | + |
| 36 | + return withErrorMessage; |
| 37 | +}; |
| 38 | + |
| 39 | +const createBuild = (rule: RuleConfig): (() => RuleConfig) => () => rule; |
| 40 | + |
3 | 41 | export const builder = {
|
4 |
| - required: (path?: string, errorMessage?: string): RuleConfig => ({ |
5 |
| - type: 'REQUIRED', |
6 |
| - path, |
7 |
| - errorMessage, |
8 |
| - }), |
| 42 | + required: { |
| 43 | + withPath: createWithPath({type: 'REQUIRED'}), |
| 44 | + withErrorMessage: createWithErrorMessage({type: 'REQUIRED'}), |
| 45 | + build: createBuild({type: 'REQUIRED'}), |
| 46 | + }, |
9 | 47 | };
|
0 commit comments