Skip to content

Commit a86d031

Browse files
committed
Update error codes and messages
1 parent cb9026b commit a86d031

File tree

8 files changed

+63
-22
lines changed

8 files changed

+63
-22
lines changed

src/main/java/com/relogiclabs/json/schema/internal/message/ActualHelper.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@ public static ActualDetail asValueMismatch(JNode node) {
2222
return new ActualDetail(node, "found ", node.getOutline());
2323
}
2424

25-
public static ActualDetail asInvalidDataType(JNode node) {
26-
return new ActualDetail(node, "applied on non-composite type ",
25+
public static ActualDetail asInvalidNestedDataType(JNode node) {
26+
return new ActualDetail(node, "found non-composite type ",
2727
getTypeName(node));
2828
}
2929

30+
public static ActualDetail asDataTypeArgumentFailed(JNode node) {
31+
return new ActualDetail(node, "found invalid value ", node.getOutline());
32+
}
33+
3034
public static ActualDetail asDataTypeMismatch(JNode node) {
3135
return new ActualDetail(node, "found ", getTypeName(node),
3236
" inferred by ", node.getOutline());
@@ -42,8 +46,9 @@ public static ActualDetail asUndefinedProperty(JProperty property) {
4246

4347
public static ActualDetail asPropertyOrderMismatch(JNode node) {
4448
return node instanceof JProperty property
45-
? new ActualDetail(property, "key ", quote(property.getKey()), " is found at position")
46-
: new ActualDetail(node, "key not found at position");
49+
? new ActualDetail(property, "key ", quote(property.getKey()),
50+
" is found at current position")
51+
: new ActualDetail(node, "key not found at current position");
4752
}
4853

4954
public static ActualDetail asInvalidFunction(JNode node) {

src/main/java/com/relogiclabs/json/schema/internal/message/ExpectedHelper.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ private ExpectedHelper() {
1818

1919
public static ExpectedDetail asArrayElementNotFound(JNode node, int index) {
2020
return new ExpectedDetail(node, "element at ", index,
21-
" (", node.getOutline(), ")");
21+
" '", node.getOutline(), "'");
2222
}
2323

2424
public static ExpectedDetail asValueMismatch(JNode node) {
@@ -29,8 +29,12 @@ public static ExpectedDetail asDataTypeMismatch(JDataType dataType) {
2929
return new ExpectedDetail(dataType, "data type ", dataType.toString(true));
3030
}
3131

32-
public static ExpectedDetail asInvalidDataType(JDataType dataType) {
33-
return new ExpectedDetail(dataType, "applying on composite type");
32+
public static ExpectedDetail asInvalidNestedDataType(JDataType dataType) {
33+
return new ExpectedDetail(dataType, "composite data type");
34+
}
35+
36+
public static ExpectedDetail asDataTypeArgumentFailed(JDataType dataType) {
37+
return new ExpectedDetail(dataType, "a valid value for ", quote(dataType.getAlias()));
3438
}
3539

3640
public static ExpectedDetail asDataTypeMismatch(JNode node) {
@@ -48,7 +52,7 @@ public static ExpectedDetail asUndefinedProperty(JObject object, JProperty prope
4852

4953
public static ExpectedDetail asPropertyOrderMismatch(JProperty property) {
5054
return new ExpectedDetail(property, "property with key ",
51-
quote(property.getKey()), " at position");
55+
quote(property.getKey()), " at current position");
5256
}
5357

5458
public static ExpectedDetail asInvalidFunction(JFunction function) {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.relogiclabs.json.schema.internal.message;
2+
3+
import static com.relogiclabs.json.schema.message.ErrorCode.DEFI03;
4+
import static com.relogiclabs.json.schema.message.ErrorCode.DEFI04;
5+
import static com.relogiclabs.json.schema.message.ErrorCode.DTYP04;
6+
import static com.relogiclabs.json.schema.message.ErrorCode.DTYP05;
7+
import static com.relogiclabs.json.schema.message.ErrorCode.DTYP06;
8+
import static com.relogiclabs.json.schema.message.ErrorCode.DTYP07;
9+
10+
public enum MatchReport {
11+
Success,
12+
TypeError(DTYP04, DTYP06),
13+
ArgumentError(DTYP05, DTYP07),
14+
AliasError(DEFI03, DEFI04);
15+
16+
private final String code1;
17+
private final String code2;
18+
19+
MatchReport(String code1, String code2) {
20+
this.code1 = code1;
21+
this.code2 = code2;
22+
}
23+
24+
MatchReport() {
25+
this(null, null);
26+
}
27+
28+
public String getCode(boolean nested) {
29+
return nested ? code2 : code1;
30+
}
31+
}

src/main/java/com/relogiclabs/json/schema/internal/message/MessageHelper.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55

66
public class MessageHelper {
77

8-
public static final String InvalidNestedDataType = "Invalid nested data type operation";
8+
public static final String ValidationFailed = "Validation failed";
9+
public static final String ValueMismatch = "Value mismatch";
10+
public static final String DataTypeMismatch = "Data type mismatch";
11+
public static final String InvalidNestedDataType = "Invalid nested data type";
12+
public static final String DataTypeArgumentFailed = "Data type argument failed";
913
public static final String InvalidNestedFunction = "Invalid nested function operation";
1014
public static final String PropertyNotFound = "Mandatory property not found";
1115
public static final String ArrayElementNotFound = "Mandatory array element not found";
1216
public static final String UndefinedPropertyFound = "Undefined property found";
1317
public static final String PropertyKeyMismatch = "Property key mismatch";
1418
public static final String PropertyValueMismatch = "Property value mismatch";
15-
public static final String DataTypeMismatch = "Data type mismatch";
16-
public static final String ValidationFailed = "Validation Failed";
17-
public static final String ValueMismatch = "Value mismatch";
1819
public static final String PropertyOrderMismatch = "Property order mismatch";
1920

2021
private MessageHelper() {

src/main/java/com/relogiclabs/json/schema/internal/util/StringHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public static String toEncoded(String target)
4747
return builder.toString();
4848
}
4949

50-
public static String quote(String source) {
51-
return '"' + source + '"';
50+
public static String quote(Object source) {
51+
return '"' + source.toString() + '"';
5252
}
5353

5454
public static String unquote(String target) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ public interface ErrorCode {
1616
String CLAS06 = "CLAS06";
1717
String CLAS07 = "CLAS07";
1818
String DCNF01 = "DCNF01";
19-
String DCNF02 = "DCNF02";
2019
String DDAY01 = "DDAY01";
2120
String DDAY02 = "DDAY02";
2221
String DDAY03 = "DDAY03";
2322
String DDAY04 = "DDAY04";
2423
String DEFI01 = "DEFI01";
2524
String DEFI02 = "DEFI02";
2625
String DEFI03 = "DEFI03";
26+
String DEFI04 = "DEFI04";
2727
String DERA01 = "DERA01";
2828
String DERA02 = "DERA02";
2929
String DFRC01 = "DFRC01";
@@ -63,6 +63,7 @@ public interface ErrorCode {
6363
String DTYP04 = "DTYP04";
6464
String DTYP05 = "DTYP05";
6565
String DTYP06 = "DTYP06";
66+
String DTYP07 = "DTYP07";
6667
String DUBL01 = "DUBL01";
6768
String DUTC01 = "DUTC01";
6869
String DUTC02 = "DUTC02";
@@ -119,7 +120,6 @@ public interface ErrorCode {
119120
String PROP04 = "PROP04";
120121
String PROP05 = "PROP05";
121122
String PROP06 = "PROP06";
122-
String PROP07 = "PROP07";
123123
String RANG01 = "RANG01";
124124
String RANG02 = "RANG02";
125125
String RANG03 = "RANG03";

src/main/java/com/relogiclabs/json/schema/types/JNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import static com.relogiclabs.json.schema.internal.message.MessageHelper.DataTypeMismatch;
1818
import static com.relogiclabs.json.schema.internal.util.StringHelper.createOutline;
19-
import static com.relogiclabs.json.schema.message.ErrorCode.DTYP03;
19+
import static com.relogiclabs.json.schema.message.ErrorCode.DTYP02;
2020
import static java.util.Objects.requireNonNull;
2121

2222
public abstract class JNode {
@@ -57,7 +57,7 @@ protected MessageFormatter getMessageFormatter() {
5757
protected <T> T castType(JNode node, Class<T> type) {
5858
if(type.isInstance(node)) return type.cast(node);
5959
failWith(new JsonSchemaException(
60-
new ErrorDetail(DTYP03, DataTypeMismatch),
60+
new ErrorDetail(DTYP02, DataTypeMismatch),
6161
ExpectedHelper.asDataTypeMismatch(this),
6262
ActualHelper.asDataTypeMismatch(node)));
6363
return null;

src/main/java/com/relogiclabs/json/schema/types/JObject.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
import static com.relogiclabs.json.schema.internal.message.MessageHelper.UndefinedPropertyFound;
1919
import static com.relogiclabs.json.schema.internal.util.MiscellaneousHelper.nonNull;
2020
import static com.relogiclabs.json.schema.internal.util.StringHelper.join;
21+
import static com.relogiclabs.json.schema.message.ErrorCode.PROP04;
2122
import static com.relogiclabs.json.schema.message.ErrorCode.PROP05;
2223
import static com.relogiclabs.json.schema.message.ErrorCode.PROP06;
23-
import static com.relogiclabs.json.schema.message.ErrorCode.PROP07;
2424
import static java.util.Objects.requireNonNull;
2525

2626
@Getter
@@ -66,15 +66,15 @@ public boolean match(JNode node) {
6666
}
6767
if(!((JValidator) thisProp.getValue()).getOptional())
6868
return failWith(new JsonSchemaException(
69-
new ErrorDetail(PROP05, PropertyNotFound),
69+
new ErrorDetail(PROP04, PropertyNotFound),
7070
ExpectedHelper.asPropertyNotFound(thisProp),
7171
ActualHelper.asPropertyNotFound(node, thisProp)));
7272
}
7373
if(unresolved.size() > 0 && !getRuntime().getIgnoreUndefinedProperties()) {
7474
for(String key : unresolved) {
7575
var property = other.properties.get(key);
7676
result &= failWith(new JsonSchemaException(
77-
new ErrorDetail(PROP06, UndefinedPropertyFound),
77+
new ErrorDetail(PROP05, UndefinedPropertyFound),
7878
ExpectedHelper.asUndefinedProperty(this, property),
7979
ActualHelper.asUndefinedProperty(property)));
8080
}
@@ -92,7 +92,7 @@ private JProperty getOtherProp(JObject other, int index) {
9292
if(otherProp == null) existing = other.properties.get(thisProp.getKey());
9393
if(otherProp == null && existing != null)
9494
failWith(new JsonSchemaException(
95-
new ErrorDetail(PROP07, PropertyOrderMismatch),
95+
new ErrorDetail(PROP06, PropertyOrderMismatch),
9696
ExpectedHelper.asPropertyOrderMismatch(thisProp),
9797
ActualHelper.asPropertyOrderMismatch(nonNull(atProp, other))));
9898
} else otherProp = other.properties.get(thisProp.getKey());

0 commit comments

Comments
 (0)