1
1
package com .relogiclabs .jschema .function ;
2
2
3
- import com .relogiclabs .jschema .exception .JsonSchemaException ;
3
+ import com .relogiclabs .jschema .exception .FunctionValidationException ;
4
4
import com .relogiclabs .jschema .message .ActualDetail ;
5
5
import com .relogiclabs .jschema .message .ErrorDetail ;
6
6
import com .relogiclabs .jschema .message .ExpectedDetail ;
17
17
import static com .relogiclabs .jschema .internal .util .CollectionHelper .containsValues ;
18
18
import static com .relogiclabs .jschema .internal .util .StreamHelper .count ;
19
19
import 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 ;
30
30
31
31
public abstract class CoreFunctions3 extends CoreFunctions2 {
32
32
private static final String URI_SCHEME_HTTPS = "https" ;
@@ -44,10 +44,10 @@ public boolean elements(JArray target, JNode... items) {
44
44
}
45
45
46
46
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 ())));
51
51
}
52
52
53
53
public boolean keys (JObject target , JString ... items ) {
@@ -56,10 +56,10 @@ public boolean keys(JObject target, JString... items) {
56
56
}
57
57
58
58
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 ())));
63
63
}
64
64
65
65
public boolean values (JObject target , JNode ... items ) {
@@ -68,82 +68,79 @@ public boolean values(JObject target, JNode... items) {
68
68
}
69
69
70
70
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 ())));
75
75
}
76
76
77
77
public boolean regex (JString target , JString pattern ) {
78
78
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 ())));
84
83
return true ;
85
84
}
86
85
87
86
public boolean email (JString target ) {
88
87
// Based on SMTP protocol RFC 5322
89
88
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" ),
92
91
new ExpectedDetail (caller , "a valid email address" ),
93
- new ActualDetail (target , "found " + target + " that is invalid" )));
92
+ new ActualDetail (target , "found malformed target " + target )));
94
93
return true ;
95
94
}
96
95
97
96
public boolean url (JString target ) {
98
- boolean result ;
99
97
URI uri ;
100
98
try {
101
99
uri = new URI (target .getValue ());
102
- result = switch (uri .getScheme ()) {
103
- case URI_SCHEME_HTTP , URI_SCHEME_HTTPS -> true ;
104
- default -> false ;
105
- };
106
100
} 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 )));
111
105
}
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 )));
117
115
return true ;
118
116
}
119
117
120
118
public boolean url (JString target , JString scheme ) {
121
- boolean result ;
122
119
URI uri ;
123
120
try {
124
121
uri = new URI (target .getValue ());
125
122
} 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 )));
130
127
}
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 )));
137
134
return true ;
138
135
}
139
136
140
137
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 )
142
139
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" ),
145
142
new ExpectedDetail (caller , "a valid phone number" ),
146
- new ActualDetail (target , "found " + target + " that is invalid" )));
143
+ new ActualDetail (target , "found malformed target " + target )));
147
144
return true ;
148
145
}
149
146
}
0 commit comments