1
+ package com .relogiclabs .jschema .function ;
2
+
3
+ import com .relogiclabs .jschema .exception .JsonSchemaException ;
4
+ import com .relogiclabs .jschema .message .ActualDetail ;
5
+ import com .relogiclabs .jschema .message .ErrorDetail ;
6
+ import com .relogiclabs .jschema .message .ExpectedDetail ;
7
+ import com .relogiclabs .jschema .node .JArray ;
8
+ import com .relogiclabs .jschema .node .JBoolean ;
9
+ import com .relogiclabs .jschema .node .JNumber ;
10
+ import com .relogiclabs .jschema .node .JObject ;
11
+ import com .relogiclabs .jschema .node .JString ;
12
+ import com .relogiclabs .jschema .node .JUndefined ;
13
+ import com .relogiclabs .jschema .tree .RuntimeContext ;
14
+
15
+ import java .util .Arrays ;
16
+ import java .util .function .Supplier ;
17
+
18
+ import static com .relogiclabs .jschema .internal .util .StringHelper .joinWith ;
19
+ import static com .relogiclabs .jschema .message .ErrorCode .ENUM01 ;
20
+ import static com .relogiclabs .jschema .message .ErrorCode .ENUM02 ;
21
+ import static com .relogiclabs .jschema .message .ErrorCode .MAXI01 ;
22
+ import static com .relogiclabs .jschema .message .ErrorCode .MAXI02 ;
23
+ import static com .relogiclabs .jschema .message .ErrorCode .MAXI03 ;
24
+ import static com .relogiclabs .jschema .message .ErrorCode .MINI01 ;
25
+ import static com .relogiclabs .jschema .message .ErrorCode .MINI02 ;
26
+ import static com .relogiclabs .jschema .message .ErrorCode .MINI03 ;
27
+ import static com .relogiclabs .jschema .message .ErrorCode .NEGI01 ;
28
+ import static com .relogiclabs .jschema .message .ErrorCode .NEGI02 ;
29
+ import static com .relogiclabs .jschema .message .ErrorCode .NEMT01 ;
30
+ import static com .relogiclabs .jschema .message .ErrorCode .NEMT02 ;
31
+ import static com .relogiclabs .jschema .message .ErrorCode .NEMT03 ;
32
+ import static com .relogiclabs .jschema .message .ErrorCode .POSI01 ;
33
+ import static com .relogiclabs .jschema .message .ErrorCode .POSI02 ;
34
+ import static com .relogiclabs .jschema .message .ErrorCode .RANG01 ;
35
+ import static com .relogiclabs .jschema .message .ErrorCode .RANG02 ;
36
+ import static com .relogiclabs .jschema .message .ErrorCode .RANG03 ;
37
+ import static com .relogiclabs .jschema .message .ErrorCode .RANG04 ;
38
+
39
+ public abstract class CoreFunctions2 extends CoreFunctions1 {
40
+ public CoreFunctions2 (RuntimeContext runtime ) {
41
+ super (runtime );
42
+ }
43
+
44
+ // enum is a keyword in Java but _ will be escaped
45
+ public boolean _enum (JString target , JString ... items ) {
46
+ var list = Arrays .asList (items );
47
+ if (!list .contains (target )) return fail (new JsonSchemaException (
48
+ new ErrorDetail (ENUM01 , "String is not in enum list" ),
49
+ new ExpectedDetail (function , "string in list " , joinWith (list , ", " , "[" , "]" )),
50
+ new ActualDetail (target , "string " , target , " is not found in list" )));
51
+ return true ;
52
+ }
53
+
54
+ public boolean _enum (JNumber target , JNumber ... items ) {
55
+ var list = Arrays .asList (items );
56
+ if (!list .contains (target )) return fail (new JsonSchemaException (
57
+ new ErrorDetail (ENUM02 , "Number is not in enum list" ),
58
+ new ExpectedDetail (function , "number in list " , joinWith (list , ", " , "[" , "]" )),
59
+ new ActualDetail (target , "number " , target , " is not found in list" )));
60
+ return true ;
61
+ }
62
+
63
+ public boolean minimum (JNumber target , JNumber minimum ) {
64
+ if (target .compare (minimum ) < 0 )
65
+ return fail (new JsonSchemaException (
66
+ new ErrorDetail (MINI01 , "Number is less than provided minimum" ),
67
+ new ExpectedDetail (function , "a number greater than or equal to " , minimum ),
68
+ new ActualDetail (target , "number " , target , " is less than " , minimum )));
69
+ return true ;
70
+ }
71
+
72
+ public boolean minimum (JNumber target , JNumber minimum , JBoolean exclusive ) {
73
+ Supplier <String > relationTo = () -> exclusive .getValue ()
74
+ ? "greater than"
75
+ : "greater than or equal to" ;
76
+
77
+ if (target .compare (minimum ) < 0 )
78
+ return fail (new JsonSchemaException (
79
+ new ErrorDetail (MINI02 , "Number is less than provided minimum" ),
80
+ new ExpectedDetail (function , "a number " , relationTo .get (), " " , minimum ),
81
+ new ActualDetail (target , "number " , target , " is less than " , minimum )));
82
+ if (exclusive .getValue () && target .compare (minimum ) == 0 )
83
+ return fail (new JsonSchemaException (
84
+ new ErrorDetail (MINI03 , "Number is equal to provided minimum" ),
85
+ new ExpectedDetail (function , "a number " , relationTo .get (), " " , minimum ),
86
+ new ActualDetail (target , "number " , target , " is equal to " , minimum )));
87
+ return true ;
88
+ }
89
+
90
+ public boolean maximum (JNumber target , JNumber maximum ) {
91
+ if (target .compare (maximum ) > 0 )
92
+ return fail (new JsonSchemaException (
93
+ new ErrorDetail (MAXI01 , "Number is greater than provided maximum" ),
94
+ new ExpectedDetail (function , "a number less than or equal " , maximum ),
95
+ new ActualDetail (target , "number " , target , " is greater than " , maximum )));
96
+ return true ;
97
+ }
98
+
99
+ public boolean maximum (JNumber target , JNumber maximum , JBoolean exclusive ) {
100
+ Supplier <String > relationTo = () -> exclusive .getValue ()
101
+ ? "less than"
102
+ : "less than or equal to" ;
103
+
104
+ if (target .compare (maximum ) > 0 )
105
+ return fail (new JsonSchemaException (
106
+ new ErrorDetail (MAXI02 , "Number is greater than provided maximum" ),
107
+ new ExpectedDetail (function , "a number " , relationTo .get (), " " , maximum ),
108
+ new ActualDetail (target , "number " , target , " is greater than " , maximum )));
109
+ if (exclusive .getValue () && target .compare (maximum ) == 0 )
110
+ return fail (new JsonSchemaException (
111
+ new ErrorDetail (MAXI03 , "Number is equal to provided maximum" ),
112
+ new ExpectedDetail (function , "a number " , relationTo .get (), " " , maximum ),
113
+ new ActualDetail (target , "number " , target , " is equal to " , maximum )));
114
+ return true ;
115
+ }
116
+
117
+ public boolean positive (JNumber target ) {
118
+ if (target .compare (0 ) <= 0 ) return fail (new JsonSchemaException (
119
+ new ErrorDetail (POSI01 , "Number is not positive" ),
120
+ new ExpectedDetail (function , "a positive number" ),
121
+ new ActualDetail (target , "number " , target , " is less than or equal to zero" )));
122
+ return true ;
123
+ }
124
+
125
+ public boolean negative (JNumber target ) {
126
+ if (target .compare (0 ) >= 0 ) return fail (new JsonSchemaException (
127
+ new ErrorDetail (NEGI01 , "Number is not negative" ),
128
+ new ExpectedDetail (function , "a negative number" ),
129
+ new ActualDetail (target , "number " , target , " is greater than or equal to zero" )));
130
+ return true ;
131
+ }
132
+
133
+ public boolean positive (JNumber target , JNumber reference ) {
134
+ if (target .compare (reference ) < 0 ) return fail (new JsonSchemaException (
135
+ new ErrorDetail (POSI02 , "Number is not positive from reference" ),
136
+ new ExpectedDetail (function , "a positive number from " , reference ),
137
+ new ActualDetail (target , "number " , target , " is less than reference" )));
138
+ return true ;
139
+ }
140
+
141
+ public boolean negative (JNumber target , JNumber reference ) {
142
+ if (target .compare (reference ) > 0 ) return fail (new JsonSchemaException (
143
+ new ErrorDetail (NEGI02 , "Number is not negative from reference" ),
144
+ new ExpectedDetail (function , "a negative number from " , reference ),
145
+ new ActualDetail (target , "number " , target , " is greater than reference" )));
146
+ return true ;
147
+ }
148
+
149
+ public boolean range (JNumber target , JNumber minimum , JNumber maximum ) {
150
+ if (target .compare (minimum ) < 0 ) return fail (new JsonSchemaException (
151
+ new ErrorDetail (RANG01 , "Number is outside of range" ),
152
+ new ExpectedDetail (function , "number in range [" , minimum , ", " , maximum , "]" ),
153
+ new ActualDetail (target , "number " , target , " is less than " , minimum )));
154
+ if (target .compare (maximum ) > 0 ) return fail (new JsonSchemaException (
155
+ new ErrorDetail (RANG02 , "Number is outside of range" ),
156
+ new ExpectedDetail (function , "number in range [" , minimum , ", " , maximum , "]" ),
157
+ new ActualDetail (target , "number " , target , " is greater than " , maximum )));
158
+ return true ;
159
+ }
160
+
161
+ public boolean range (JNumber target , JNumber minimum , JUndefined undefined ) {
162
+ if (target .compare (minimum ) < 0 ) return fail (new JsonSchemaException (
163
+ new ErrorDetail (RANG03 , "Number is outside of range" ),
164
+ new ExpectedDetail (function , "number in range [" , minimum , ", " , undefined , "]" ),
165
+ new ActualDetail (target , "number " , target , " is less than " , minimum )));
166
+ return true ;
167
+ }
168
+
169
+ public boolean range (JNumber target , JUndefined undefined , JNumber maximum ) {
170
+ if (target .compare (maximum ) > 0 ) return fail (new JsonSchemaException (
171
+ new ErrorDetail (RANG04 , "Number is outside of range" ),
172
+ new ExpectedDetail (function , "number in range [" , undefined , ", " , maximum , "]" ),
173
+ new ActualDetail (target , "number " , target , " is greater than " , maximum )));
174
+ return true ;
175
+ }
176
+
177
+ public boolean nonempty (JString target ) {
178
+ var length = target .length ();
179
+ if (length <= 0 ) return fail (new JsonSchemaException (
180
+ new ErrorDetail (NEMT01 , "String is empty" ),
181
+ new ExpectedDetail (function , "Non empty string" ),
182
+ new ActualDetail (target , "found empty string" )));
183
+ return true ;
184
+ }
185
+
186
+ public boolean nonempty (JArray target ) {
187
+ var length = target .getElements ().size ();
188
+ if (length <= 0 ) return fail (new JsonSchemaException (
189
+ new ErrorDetail (NEMT02 , "Array is empty" ),
190
+ new ExpectedDetail (function , "Non empty array" ),
191
+ new ActualDetail (target , "found empty array" )));
192
+ return true ;
193
+ }
194
+
195
+ public boolean nonempty (JObject target ) {
196
+ var length = target .getProperties ().size ();
197
+ if (length <= 0 ) return fail (new JsonSchemaException (
198
+ new ErrorDetail (NEMT03 , "Object is empty" ),
199
+ new ExpectedDetail (function , "Non empty object" ),
200
+ new ActualDetail (target , "found empty object" )));
201
+ return true ;
202
+ }
203
+ }
0 commit comments