10
10
import java .math .BigDecimal ;
11
11
import java .util .Collections ;
12
12
import java .util .List ;
13
+ import java .util .Optional ;
13
14
14
15
import static graphql .validation .constraints .GraphQLScalars .GRAPHQL_NUMBER_AND_STRING_TYPES ;
15
16
@@ -21,14 +22,14 @@ public DigitsConstraint() {
21
22
@ Override
22
23
public Documentation getDocumentation () {
23
24
return Documentation .newDocumentation ()
24
- .messageTemplate (getMessageTemplate ())
25
- .description ("The element must be a number inside the specified `integer` and `fraction` range." )
26
- .example ("buyCar( carCost : Float @Digits(integer : 5, fraction : 2) : DriverDetails" )
27
- .applicableTypes (GRAPHQL_NUMBER_AND_STRING_TYPES )
28
- .directiveSDL ("directive @Digits(integer : Int!, fraction : Int! , message : String = \" %s\" ) " +
29
- "on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION" ,
30
- getMessageTemplate ())
31
- .build ();
25
+ .messageTemplate (getMessageTemplate ())
26
+ .description ("The element must be a number inside the specified `integer` and optionally inside `fraction` range." )
27
+ .example ("buyCar( carCost : Float @Digits(integer : 5, fraction : 2) : DriverDetails" )
28
+ .applicableTypes (GRAPHQL_NUMBER_AND_STRING_TYPES )
29
+ .directiveSDL ("directive @Digits(integer : Int!, fraction : Int, message : String = \" %s\" ) " +
30
+ "on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION" ,
31
+ getMessageTemplate ())
32
+ .build ();
32
33
}
33
34
34
35
@ Override
@@ -43,28 +44,40 @@ protected List<GraphQLError> runConstraint(ValidationEnvironment validationEnvir
43
44
44
45
GraphQLAppliedDirective directive = validationEnvironment .getContextObject (GraphQLAppliedDirective .class );
45
46
int maxIntegerLength = getIntArg (directive , "integer" );
46
- int maxFractionLength = getIntArg (directive , "fraction" );
47
+ Optional < Integer > maxFractionLengthOpt = getIntArgOpt (directive , "fraction" );
47
48
48
49
boolean isOk ;
49
50
try {
50
51
BigDecimal bigNum = asBigDecimal (validatedValue );
51
- isOk = isOk (bigNum , maxIntegerLength , maxFractionLength );
52
+ boolean isFractionPartOk = maxFractionLengthOpt
53
+ .map (maxFractionLength -> isFractionPartOk (bigNum , maxFractionLength ))
54
+ .orElse (true );
55
+
56
+ isOk = isFractionPartOk && isIntegerPartOk (bigNum , maxIntegerLength );
52
57
} catch (NumberFormatException e ) {
53
58
isOk = false ;
54
59
}
55
60
56
61
if (!isOk ) {
57
- return mkError (validationEnvironment , "integer" , maxIntegerLength , "fraction" , maxFractionLength );
62
+ return mkError (
63
+ validationEnvironment ,
64
+ "integer" ,
65
+ maxIntegerLength , "fraction" ,
66
+ maxFractionLengthOpt .map (Object ::toString ).orElse ("unlimited" )
67
+ );
58
68
}
59
69
60
70
return Collections .emptyList ();
61
71
}
62
72
63
- private boolean isOk (BigDecimal bigNum , int maxIntegerLength , int maxFractionLength ) {
64
- int integerPartLength = bigNum .precision () - bigNum .scale ();
65
- int fractionPartLength = Math .max (bigNum .scale (), 0 );
73
+ private static boolean isIntegerPartOk (BigDecimal bigNum , int maxIntegerLength ) {
74
+ final int integerPartLength = bigNum .precision () - bigNum .scale ();
75
+ return maxIntegerLength >= integerPartLength ;
76
+ }
66
77
67
- return maxIntegerLength >= integerPartLength && maxFractionLength >= fractionPartLength ;
78
+ private static boolean isFractionPartOk (BigDecimal bigNum , int maxFractionLength ) {
79
+ final int fractionPartLength = Math .max (bigNum .scale (), 0 );
80
+ return maxFractionLength >= fractionPartLength ;
68
81
}
69
82
70
83
@ Override
0 commit comments