11package com .relogiclabs .json .schema .function ;
22
33import com .relogiclabs .json .schema .exception .JsonSchemaException ;
4+ import com .relogiclabs .json .schema .internal .function .DateTimeAgent ;
45import com .relogiclabs .json .schema .internal .time .DateTimeParser ;
5- import com .relogiclabs .json .schema .internal .time .DateTimeType ;
66import com .relogiclabs .json .schema .message .ActualDetail ;
77import com .relogiclabs .json .schema .message .ErrorDetail ;
88import com .relogiclabs .json .schema .message .ExpectedDetail ;
9+ import com .relogiclabs .json .schema .time .DateTimeType ;
910import com .relogiclabs .json .schema .tree .RuntimeContext ;
10- import com .relogiclabs .json .schema .types .JDateTime ;
11- import com .relogiclabs .json .schema .types .JString ;
12- import com .relogiclabs .json .schema .types .JUndefined ;
11+ import com .relogiclabs .json .schema .type .JDateTime ;
12+ import com .relogiclabs .json .schema .type .JString ;
13+ import com .relogiclabs .json .schema .type .JUndefined ;
1314
14- import static com .relogiclabs .json .schema .internal .time .DateTimeType .DATE_TYPE ;
1515import static com .relogiclabs .json .schema .message .ErrorCode .AFTR01 ;
1616import static com .relogiclabs .json .schema .message .ErrorCode .AFTR02 ;
1717import static com .relogiclabs .json .schema .message .ErrorCode .BFOR01 ;
2828import static com .relogiclabs .json .schema .message .ErrorCode .ENDE02 ;
2929import static com .relogiclabs .json .schema .message .ErrorCode .STRT01 ;
3030import static com .relogiclabs .json .schema .message .ErrorCode .STRT02 ;
31+ import static com .relogiclabs .json .schema .time .DateTimeType .DATE_TYPE ;
3132
3233public class CoreFunctions4 extends CoreFunctions3 {
3334 public CoreFunctions4 (RuntimeContext runtime ) {
@@ -77,48 +78,47 @@ public boolean range(JDateTime target, JString start, JString end) {
7778 if (rStart == null ) return false ;
7879 var rEnd = getDateTime (target .getDateTimeParser (), end );
7980 if (rEnd == null ) return false ;
80- boolean result = true ;
81- result &= isValidStart (target , rStart , DRNG01 , DRNG02 );
82- result &= isValidEnd (target , rEnd , DRNG03 , DRNG04 );
83- return result ;
81+ if (target .getDateTime ().compare (rStart .getDateTime ()) < 0 )
82+ return failOnStartDate (target , rStart , getErrorCode (target , DRNG01 , DRNG02 ));
83+ if (target .getDateTime ().compare (rEnd .getDateTime ()) > 0 )
84+ return failOnEndDate (target , rEnd , getErrorCode (target , DRNG03 , DRNG04 ));
85+ return true ;
86+ }
87+
88+ private static String getErrorCode (JDateTime target , String date , String time ) {
89+ return target .getDateTime ().getType () == DATE_TYPE ? date : time ;
90+ }
91+
92+ private boolean failOnStartDate (JDateTime target , JDateTime start , String code ) {
93+ var type = target .getDateTime ().getType ();
94+ return failWith (new JsonSchemaException (
95+ new ErrorDetail (code , type , " is earlier than start " , type ),
96+ new ExpectedDetail (start , "a " , type , " from or after " , start ),
97+ new ActualDetail (target , "found " , target , " which is before start " , type )
98+ ));
99+ }
100+
101+ private boolean failOnEndDate (JDateTime target , JDateTime end , String code ) {
102+ var type = target .getDateTime ().getType ();
103+ return failWith (new JsonSchemaException (
104+ new ErrorDetail (code , type , " is later than end " , type ),
105+ new ExpectedDetail (end , "a " , type , " until or before " , end ),
106+ new ActualDetail (target , "found " , target , " which is after end " , type )
107+ ));
84108 }
85109
86110 public boolean range (JDateTime target , JUndefined start , JString end ) {
87111 var rEnd = getDateTime (target .getDateTimeParser (), end );
88112 if (rEnd == null ) return false ;
89- return isValidEnd (target , rEnd , DRNG05 , DRNG06 );
113+ if (target .getDateTime ().compare (rEnd .getDateTime ()) <= 0 ) return true ;
114+ return failOnEndDate (target , rEnd , getErrorCode (target , DRNG05 , DRNG06 ));
90115 }
91116
92117 public boolean range (JDateTime target , JString start , JUndefined end ) {
93118 var rStart = getDateTime (target .getDateTimeParser (), start );
94119 if (rStart == null ) return false ;
95- return isValidStart (target , rStart , DRNG07 , DRNG08 );
96- }
97-
98- private boolean isValidStart (JDateTime target , JDateTime start , String codeDate , String codeTime ) {
99- if (target .getDateTime ().compare (start .getDateTime ()) < 0 ) {
100- var type = target .getDateTime ().getType ();
101- var code = type == DATE_TYPE ? codeDate : codeTime ;
102- return failWith (new JsonSchemaException (
103- new ErrorDetail (code , type , " is earlier than start " , type ),
104- new ExpectedDetail (start , "a " , type , " from or after " , start ),
105- new ActualDetail (target , "found " , target , " which is before start " , type )
106- ));
107- }
108- return true ;
109- }
110-
111- private boolean isValidEnd (JDateTime target , JDateTime end , String codeDate , String codeTime ) {
112- if (target .getDateTime ().compare (end .getDateTime ()) > 0 ) {
113- var type = target .getDateTime ().getType ();
114- var code = type == DATE_TYPE ? codeDate : codeTime ;
115- return failWith (new JsonSchemaException (
116- new ErrorDetail (code , type , " is later than end " , type ),
117- new ExpectedDetail (end , "a " , type , " until or before " , end ),
118- new ActualDetail (target , "found " , target , " which is after end " , type )
119- ));
120- }
121- return true ;
120+ if (target .getDateTime ().compare (rStart .getDateTime ()) >= 0 ) return true ;
121+ return failOnStartDate (target , rStart , getErrorCode (target , DRNG07 , DRNG08 ));
122122 }
123123
124124 public boolean start (JDateTime target , JString reference ) {
@@ -156,7 +156,7 @@ private JDateTime getDateTime(DateTimeParser parser, JString dateTime) {
156156 && result .getDateTime ().getType () == parser .getType ()) return result ;
157157 var jDateTime = new DateTimeAgent (parser ).parse (function , dateTime );
158158 if (jDateTime == null ) return null ;
159- dateTime .setDerived (jDateTime .create (dateTime ));
159+ dateTime .setDerived (jDateTime .createNode (dateTime ));
160160 return (JDateTime ) dateTime .getDerived ();
161161 }
162162}
0 commit comments