1+ package com .relogiclabs .json .schema .function ;
2+
3+ import com .relogiclabs .json .schema .exception .JsonSchemaException ;
4+ import com .relogiclabs .json .schema .internal .time .DateTimeParser ;
5+ import com .relogiclabs .json .schema .internal .time .DateTimeType ;
6+ import com .relogiclabs .json .schema .message .ActualDetail ;
7+ import com .relogiclabs .json .schema .message .ErrorDetail ;
8+ import com .relogiclabs .json .schema .message .ExpectedDetail ;
9+ import 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 ;
13+
14+ import static com .relogiclabs .json .schema .internal .time .DateTimeType .DATE_TYPE ;
15+ import static com .relogiclabs .json .schema .message .ErrorCode .AFTR01 ;
16+ import static com .relogiclabs .json .schema .message .ErrorCode .AFTR02 ;
17+ import static com .relogiclabs .json .schema .message .ErrorCode .BFOR01 ;
18+ import static com .relogiclabs .json .schema .message .ErrorCode .BFOR02 ;
19+ import static com .relogiclabs .json .schema .message .ErrorCode .DRNG01 ;
20+ import static com .relogiclabs .json .schema .message .ErrorCode .DRNG02 ;
21+ import static com .relogiclabs .json .schema .message .ErrorCode .DRNG03 ;
22+ import static com .relogiclabs .json .schema .message .ErrorCode .DRNG04 ;
23+ import static com .relogiclabs .json .schema .message .ErrorCode .DRNG05 ;
24+ import static com .relogiclabs .json .schema .message .ErrorCode .DRNG06 ;
25+ import static com .relogiclabs .json .schema .message .ErrorCode .DRNG07 ;
26+ import static com .relogiclabs .json .schema .message .ErrorCode .DRNG08 ;
27+ import static com .relogiclabs .json .schema .message .ErrorCode .ENDE01 ;
28+ import static com .relogiclabs .json .schema .message .ErrorCode .ENDE02 ;
29+ import static com .relogiclabs .json .schema .message .ErrorCode .STRT01 ;
30+ import static com .relogiclabs .json .schema .message .ErrorCode .STRT02 ;
31+
32+ public class CoreFunctions4 extends CoreFunctions3 {
33+ public CoreFunctions4 (RuntimeContext runtime ) {
34+ super (runtime );
35+ }
36+
37+ public boolean date (JString target , JString pattern ) {
38+ return dateTime (target , pattern , DATE_TYPE );
39+ }
40+
41+ public boolean time (JString target , JString pattern ) {
42+ return dateTime (target , pattern , DateTimeType .TIME_TYPE );
43+ }
44+
45+ private boolean dateTime (JString target , JString pattern , DateTimeType type ) {
46+ return new DateTimeAgent (pattern .getValue (), type ).parse (function , target ) != null ;
47+ }
48+
49+ public boolean before (JDateTime target , JString reference ) {
50+ var dateTime = getDateTime (target .getDateTimeParser (), reference );
51+ if (dateTime == null ) return false ;
52+ if (target .getDateTime ().compare (dateTime .getDateTime ()) < 0 ) return true ;
53+ var type = target .getDateTime ().getType ();
54+ var code = type == DATE_TYPE ? BFOR01 : BFOR02 ;
55+ return failWith (new JsonSchemaException (
56+ new ErrorDetail (code , type , " is not earlier than specified" ),
57+ new ExpectedDetail (reference , "a " , type , " before " , reference ),
58+ new ActualDetail (target , "found " , target , " which is not inside limit" )
59+ ));
60+ }
61+
62+ public boolean after (JDateTime target , JString reference ) {
63+ var dateTime = getDateTime (target .getDateTimeParser (), reference );
64+ if (dateTime == null ) return false ;
65+ if (target .getDateTime ().compare (dateTime .getDateTime ()) > 0 ) return true ;
66+ var type = target .getDateTime ().getType ();
67+ var code = type == DATE_TYPE ? AFTR01 : AFTR02 ;
68+ return failWith (new JsonSchemaException (
69+ new ErrorDetail (code , type , " is not later than specified" ),
70+ new ExpectedDetail (reference , "a " , type , " after " , reference ),
71+ new ActualDetail (target , "found " , target , " which is not inside limit" )
72+ ));
73+ }
74+
75+ public boolean range (JDateTime target , JString start , JString end ) {
76+ var rStart = getDateTime (target .getDateTimeParser (), start );
77+ if (rStart == null ) return false ;
78+ var rEnd = getDateTime (target .getDateTimeParser (), end );
79+ 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 ;
84+ }
85+
86+ public boolean range (JDateTime target , JUndefined start , JString end ) {
87+ var rEnd = getDateTime (target .getDateTimeParser (), end );
88+ if (rEnd == null ) return false ;
89+ return isValidEnd (target , rEnd , DRNG05 , DRNG06 );
90+ }
91+
92+ public boolean range (JDateTime target , JString start , JUndefined end ) {
93+ var rStart = getDateTime (target .getDateTimeParser (), start );
94+ 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 ;
122+ }
123+
124+ public boolean start (JDateTime target , JString reference ) {
125+ var dateTime = getDateTime (target .getDateTimeParser (), reference );
126+ if (dateTime == null ) return false ;
127+ if (target .getDateTime ().compare (dateTime .getDateTime ()) < 0 ) {
128+ var type = target .getDateTime ().getType ();
129+ var code = type == DATE_TYPE ? STRT01 : STRT02 ;
130+ return failWith (new JsonSchemaException (
131+ new ErrorDetail (code , type , " is earlier than specified" ),
132+ new ExpectedDetail (dateTime , "a " , type , " from or after " , dateTime ),
133+ new ActualDetail (target , "found " , target , " which is before limit" )
134+ ));
135+ }
136+ return true ;
137+ }
138+
139+ public boolean end (JDateTime target , JString reference ) {
140+ var dateTime = getDateTime (target .getDateTimeParser (), reference );
141+ if (dateTime == null ) return false ;
142+ if (target .getDateTime ().compare (dateTime .getDateTime ()) > 0 ) {
143+ var type = target .getDateTime ().getType ();
144+ var code = type == DATE_TYPE ? ENDE01 : ENDE02 ;
145+ return failWith (new JsonSchemaException (
146+ new ErrorDetail (code , type , " is later than specified" ),
147+ new ExpectedDetail (dateTime , "a " , type , " until or before " , dateTime ),
148+ new ActualDetail (target , "found " , target , " which is after limit" )
149+ ));
150+ }
151+ return true ;
152+ }
153+
154+ private JDateTime getDateTime (DateTimeParser parser , JString dateTime ) {
155+ if (dateTime .getDerived () instanceof JDateTime result
156+ && result .getDateTime ().getType () == parser .getType ()) return result ;
157+ var jDateTime = new DateTimeAgent (parser ).parse (function , dateTime );
158+ if (jDateTime == null ) return null ;
159+ dateTime .setDerived (jDateTime .create (dateTime ));
160+ return (JDateTime ) dateTime .getDerived ();
161+ }
162+ }
0 commit comments