66import com .relogiclabs .json .schema .message .ExpectedDetail ;
77import com .relogiclabs .json .schema .tree .RuntimeContext ;
88import com .relogiclabs .json .schema .types .JArray ;
9+ import com .relogiclabs .json .schema .types .JBoolean ;
910import com .relogiclabs .json .schema .types .JNumber ;
1011import com .relogiclabs .json .schema .types .JObject ;
1112import com .relogiclabs .json .schema .types .JString ;
1213import com .relogiclabs .json .schema .types .JUndefined ;
1314
1415import java .util .Arrays ;
16+ import java .util .function .Supplier ;
1517
16- import static com .relogiclabs .json .schema .internal .util .StringHelper .toUnitedString ;
18+ import static com .relogiclabs .json .schema .internal .util .StringHelper .join ;
1719import static com .relogiclabs .json .schema .message .ErrorCode .ENUM01 ;
1820import 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 ;
1927import static com .relogiclabs .json .schema .message .ErrorCode .NEGI01 ;
2028import static com .relogiclabs .json .schema .message .ErrorCode .NEMT01 ;
2129import static com .relogiclabs .json .schema .message .ErrorCode .NEMT02 ;
@@ -31,11 +39,12 @@ public CoreFunctions2(RuntimeContext runtime) {
3139 super (runtime );
3240 }
3341
42+ // enum is a keyword in Java but _ will be escaped
3443 public boolean _enum (JString target , JString ... items ) {
3544 var list = Arrays .asList (items );
3645 if (!list .contains (target )) return failWith (new JsonSchemaException (
3746 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 , ", " , "[" , "]" )),
3948 new ActualDetail (target , "string " , target , " is not found in list" )));
4049 return true ;
4150 }
@@ -44,11 +53,65 @@ public boolean _enum(JNumber target, JNumber... items) {
4453 var list = Arrays .asList (items );
4554 if (!list .contains (target )) return failWith (new JsonSchemaException (
4655 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 , ", " , "[" , "]" )),
4857 new ActualDetail (target , "number " , target , " is not found in list" )));
4958 return true ;
5059 }
5160
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+
52115 public boolean positive (JNumber target ) {
53116 if (target .compare (0 ) <= 0 ) return failWith (new JsonSchemaException (
54117 new ErrorDetail (POSI01 , "Number is not positive" ),
0 commit comments