Skip to content

Commit 654f11d

Browse files
committed
Refactor core functions for schema
1 parent d6d4710 commit 654f11d

File tree

8 files changed

+762
-20
lines changed

8 files changed

+762
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package com.relogiclabs.jschema.function;
2+
3+
import com.relogiclabs.jschema.exception.JsonSchemaException;
4+
import com.relogiclabs.jschema.message.ActualDetail;
5+
import com.relogiclabs.jschema.message.ErrorDetail;
6+
import com.relogiclabs.jschema.message.ExpectedDetail;
7+
import com.relogiclabs.jschema.node.JArray;
8+
import com.relogiclabs.jschema.node.JInteger;
9+
import com.relogiclabs.jschema.node.JObject;
10+
import com.relogiclabs.jschema.node.JString;
11+
import com.relogiclabs.jschema.node.JUndefined;
12+
import com.relogiclabs.jschema.tree.RuntimeContext;
13+
14+
import static com.relogiclabs.jschema.message.ErrorCode.ALEN01;
15+
import static com.relogiclabs.jschema.message.ErrorCode.ALEN02;
16+
import static com.relogiclabs.jschema.message.ErrorCode.ALEN03;
17+
import static com.relogiclabs.jschema.message.ErrorCode.ALEN04;
18+
import static com.relogiclabs.jschema.message.ErrorCode.ALEN05;
19+
import static com.relogiclabs.jschema.message.ErrorCode.OLEN01;
20+
import static com.relogiclabs.jschema.message.ErrorCode.OLEN02;
21+
import static com.relogiclabs.jschema.message.ErrorCode.OLEN03;
22+
import static com.relogiclabs.jschema.message.ErrorCode.OLEN04;
23+
import static com.relogiclabs.jschema.message.ErrorCode.OLEN05;
24+
import static com.relogiclabs.jschema.message.ErrorCode.SLEN01;
25+
import static com.relogiclabs.jschema.message.ErrorCode.SLEN02;
26+
import static com.relogiclabs.jschema.message.ErrorCode.SLEN03;
27+
import static com.relogiclabs.jschema.message.ErrorCode.SLEN04;
28+
import static com.relogiclabs.jschema.message.ErrorCode.SLEN05;
29+
30+
public abstract class CoreFunctions1 extends FunctionProvider {
31+
public CoreFunctions1(RuntimeContext runtime) {
32+
super(runtime);
33+
}
34+
35+
public boolean length(JString target, JInteger length) {
36+
var rLength = target.length();
37+
if(rLength != length.getValue()) return fail(new JsonSchemaException(
38+
new ErrorDetail(SLEN01, "Invalid length of string ", target),
39+
new ExpectedDetail(function, "a string of length ", length),
40+
new ActualDetail(target, "found ", rLength, " which does not match")));
41+
return true;
42+
}
43+
44+
public boolean length(JArray target, JInteger length) {
45+
var rLength = target.getElements().size();
46+
if(rLength != length.getValue()) return fail(new JsonSchemaException(
47+
new ErrorDetail(ALEN01, "Invalid length of array ", target.getOutline()),
48+
new ExpectedDetail(function, "an array of length ", length),
49+
new ActualDetail(target, "found ", rLength, " which does not match")));
50+
return true;
51+
}
52+
53+
public boolean length(JObject target, JInteger length) {
54+
var rLength = target.getProperties().size();
55+
if(rLength != length.getValue()) return fail(new JsonSchemaException(
56+
new ErrorDetail(OLEN01, "Invalid size or length of object ", target.getOutline()),
57+
new ExpectedDetail(function, "an object of length ", length),
58+
new ActualDetail(target, "found ", rLength, " which does not match")));
59+
return true;
60+
}
61+
62+
public boolean length(JString target, JInteger minimum, JInteger maximum) {
63+
var length = target.length();
64+
if(length < minimum.getValue())
65+
return fail(new JsonSchemaException(new ErrorDetail(SLEN02,
66+
"String ", target.getOutline(), " length is outside of range"),
67+
new ExpectedDetail(function, "length in range [", minimum, ", ", maximum, "]"),
68+
new ActualDetail(target, "found ", length, " that is less than ", minimum)));
69+
if(length > maximum.getValue())
70+
return fail(new JsonSchemaException(new ErrorDetail(SLEN03,
71+
"String ", target.getOutline(), " length is outside of range"),
72+
new ExpectedDetail(function, "length in range [", minimum, ", ", maximum, "]"),
73+
new ActualDetail(target, "found ", length, " that is greater than ", maximum)));
74+
return true;
75+
}
76+
77+
public boolean length(JString target, JInteger minimum, JUndefined undefined) {
78+
var length = target.length();
79+
if(length < minimum.getValue())
80+
return fail(new JsonSchemaException(new ErrorDetail(SLEN04,
81+
"String ", target.getOutline(), " length is outside of range"),
82+
new ExpectedDetail(function, "length in range [", minimum, ", ", undefined, "]"),
83+
new ActualDetail(target, "found ", length, " that is less than ", minimum)));
84+
return true;
85+
}
86+
87+
public boolean length(JString target, JUndefined undefined, JInteger maximum) {
88+
var length = target.length();
89+
if(length > maximum.getValue())
90+
return fail(new JsonSchemaException(new ErrorDetail(SLEN05,
91+
"String ", target.getOutline(), " length is outside of range"),
92+
new ExpectedDetail(function, "length in range [", undefined, ", ", maximum, "]"),
93+
new ActualDetail(target, "found ", length, " that is greater than ", maximum)));
94+
return true;
95+
}
96+
97+
public boolean length(JArray target, JInteger minimum, JInteger maximum) {
98+
var length = target.getElements().size();
99+
if(length < minimum.getValue())
100+
return fail(new JsonSchemaException(new ErrorDetail(ALEN02,
101+
"Array ", target.getOutline(), " length is outside of range"),
102+
new ExpectedDetail(function, "length in range [", minimum, ", ", maximum, "]"),
103+
new ActualDetail(target, "found ", length, " that is less than ", minimum)));
104+
if(length > maximum.getValue())
105+
return fail(new JsonSchemaException(new ErrorDetail(ALEN03,
106+
"Array ", target.getOutline(), " length is outside of range"),
107+
new ExpectedDetail(function, "length in range [", minimum, ", ", maximum, "]"),
108+
new ActualDetail(target, "found ", length, " that is greater than ", maximum)));
109+
return true;
110+
}
111+
112+
public boolean length(JArray target, JInteger minimum, JUndefined undefined) {
113+
var length = target.getElements().size();
114+
if(length < minimum.getValue())
115+
return fail(new JsonSchemaException(new ErrorDetail(ALEN04,
116+
"Array ", target.getOutline(), " length is outside of range"),
117+
new ExpectedDetail(function, "length in range [", minimum, ", ", undefined, "]"),
118+
new ActualDetail(target, "found ", length, " that is less than ", minimum)));
119+
return true;
120+
}
121+
122+
public boolean length(JArray target, JUndefined undefined, JInteger maximum) {
123+
var length = target.getElements().size();
124+
if(length > maximum.getValue())
125+
return fail(new JsonSchemaException(new ErrorDetail(ALEN05,
126+
"Array ", target.getOutline(), " length is outside of range"),
127+
new ExpectedDetail(function, "length in range [", undefined, ", ", maximum, "]"),
128+
new ActualDetail(target, "found ", length, " that is greater than ", maximum)));
129+
return true;
130+
}
131+
132+
public boolean length(JObject target, JInteger minimum, JInteger maximum) {
133+
var length = target.getProperties().size();
134+
if(length < minimum.getValue())
135+
return fail(new JsonSchemaException(new ErrorDetail(OLEN02,
136+
"Object ", target.getOutline(), " size or length is outside of range"),
137+
new ExpectedDetail(function, "length in range [", minimum, ", ", maximum, "]"),
138+
new ActualDetail(target, "found ", length, " that is less than ", minimum)));
139+
if(length > maximum.getValue())
140+
return fail(new JsonSchemaException(new ErrorDetail(OLEN03,
141+
"Object ", target.getOutline(), " size or length is outside of range"),
142+
new ExpectedDetail(function, "length in range [", minimum, ", ", maximum, "]"),
143+
new ActualDetail(target, "found ", length, " that is greater than ", maximum)));
144+
return true;
145+
}
146+
147+
public boolean length(JObject target, JInteger minimum, JUndefined undefined) {
148+
var length = target.getProperties().size();
149+
if(length < minimum.getValue())
150+
return fail(new JsonSchemaException(new ErrorDetail(OLEN04,
151+
"Object ", target.getOutline(), " size or length is outside of range"),
152+
new ExpectedDetail(function, "length in range [", minimum, ", ", undefined, "]"),
153+
new ActualDetail(target, "found ", length, " that is less than ", minimum)));
154+
return true;
155+
}
156+
157+
public boolean length(JObject target, JUndefined undefined, JInteger maximum) {
158+
var length = target.getProperties().size();
159+
if(length > maximum.getValue())
160+
return fail(new JsonSchemaException(new ErrorDetail(OLEN05,
161+
"Object ", target.getOutline(), " size or length is outside of range"),
162+
new ExpectedDetail(function, "length in range [", undefined, ", ", maximum, "]"),
163+
new ActualDetail(target, "found ", length, " that is greater than ", maximum)));
164+
return true;
165+
}
166+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
package com.relogiclabs.jschema.function;
2+
3+
import com.relogiclabs.jschema.exception.JsonSchemaException;
4+
import com.relogiclabs.jschema.message.ActualDetail;
5+
import com.relogiclabs.jschema.message.ErrorDetail;
6+
import com.relogiclabs.jschema.message.ExpectedDetail;
7+
import com.relogiclabs.jschema.node.JArray;
8+
import com.relogiclabs.jschema.node.JBoolean;
9+
import com.relogiclabs.jschema.node.JNumber;
10+
import com.relogiclabs.jschema.node.JObject;
11+
import com.relogiclabs.jschema.node.JString;
12+
import com.relogiclabs.jschema.node.JUndefined;
13+
import com.relogiclabs.jschema.tree.RuntimeContext;
14+
15+
import java.util.Arrays;
16+
import java.util.function.Supplier;
17+
18+
import static com.relogiclabs.jschema.internal.util.StringHelper.joinWith;
19+
import static com.relogiclabs.jschema.message.ErrorCode.ENUM01;
20+
import static com.relogiclabs.jschema.message.ErrorCode.ENUM02;
21+
import static com.relogiclabs.jschema.message.ErrorCode.MAXI01;
22+
import static com.relogiclabs.jschema.message.ErrorCode.MAXI02;
23+
import static com.relogiclabs.jschema.message.ErrorCode.MAXI03;
24+
import static com.relogiclabs.jschema.message.ErrorCode.MINI01;
25+
import static com.relogiclabs.jschema.message.ErrorCode.MINI02;
26+
import static com.relogiclabs.jschema.message.ErrorCode.MINI03;
27+
import static com.relogiclabs.jschema.message.ErrorCode.NEGI01;
28+
import static com.relogiclabs.jschema.message.ErrorCode.NEGI02;
29+
import static com.relogiclabs.jschema.message.ErrorCode.NEMT01;
30+
import static com.relogiclabs.jschema.message.ErrorCode.NEMT02;
31+
import static com.relogiclabs.jschema.message.ErrorCode.NEMT03;
32+
import static com.relogiclabs.jschema.message.ErrorCode.POSI01;
33+
import static com.relogiclabs.jschema.message.ErrorCode.POSI02;
34+
import static com.relogiclabs.jschema.message.ErrorCode.RANG01;
35+
import static com.relogiclabs.jschema.message.ErrorCode.RANG02;
36+
import static com.relogiclabs.jschema.message.ErrorCode.RANG03;
37+
import static com.relogiclabs.jschema.message.ErrorCode.RANG04;
38+
39+
public abstract class CoreFunctions2 extends CoreFunctions1 {
40+
public CoreFunctions2(RuntimeContext runtime) {
41+
super(runtime);
42+
}
43+
44+
// enum is a keyword in Java but _ will be escaped
45+
public boolean _enum(JString target, JString... items) {
46+
var list = Arrays.asList(items);
47+
if(!list.contains(target)) return fail(new JsonSchemaException(
48+
new ErrorDetail(ENUM01, "String is not in enum list"),
49+
new ExpectedDetail(function, "string in list ", joinWith(list, ", ", "[", "]")),
50+
new ActualDetail(target, "string ", target, " is not found in list")));
51+
return true;
52+
}
53+
54+
public boolean _enum(JNumber target, JNumber... items) {
55+
var list = Arrays.asList(items);
56+
if(!list.contains(target)) return fail(new JsonSchemaException(
57+
new ErrorDetail(ENUM02, "Number is not in enum list"),
58+
new ExpectedDetail(function, "number in list ", joinWith(list, ", ", "[", "]")),
59+
new ActualDetail(target, "number ", target, " is not found in list")));
60+
return true;
61+
}
62+
63+
public boolean minimum(JNumber target, JNumber minimum) {
64+
if(target.compare(minimum) < 0)
65+
return fail(new JsonSchemaException(
66+
new ErrorDetail(MINI01, "Number is less than provided minimum"),
67+
new ExpectedDetail(function, "a number greater than or equal to ", minimum),
68+
new ActualDetail(target, "number ", target, " is less than ", minimum)));
69+
return true;
70+
}
71+
72+
public boolean minimum(JNumber target, JNumber minimum, JBoolean exclusive) {
73+
Supplier<String> relationTo = () -> exclusive.getValue()
74+
? "greater than"
75+
: "greater than or equal to";
76+
77+
if(target.compare(minimum) < 0)
78+
return fail(new JsonSchemaException(
79+
new ErrorDetail(MINI02, "Number is less than provided minimum"),
80+
new ExpectedDetail(function, "a number ", relationTo.get(), " ", minimum),
81+
new ActualDetail(target, "number ", target, " is less than ", minimum)));
82+
if(exclusive.getValue() && target.compare(minimum) == 0)
83+
return fail(new JsonSchemaException(
84+
new ErrorDetail(MINI03, "Number is equal to provided minimum"),
85+
new ExpectedDetail(function, "a number ", relationTo.get(), " ", minimum),
86+
new ActualDetail(target, "number ", target, " is equal to ", minimum)));
87+
return true;
88+
}
89+
90+
public boolean maximum(JNumber target, JNumber maximum) {
91+
if(target.compare(maximum) > 0)
92+
return fail(new JsonSchemaException(
93+
new ErrorDetail(MAXI01, "Number is greater than provided maximum"),
94+
new ExpectedDetail(function, "a number less than or equal ", maximum),
95+
new ActualDetail(target, "number ", target, " is greater than ", maximum)));
96+
return true;
97+
}
98+
99+
public boolean maximum(JNumber target, JNumber maximum, JBoolean exclusive) {
100+
Supplier<String> relationTo = () -> exclusive.getValue()
101+
? "less than"
102+
: "less than or equal to";
103+
104+
if(target.compare(maximum) > 0)
105+
return fail(new JsonSchemaException(
106+
new ErrorDetail(MAXI02, "Number is greater than provided maximum"),
107+
new ExpectedDetail(function, "a number ", relationTo.get(), " ", maximum),
108+
new ActualDetail(target, "number ", target, " is greater than ", maximum)));
109+
if(exclusive.getValue() && target.compare(maximum) == 0)
110+
return fail(new JsonSchemaException(
111+
new ErrorDetail(MAXI03, "Number is equal to provided maximum"),
112+
new ExpectedDetail(function, "a number ", relationTo.get(), " ", maximum),
113+
new ActualDetail(target, "number ", target, " is equal to ", maximum)));
114+
return true;
115+
}
116+
117+
public boolean positive(JNumber target) {
118+
if(target.compare(0) <= 0) return fail(new JsonSchemaException(
119+
new ErrorDetail(POSI01, "Number is not positive"),
120+
new ExpectedDetail(function, "a positive number"),
121+
new ActualDetail(target, "number ", target, " is less than or equal to zero")));
122+
return true;
123+
}
124+
125+
public boolean negative(JNumber target) {
126+
if(target.compare(0) >= 0) return fail(new JsonSchemaException(
127+
new ErrorDetail(NEGI01, "Number is not negative"),
128+
new ExpectedDetail(function, "a negative number"),
129+
new ActualDetail(target, "number ", target, " is greater than or equal to zero")));
130+
return true;
131+
}
132+
133+
public boolean positive(JNumber target, JNumber reference) {
134+
if(target.compare(reference) < 0) return fail(new JsonSchemaException(
135+
new ErrorDetail(POSI02, "Number is not positive from reference"),
136+
new ExpectedDetail(function, "a positive number from ", reference),
137+
new ActualDetail(target, "number ", target, " is less than reference")));
138+
return true;
139+
}
140+
141+
public boolean negative(JNumber target, JNumber reference) {
142+
if(target.compare(reference) > 0) return fail(new JsonSchemaException(
143+
new ErrorDetail(NEGI02, "Number is not negative from reference"),
144+
new ExpectedDetail(function, "a negative number from ", reference),
145+
new ActualDetail(target, "number ", target, " is greater than reference")));
146+
return true;
147+
}
148+
149+
public boolean range(JNumber target, JNumber minimum, JNumber maximum) {
150+
if(target.compare(minimum) < 0) return fail(new JsonSchemaException(
151+
new ErrorDetail(RANG01, "Number is outside of range"),
152+
new ExpectedDetail(function, "number in range [", minimum, ", ", maximum, "]"),
153+
new ActualDetail(target, "number ", target, " is less than ", minimum)));
154+
if(target.compare(maximum) > 0) return fail(new JsonSchemaException(
155+
new ErrorDetail(RANG02, "Number is outside of range"),
156+
new ExpectedDetail(function, "number in range [", minimum, ", ", maximum, "]"),
157+
new ActualDetail(target, "number ", target, " is greater than ", maximum)));
158+
return true;
159+
}
160+
161+
public boolean range(JNumber target, JNumber minimum, JUndefined undefined) {
162+
if(target.compare(minimum) < 0) return fail(new JsonSchemaException(
163+
new ErrorDetail(RANG03, "Number is outside of range"),
164+
new ExpectedDetail(function, "number in range [", minimum, ", ", undefined, "]"),
165+
new ActualDetail(target, "number ", target, " is less than ", minimum)));
166+
return true;
167+
}
168+
169+
public boolean range(JNumber target, JUndefined undefined, JNumber maximum) {
170+
if(target.compare(maximum) > 0) return fail(new JsonSchemaException(
171+
new ErrorDetail(RANG04, "Number is outside of range"),
172+
new ExpectedDetail(function, "number in range [", undefined, ", ", maximum, "]"),
173+
new ActualDetail(target, "number ", target, " is greater than ", maximum)));
174+
return true;
175+
}
176+
177+
public boolean nonempty(JString target) {
178+
var length = target.length();
179+
if(length <= 0) return fail(new JsonSchemaException(
180+
new ErrorDetail(NEMT01, "String is empty"),
181+
new ExpectedDetail(function, "Non empty string"),
182+
new ActualDetail(target, "found empty string")));
183+
return true;
184+
}
185+
186+
public boolean nonempty(JArray target) {
187+
var length = target.getElements().size();
188+
if(length <= 0) return fail(new JsonSchemaException(
189+
new ErrorDetail(NEMT02, "Array is empty"),
190+
new ExpectedDetail(function, "Non empty array"),
191+
new ActualDetail(target, "found empty array")));
192+
return true;
193+
}
194+
195+
public boolean nonempty(JObject target) {
196+
var length = target.getProperties().size();
197+
if(length <= 0) return fail(new JsonSchemaException(
198+
new ErrorDetail(NEMT03, "Object is empty"),
199+
new ExpectedDetail(function, "Non empty object"),
200+
new ActualDetail(target, "found empty object")));
201+
return true;
202+
}
203+
}

0 commit comments

Comments
 (0)