-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathdirective-mapping.ts
More file actions
110 lines (104 loc) · 2.42 KB
/
directive-mapping.ts
File metadata and controls
110 lines (104 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* GraphQL validation directives to Jakarta Validation annotations mapping
*/
export interface DirectiveArgument {
name: string;
value: string;
}
export const VALIDATION_DIRECTIVES: Record<string, string> = {
'@notBlank': 'NotBlank',
'@size': 'Size',
'@email': 'Email',
'@pattern': 'Pattern',
'@positive': 'Positive',
'@future': 'Future',
'@past': 'Past',
'@min': 'Min',
'@max': 'Max',
'@notNull': 'NotNull',
'@null': 'Null',
'@assertTrue': 'AssertTrue',
'@assertFalse': 'AssertFalse',
'@negative': 'Negative',
'@negativeOrZero': 'NegativeOrZero',
'@positiveOrZero': 'PositiveOrZero',
'@decimalMin': 'DecimalMin',
'@decimalMax': 'DecimalMax',
'@digits': 'Digits'
};
/**
* Validation directive parameter mapping
*/
export const VALIDATION_PARAM_MAPPING: Record<string, Record<string, string>> = {
'@size': {
'min': 'min',
'max': 'max',
'message': 'message'
},
'@pattern': {
'regexp': 'regexp',
'message': 'message'
},
'@min': {
'value': 'value',
'message': 'message'
},
'@max': {
'value': 'value',
'message': 'message'
},
'@decimalMin': {
'value': 'value',
'message': 'message'
},
'@decimalMax': {
'value': 'value',
'message': 'message'
},
'@digits': {
'integer': 'integer',
'fraction': 'fraction',
'message': 'message'
}
};
/**
* Parse GraphQL directive arguments
*/
export function parseDirectiveArgs(
directiveName: string,
args: any[]
): DirectiveArgument[] {
const paramMapping = VALIDATION_PARAM_MAPPING[directiveName] || {};
return args.map(arg => {
const argName = arg.name.value;
const mappedArgName = paramMapping[argName] || argName;
const value = formatArgValue(arg.value);
return {
name: mappedArgName,
value: value
};
});
}
/**
* Format argument values
*/
function formatArgValue(value: any): string {
switch (value.kind) {
case 'StringValue':
// Escape backslashes in string values
return `"${value.value.replace(/\\/g, '\\')}"`;
case 'IntValue':
case 'FloatValue':
case 'BooleanValue':
return value.value;
default:
// Escape backslashes in string values for default case
return `"${value.value.replace(/\\/g, '\\')}"`;
}
}
/**
* Check if directive is a validation directive
*/
export function isValidationDirective(directiveName: string): boolean {
return VALIDATION_DIRECTIVES.hasOwnProperty(`@${directiveName}`);
}