Skip to content

Commit d833653

Browse files
committed
Add new validation functions
1 parent 076a538 commit d833653

File tree

4 files changed

+80
-13
lines changed

4 files changed

+80
-13
lines changed

src/main/java/com/relogiclabs/json/schema/JsonAssert.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.relogiclabs.json.schema;
22

3+
import com.relogiclabs.json.schema.internal.util.DebugUtils;
34
import com.relogiclabs.json.schema.message.MessageFormatter;
45
import com.relogiclabs.json.schema.tree.JsonTree;
56
import com.relogiclabs.json.schema.tree.RuntimeContext;
67
import com.relogiclabs.json.schema.tree.SchemaTree;
7-
import com.relogiclabs.json.schema.util.DebugUtils;
88
import lombok.Getter;
99

1010
/**
@@ -26,6 +26,11 @@ public JsonAssert(String schema) {
2626
schemaTree = new SchemaTree(runtime, schema);
2727
}
2828

29+
/**
30+
* Tests whether the input JSON string conforms to the Schema specified
31+
* in the {@link JsonAssert} constructor.
32+
* @param jsonActual The actual JSON to conform or validate
33+
*/
2934
public void isValid(String jsonActual) {
3035
runtime.getExceptions().clear();
3136
var jsonTree = new JsonTree(runtime, jsonActual);

src/main/java/com/relogiclabs/json/schema/function/CoreFunctions2.java

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,24 @@
66
import com.relogiclabs.json.schema.message.ExpectedDetail;
77
import com.relogiclabs.json.schema.tree.RuntimeContext;
88
import com.relogiclabs.json.schema.types.JArray;
9+
import com.relogiclabs.json.schema.types.JBoolean;
910
import com.relogiclabs.json.schema.types.JNumber;
1011
import com.relogiclabs.json.schema.types.JObject;
1112
import com.relogiclabs.json.schema.types.JString;
1213
import com.relogiclabs.json.schema.types.JUndefined;
1314

1415
import java.util.Arrays;
16+
import java.util.function.Supplier;
1517

16-
import static com.relogiclabs.json.schema.internal.util.StringHelper.toUnitedString;
18+
import static com.relogiclabs.json.schema.internal.util.StringHelper.join;
1719
import static com.relogiclabs.json.schema.message.ErrorCode.ENUM01;
1820
import static com.relogiclabs.json.schema.message.ErrorCode.ENUM02;
21+
import static com.relogiclabs.json.schema.message.ErrorCode.MAXI01;
22+
import static com.relogiclabs.json.schema.message.ErrorCode.MAXI02;
23+
import static com.relogiclabs.json.schema.message.ErrorCode.MAXI03;
24+
import static com.relogiclabs.json.schema.message.ErrorCode.MINI01;
25+
import static com.relogiclabs.json.schema.message.ErrorCode.MINI02;
26+
import static com.relogiclabs.json.schema.message.ErrorCode.MINI03;
1927
import static com.relogiclabs.json.schema.message.ErrorCode.NEGI01;
2028
import static com.relogiclabs.json.schema.message.ErrorCode.NEMT01;
2129
import static com.relogiclabs.json.schema.message.ErrorCode.NEMT02;
@@ -31,11 +39,12 @@ public CoreFunctions2(RuntimeContext runtime) {
3139
super(runtime);
3240
}
3341

42+
// enum is a keyword in Java but _ will be escaped
3443
public boolean _enum(JString target, JString... items) {
3544
var list = Arrays.asList(items);
3645
if(!list.contains(target)) return failWith(new JsonSchemaException(
3746
new ErrorDetail(ENUM01, "String is not in enum list"),
38-
new ExpectedDetail(function, "string in list ", toUnitedString(list, ", ", "[", "]")),
47+
new ExpectedDetail(function, "string in list ", join(list, ", ", "[", "]")),
3948
new ActualDetail(target, "string ", target, " is not found in list")));
4049
return true;
4150
}
@@ -44,11 +53,65 @@ public boolean _enum(JNumber target, JNumber... items) {
4453
var list = Arrays.asList(items);
4554
if(!list.contains(target)) return failWith(new JsonSchemaException(
4655
new ErrorDetail(ENUM02, "Number is not in enum list"),
47-
new ExpectedDetail(function, "number in list ", toUnitedString(list, ", ", "[", "]")),
56+
new ExpectedDetail(function, "number in list ", join(list, ", ", "[", "]")),
4857
new ActualDetail(target, "number ", target, " is not found in list")));
4958
return true;
5059
}
5160

61+
public boolean minimum(JNumber target, JNumber minimum) {
62+
if(target.compare(minimum) < 0)
63+
return failWith(new JsonSchemaException(
64+
new ErrorDetail(MINI01, "Number is less than provided minimum"),
65+
new ExpectedDetail(function, "a number greater than or equal to ", minimum),
66+
new ActualDetail(target, "number ", target, " is less than ", minimum)));
67+
return true;
68+
}
69+
70+
public boolean minimum(JNumber target, JNumber minimum, JBoolean exclusive) {
71+
Supplier<String> relationTo = () -> exclusive.getValue()
72+
? "greater than"
73+
: "greater than or equal to";
74+
75+
if(target.compare(minimum) < 0)
76+
return failWith(new JsonSchemaException(
77+
new ErrorDetail(MINI02, "Number is less than provided minimum"),
78+
new ExpectedDetail(function, "a number ", relationTo.get(), " ", minimum),
79+
new ActualDetail(target, "number ", target, " is less than ", minimum)));
80+
if(exclusive.getValue() && target.compare(minimum) == 0)
81+
return failWith(new JsonSchemaException(
82+
new ErrorDetail(MINI03, "Number is equal to provided minimum"),
83+
new ExpectedDetail(function, "a number ", relationTo.get(), " ", minimum),
84+
new ActualDetail(target, "number ", target, " is equal to ", minimum)));
85+
return true;
86+
}
87+
88+
public boolean maximum(JNumber target, JNumber maximum) {
89+
if(target.compare(maximum) > 0)
90+
return failWith(new JsonSchemaException(
91+
new ErrorDetail(MAXI01, "Number is greater than provided maximum"),
92+
new ExpectedDetail(function, "a number less than or equal ", maximum),
93+
new ActualDetail(target, "number ", target, " is greater than ", maximum)));
94+
return true;
95+
}
96+
97+
public boolean maximum(JNumber target, JNumber maximum, JBoolean exclusive) {
98+
Supplier<String> relationTo = () -> exclusive.getValue()
99+
? "less than"
100+
: "less than or equal to";
101+
102+
if(target.compare(maximum) > 0)
103+
return failWith(new JsonSchemaException(
104+
new ErrorDetail(MAXI02, "Number is greater than provided maximum"),
105+
new ExpectedDetail(function, "a number ", relationTo.get(), " ", maximum),
106+
new ActualDetail(target, "number ", target, " is greater than ", maximum)));
107+
if(exclusive.getValue() && target.compare(maximum) == 0)
108+
return failWith(new JsonSchemaException(
109+
new ErrorDetail(MAXI03, "Number is equal to provided maximum"),
110+
new ExpectedDetail(function, "a number ", relationTo.get(), " ", maximum),
111+
new ActualDetail(target, "number ", target, " is equal to ", maximum)));
112+
return true;
113+
}
114+
52115
public boolean positive(JNumber target) {
53116
if(target.compare(0) <= 0) return failWith(new JsonSchemaException(
54117
new ErrorDetail(POSI01, "Number is not positive"),

src/main/java/com/relogiclabs/json/schema/message/ErrorCode.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ public interface ErrorCode {
9393
String JLEX01 = "JLEX01";
9494
String JPRS01 = "JPRS01";
9595
String KEYS01 = "KEYS01";
96+
String MAXI01 = "MAXI01";
97+
String MAXI02 = "MAXI02";
98+
String MAXI03 = "MAXI03";
99+
String MINI01 = "MINI01";
100+
String MINI02 = "MINI02";
101+
String MINI03 = "MINI03";
96102
String NEGI01 = "NEGI01";
97103
String NEMT01 = "NEMT01";
98104
String NEMT02 = "NEMT02";
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
package com.relogiclabs.json.schema.tree;
22

3-
import lombok.AllArgsConstructor;
4-
import lombok.Getter;
53
import org.antlr.v4.runtime.Token;
64

7-
@Getter
8-
@AllArgsConstructor
9-
public class Location {
10-
private int line;
11-
private int column;
12-
5+
public record Location(int line, int column) {
136
public static Location from(Token token) {
147
return new Location(token.getLine(), token.getCharPositionInLine());
158
}
@@ -18,4 +11,4 @@ public static Location from(Token token) {
1811
public String toString() {
1912
return line + ":" + column;
2013
}
21-
}
14+
}

0 commit comments

Comments
 (0)