6
6
import com .relogiclabs .json .schema .message .ExpectedDetail ;
7
7
import com .relogiclabs .json .schema .tree .RuntimeContext ;
8
8
import com .relogiclabs .json .schema .types .JArray ;
9
+ import com .relogiclabs .json .schema .types .JBoolean ;
9
10
import com .relogiclabs .json .schema .types .JNumber ;
10
11
import com .relogiclabs .json .schema .types .JObject ;
11
12
import com .relogiclabs .json .schema .types .JString ;
12
13
import com .relogiclabs .json .schema .types .JUndefined ;
13
14
14
15
import java .util .Arrays ;
16
+ import java .util .function .Supplier ;
15
17
16
- import static com .relogiclabs .json .schema .internal .util .StringHelper .toUnitedString ;
18
+ import static com .relogiclabs .json .schema .internal .util .StringHelper .join ;
17
19
import static com .relogiclabs .json .schema .message .ErrorCode .ENUM01 ;
18
20
import static com .relogiclabs .json .schema .message .ErrorCode .ENUM02 ;
21
+ import static com .relogiclabs .json .schema .message .ErrorCode .MAXI01 ;
22
+ import static com .relogiclabs .json .schema .message .ErrorCode .MAXI02 ;
23
+ import static com .relogiclabs .json .schema .message .ErrorCode .MAXI03 ;
24
+ import static com .relogiclabs .json .schema .message .ErrorCode .MINI01 ;
25
+ import static com .relogiclabs .json .schema .message .ErrorCode .MINI02 ;
26
+ import static com .relogiclabs .json .schema .message .ErrorCode .MINI03 ;
19
27
import static com .relogiclabs .json .schema .message .ErrorCode .NEGI01 ;
20
28
import static com .relogiclabs .json .schema .message .ErrorCode .NEMT01 ;
21
29
import static com .relogiclabs .json .schema .message .ErrorCode .NEMT02 ;
@@ -31,11 +39,12 @@ public CoreFunctions2(RuntimeContext runtime) {
31
39
super (runtime );
32
40
}
33
41
42
+ // enum is a keyword in Java but _ will be escaped
34
43
public boolean _enum (JString target , JString ... items ) {
35
44
var list = Arrays .asList (items );
36
45
if (!list .contains (target )) return failWith (new JsonSchemaException (
37
46
new ErrorDetail (ENUM01 , "String is not in enum list" ),
38
- new ExpectedDetail (function , "string in list " , toUnitedString (list , ", " , "[" , "]" )),
47
+ new ExpectedDetail (function , "string in list " , join (list , ", " , "[" , "]" )),
39
48
new ActualDetail (target , "string " , target , " is not found in list" )));
40
49
return true ;
41
50
}
@@ -44,11 +53,65 @@ public boolean _enum(JNumber target, JNumber... items) {
44
53
var list = Arrays .asList (items );
45
54
if (!list .contains (target )) return failWith (new JsonSchemaException (
46
55
new ErrorDetail (ENUM02 , "Number is not in enum list" ),
47
- new ExpectedDetail (function , "number in list " , toUnitedString (list , ", " , "[" , "]" )),
56
+ new ExpectedDetail (function , "number in list " , join (list , ", " , "[" , "]" )),
48
57
new ActualDetail (target , "number " , target , " is not found in list" )));
49
58
return true ;
50
59
}
51
60
61
+ public boolean minimum (JNumber target , JNumber minimum ) {
62
+ if (target .compare (minimum ) < 0 )
63
+ return failWith (new JsonSchemaException (
64
+ new ErrorDetail (MINI01 , "Number is less than provided minimum" ),
65
+ new ExpectedDetail (function , "a number greater than or equal to " , minimum ),
66
+ new ActualDetail (target , "number " , target , " is less than " , minimum )));
67
+ return true ;
68
+ }
69
+
70
+ public boolean minimum (JNumber target , JNumber minimum , JBoolean exclusive ) {
71
+ Supplier <String > relationTo = () -> exclusive .getValue ()
72
+ ? "greater than"
73
+ : "greater than or equal to" ;
74
+
75
+ if (target .compare (minimum ) < 0 )
76
+ return failWith (new JsonSchemaException (
77
+ new ErrorDetail (MINI02 , "Number is less than provided minimum" ),
78
+ new ExpectedDetail (function , "a number " , relationTo .get (), " " , minimum ),
79
+ new ActualDetail (target , "number " , target , " is less than " , minimum )));
80
+ if (exclusive .getValue () && target .compare (minimum ) == 0 )
81
+ return failWith (new JsonSchemaException (
82
+ new ErrorDetail (MINI03 , "Number is equal to provided minimum" ),
83
+ new ExpectedDetail (function , "a number " , relationTo .get (), " " , minimum ),
84
+ new ActualDetail (target , "number " , target , " is equal to " , minimum )));
85
+ return true ;
86
+ }
87
+
88
+ public boolean maximum (JNumber target , JNumber maximum ) {
89
+ if (target .compare (maximum ) > 0 )
90
+ return failWith (new JsonSchemaException (
91
+ new ErrorDetail (MAXI01 , "Number is greater than provided maximum" ),
92
+ new ExpectedDetail (function , "a number less than or equal " , maximum ),
93
+ new ActualDetail (target , "number " , target , " is greater than " , maximum )));
94
+ return true ;
95
+ }
96
+
97
+ public boolean maximum (JNumber target , JNumber maximum , JBoolean exclusive ) {
98
+ Supplier <String > relationTo = () -> exclusive .getValue ()
99
+ ? "less than"
100
+ : "less than or equal to" ;
101
+
102
+ if (target .compare (maximum ) > 0 )
103
+ return failWith (new JsonSchemaException (
104
+ new ErrorDetail (MAXI02 , "Number is greater than provided maximum" ),
105
+ new ExpectedDetail (function , "a number " , relationTo .get (), " " , maximum ),
106
+ new ActualDetail (target , "number " , target , " is greater than " , maximum )));
107
+ if (exclusive .getValue () && target .compare (maximum ) == 0 )
108
+ return failWith (new JsonSchemaException (
109
+ new ErrorDetail (MAXI03 , "Number is equal to provided maximum" ),
110
+ new ExpectedDetail (function , "a number " , relationTo .get (), " " , maximum ),
111
+ new ActualDetail (target , "number " , target , " is equal to " , maximum )));
112
+ return true ;
113
+ }
114
+
52
115
public boolean positive (JNumber target ) {
53
116
if (target .compare (0 ) <= 0 ) return failWith (new JsonSchemaException (
54
117
new ErrorDetail (POSI01 , "Number is not positive" ),
0 commit comments