11package com .relogiclabs .jschema .function ;
22
3- import com .relogiclabs .jschema .exception .JsonSchemaException ;
3+ import com .relogiclabs .jschema .exception .FunctionValidationException ;
44import com .relogiclabs .jschema .message .ActualDetail ;
55import com .relogiclabs .jschema .message .ErrorDetail ;
66import com .relogiclabs .jschema .message .ExpectedDetail ;
1717import static com .relogiclabs .jschema .internal .util .CollectionHelper .containsValues ;
1818import static com .relogiclabs .jschema .internal .util .StreamHelper .count ;
1919import static com .relogiclabs .jschema .internal .util .StringHelper .quote ;
20- import static com .relogiclabs .jschema .message .ErrorCode .ELEM01 ;
21- import static com .relogiclabs .jschema .message .ErrorCode .EMAL01 ;
22- import static com .relogiclabs .jschema .message .ErrorCode .KEYS01 ;
23- import static com .relogiclabs .jschema .message .ErrorCode .PHON01 ;
24- import static com .relogiclabs .jschema .message .ErrorCode .REGX01 ;
25- import static com .relogiclabs .jschema .message .ErrorCode .URLA01 ;
26- import static com .relogiclabs .jschema .message .ErrorCode .URLA02 ;
27- import static com .relogiclabs .jschema .message .ErrorCode .URLA03 ;
28- import static com .relogiclabs .jschema .message .ErrorCode .URLA04 ;
29- import static com .relogiclabs .jschema .message .ErrorCode .VALU01 ;
20+ import static com .relogiclabs .jschema .message .ErrorCode .ELEMCF01 ;
21+ import static com .relogiclabs .jschema .message .ErrorCode .EMALCF01 ;
22+ import static com .relogiclabs .jschema .message .ErrorCode .KEYFND01 ;
23+ import static com .relogiclabs .jschema .message .ErrorCode .PHONCF01 ;
24+ import static com .relogiclabs .jschema .message .ErrorCode .REGXCF01 ;
25+ import static com .relogiclabs .jschema .message .ErrorCode .URLADR01 ;
26+ import static com .relogiclabs .jschema .message .ErrorCode .URLADR02 ;
27+ import static com .relogiclabs .jschema .message .ErrorCode .URLSCM01 ;
28+ import static com .relogiclabs .jschema .message .ErrorCode .URLSCM02 ;
29+ import static com .relogiclabs .jschema .message .ErrorCode .VALFND01 ;
3030
3131public abstract class CoreFunctions3 extends CoreFunctions2 {
3232 private static final String URI_SCHEME_HTTPS = "https" ;
@@ -44,10 +44,10 @@ public boolean elements(JArray target, JNode... items) {
4444 }
4545
4646 private boolean failOnElement (JArray target , JNode node ) {
47- return fail (new JsonSchemaException (
48- new ErrorDetail (ELEM01 , "Value is not an element of array" ),
49- new ExpectedDetail (caller , "array with value " + node ),
50- new ActualDetail (target , "not found in " + target .getOutline ())));
47+ return fail (new FunctionValidationException (
48+ new ErrorDetail (ELEMCF01 , "Element not found in target array" ),
49+ new ExpectedDetail (caller , "an array with element " + node ),
50+ new ActualDetail (target , "not found in target " + target .getOutline ())));
5151 }
5252
5353 public boolean keys (JObject target , JString ... items ) {
@@ -56,10 +56,10 @@ public boolean keys(JObject target, JString... items) {
5656 }
5757
5858 private boolean failOnKey (JObject target , String string ) {
59- return fail (new JsonSchemaException (
60- new ErrorDetail (KEYS01 , "Object does not contain the key " ),
61- new ExpectedDetail (caller , "object with key " + quote (string )),
62- new ActualDetail (target , "does not contain in " + target .getOutline ())));
59+ return fail (new FunctionValidationException (
60+ new ErrorDetail (KEYFND01 , "Key not found in target object " ),
61+ new ExpectedDetail (caller , "an object with key " + quote (string )),
62+ new ActualDetail (target , "not found in target " + target .getOutline ())));
6363 }
6464
6565 public boolean values (JObject target , JNode ... items ) {
@@ -68,82 +68,79 @@ public boolean values(JObject target, JNode... items) {
6868 }
6969
7070 private boolean failOnValue (JObject target , JNode node ) {
71- return fail (new JsonSchemaException (
72- new ErrorDetail (VALU01 , "Object does not contain the value " ),
73- new ExpectedDetail (caller , "object with value " + node ),
74- new ActualDetail (target , "does not contain in " + target .getOutline ())));
71+ return fail (new FunctionValidationException (
72+ new ErrorDetail (VALFND01 , "Value not found in target object " ),
73+ new ExpectedDetail (caller , "an object with value " + node ),
74+ new ActualDetail (target , "not found in target " + target .getOutline ())));
7575 }
7676
7777 public boolean regex (JString target , JString pattern ) {
7878 if (!Pattern .matches (pattern .getValue (), target .getValue ()))
79- return fail (new JsonSchemaException (
80- new ErrorDetail (REGX01 , "Regex pattern does not match" ),
81- new ExpectedDetail (caller , "string of pattern " + pattern .getOutline ()),
82- new ActualDetail (target , "found " + target .getOutline ()
83- + " that mismatches with pattern" )));
79+ return fail (new FunctionValidationException (
80+ new ErrorDetail (REGXCF01 , "Target mismatched with regex pattern" ),
81+ new ExpectedDetail (caller , "a string following pattern " + pattern ),
82+ new ActualDetail (target , "mismatched for target " + target .getOutline ())));
8483 return true ;
8584 }
8685
8786 public boolean email (JString target ) {
8887 // Based on SMTP protocol RFC 5322
8988 if (!EMAIL_REGEX .matcher (target .getValue ()).matches ())
90- return fail (new JsonSchemaException (
91- new ErrorDetail (EMAL01 , "Invalid email address" ),
89+ return fail (new FunctionValidationException (
90+ new ErrorDetail (EMALCF01 , "Invalid target email address" ),
9291 new ExpectedDetail (caller , "a valid email address" ),
93- new ActualDetail (target , "found " + target + " that is invalid" )));
92+ new ActualDetail (target , "found malformed target " + target )));
9493 return true ;
9594 }
9695
9796 public boolean url (JString target ) {
98- boolean result ;
9997 URI uri ;
10098 try {
10199 uri = new URI (target .getValue ());
102- result = switch (uri .getScheme ()) {
103- case URI_SCHEME_HTTP , URI_SCHEME_HTTPS -> true ;
104- default -> false ;
105- };
106100 } catch (Exception e ) {
107- return fail (new JsonSchemaException (
108- new ErrorDetail (URLA01 , "Invalid url address" ),
109- new ExpectedDetail (caller , "a valid url address" ),
110- new ActualDetail (target , "found " + target + " that is invalid" )));
101+ return fail (new FunctionValidationException (
102+ new ErrorDetail (URLADR01 , "Invalid target URL address" ),
103+ new ExpectedDetail (caller , "a valid URL address" ),
104+ new ActualDetail (target , "found malformed target " + target )));
111105 }
112- if (!result ) return fail (new JsonSchemaException (
113- new ErrorDetail (URLA02 , "Invalid url address scheme" ),
114- new ExpectedDetail (caller , "HTTP or HTTPS scheme" ),
115- new ActualDetail (target , "found " + quote (uri .getScheme ()) + " from "
116- + target + " that has invalid scheme" )));
106+ var result = switch (uri .getScheme ()) {
107+ case URI_SCHEME_HTTP , URI_SCHEME_HTTPS -> true ;
108+ default -> false ;
109+ };
110+ if (!result ) return fail (new FunctionValidationException (
111+ new ErrorDetail (URLADR02 , "Mismatched target URL address scheme" ),
112+ new ExpectedDetail (caller , "A URL with HTTP or HTTPS scheme" ),
113+ new ActualDetail (target , "found scheme " + quote (uri .getScheme ())
114+ + " for target " + target )));
117115 return true ;
118116 }
119117
120118 public boolean url (JString target , JString scheme ) {
121- boolean result ;
122119 URI uri ;
123120 try {
124121 uri = new URI (target .getValue ());
125122 } catch (Exception e ) {
126- return fail (new JsonSchemaException (
127- new ErrorDetail (URLA03 , "Invalid url address" ),
128- new ExpectedDetail (caller , "a valid url address" ),
129- new ActualDetail (target , "found " + target + " that is invalid" )));
123+ return fail (new FunctionValidationException (
124+ new ErrorDetail (URLSCM01 , "Invalid target URL address" ),
125+ new ExpectedDetail (caller , "a valid URL address" ),
126+ new ActualDetail (target , "found malformed target " + target )));
130127 }
131- result = scheme .getValue ().equals (uri .getScheme ());
132- if (!result ) return fail (new JsonSchemaException (
133- new ErrorDetail (URLA04 , "Mismatch url address scheme" ),
134- new ExpectedDetail (caller , "scheme " + scheme + " for url address" ),
135- new ActualDetail (target , "found " + quote (uri .getScheme ()) + " from "
136- + target + " that does not matched" )));
128+ var result = scheme .getValue ().equals (uri .getScheme ());
129+ if (!result ) return fail (new FunctionValidationException (
130+ new ErrorDetail (URLSCM02 , "Mismatched target URL address scheme" ),
131+ new ExpectedDetail (caller , "A URL with scheme " + scheme ),
132+ new ActualDetail (target , "found scheme " + quote (uri .getScheme ())
133+ + " for target " + target )));
137134 return true ;
138135 }
139136
140137 public boolean phone (JString target ) {
141- // Based on ITU-T E.163 and E.164 (extended )
138+ // Based on ITU-T E.163 and E.164 (including widely used )
142139 if (!PHONE_REGEX .matcher (target .getValue ()).matches ())
143- return fail (new JsonSchemaException (
144- new ErrorDetail (PHON01 , "Invalid phone number format" ),
140+ return fail (new FunctionValidationException (
141+ new ErrorDetail (PHONCF01 , "Invalid target phone number format" ),
145142 new ExpectedDetail (caller , "a valid phone number" ),
146- new ActualDetail (target , "found " + target + " that is invalid" )));
143+ new ActualDetail (target , "found malformed target " + target )));
147144 return true ;
148145 }
149146}
0 commit comments