|
4 | 4 | import com.relogiclabs.json.schema.exception.JsonSchemaException; |
5 | 5 | import com.relogiclabs.json.schema.internal.message.ActualHelper; |
6 | 6 | import com.relogiclabs.json.schema.internal.message.ExpectedHelper; |
| 7 | +import com.relogiclabs.json.schema.internal.message.MatchReport; |
7 | 8 | import com.relogiclabs.json.schema.message.ErrorDetail; |
8 | 9 | import com.relogiclabs.json.schema.message.MessageFormatter; |
9 | 10 | import lombok.EqualsAndHashCode; |
|
14 | 15 | import java.util.Collection; |
15 | 16 | import java.util.List; |
16 | 17 |
|
| 18 | +import static com.relogiclabs.json.schema.internal.message.MatchReport.AliasError; |
| 19 | +import static com.relogiclabs.json.schema.internal.message.MatchReport.ArgumentError; |
| 20 | +import static com.relogiclabs.json.schema.internal.message.MatchReport.Success; |
| 21 | +import static com.relogiclabs.json.schema.internal.message.MatchReport.TypeError; |
| 22 | +import static com.relogiclabs.json.schema.internal.message.MessageHelper.DataTypeArgumentFailed; |
17 | 23 | import static com.relogiclabs.json.schema.internal.message.MessageHelper.DataTypeMismatch; |
18 | 24 | import static com.relogiclabs.json.schema.internal.message.MessageHelper.InvalidNestedDataType; |
19 | 25 | import static com.relogiclabs.json.schema.internal.util.StreamHelper.allTrue; |
20 | | -import static com.relogiclabs.json.schema.message.ErrorCode.DEFI03; |
21 | | -import static com.relogiclabs.json.schema.message.ErrorCode.DTYP02; |
22 | | -import static com.relogiclabs.json.schema.message.ErrorCode.DTYP04; |
23 | | -import static com.relogiclabs.json.schema.message.ErrorCode.DTYP05; |
24 | | -import static com.relogiclabs.json.schema.message.ErrorCode.DTYP06; |
| 26 | +import static com.relogiclabs.json.schema.internal.util.StringHelper.quote; |
| 27 | +import static com.relogiclabs.json.schema.message.ErrorCode.DTYP03; |
25 | 28 | import static java.util.Collections.emptyList; |
26 | 29 | import static java.util.Objects.requireNonNull; |
27 | 30 |
|
@@ -57,47 +60,52 @@ protected <T extends JNode> T initialize() { |
57 | 60 |
|
58 | 61 | @Override |
59 | 62 | public boolean match(JNode node) { |
60 | | - if(!nested) return jsonType.match(node); |
61 | | - if(!(node instanceof JComposite composite)) |
62 | | - return failWith(new JsonSchemaException( |
63 | | - new ErrorDetail(DTYP02, InvalidNestedDataType), |
64 | | - ExpectedHelper.asInvalidDataType(this), |
65 | | - ActualHelper.asInvalidDataType(node))); |
66 | | - return composite.components().stream().map(this::matchCurrent).allMatch(allTrue()); |
| 63 | + if(!nested) return isMatchCurrent(node); |
| 64 | + if(!(node instanceof JComposite composite)) return false; |
| 65 | + return composite.components().stream().map(this::isMatchCurrent).allMatch(allTrue()); |
67 | 66 | } |
68 | 67 |
|
69 | | - private boolean matchCurrent(JNode node) { |
70 | | - boolean result = true; |
71 | | - result &= jsonType.match(node); |
72 | | - if(alias == null) return result; |
| 68 | + private boolean isMatchCurrent(JNode node) { |
| 69 | + return matchCurrent(node) == Success; |
| 70 | + } |
| 71 | + |
| 72 | + private MatchReport matchCurrent(JNode node) { |
| 73 | + var result = jsonType.match(node) ? Success : TypeError; |
| 74 | + if(alias == null || result != Success) return result; |
73 | 75 | var validator = getRuntime().getDefinitions().get(alias); |
74 | | - if(validator == null) return failWith(new DefinitionNotFoundException( |
75 | | - MessageFormatter.formatForSchema(DEFI03, alias.getName(), getContext()))); |
76 | | - result &= validator.match(node); |
| 76 | + if(validator == null) return AliasError; |
| 77 | + result = validator.match(node) ? Success : ArgumentError; |
77 | 78 | return result; |
78 | 79 | } |
79 | 80 |
|
80 | 81 | public boolean matchForReport(JNode node) { |
81 | | - if(!nested && !jsonType.match(node)) |
82 | | - return failWith(new JsonSchemaException( |
83 | | - new ErrorDetail(DTYP04, DataTypeMismatch), |
84 | | - ExpectedHelper.asDataTypeMismatch(this), |
85 | | - ActualHelper.asDataTypeMismatch(node))); |
| 82 | + if(!nested) return matchForReport(node, false); |
86 | 83 | if(!(node instanceof JComposite composite)) |
87 | 84 | return failWith(new JsonSchemaException( |
88 | | - new ErrorDetail(DTYP05, InvalidNestedDataType), |
89 | | - ExpectedHelper.asInvalidDataType(this), |
90 | | - ActualHelper.asInvalidDataType(node))); |
| 85 | + new ErrorDetail(DTYP03, InvalidNestedDataType), |
| 86 | + ExpectedHelper.asInvalidNestedDataType(this), |
| 87 | + ActualHelper.asInvalidNestedDataType(node))); |
91 | 88 | boolean result = true; |
92 | | - for(var c : composite.components()) { |
93 | | - if(!matchCurrent(c)) result &= failWith(new JsonSchemaException( |
94 | | - new ErrorDetail(DTYP06, DataTypeMismatch), |
95 | | - ExpectedHelper.asDataTypeMismatch(this), |
96 | | - ActualHelper.asDataTypeMismatch(c))); |
97 | | - } |
| 89 | + for(var c : composite.components()) result &= matchForReport(c, true); |
98 | 90 | return result; |
99 | 91 | } |
100 | 92 |
|
| 93 | + private boolean matchForReport(JNode node, boolean nested) { |
| 94 | + var result = matchCurrent(node); |
| 95 | + if(result == TypeError) return failWith(new JsonSchemaException( |
| 96 | + new ErrorDetail(TypeError.getCode(nested), DataTypeMismatch), |
| 97 | + ExpectedHelper.asDataTypeMismatch(this), |
| 98 | + ActualHelper.asDataTypeMismatch(node))); |
| 99 | + if(result == AliasError) return failWith(new DefinitionNotFoundException( |
| 100 | + MessageFormatter.formatForSchema(AliasError.getCode(nested), |
| 101 | + "No definition found for " + quote(alias), getContext()))); |
| 102 | + if(result == ArgumentError) return failWith(new JsonSchemaException( |
| 103 | + new ErrorDetail(ArgumentError.getCode(nested), DataTypeArgumentFailed), |
| 104 | + ExpectedHelper.asDataTypeArgumentFailed(this), |
| 105 | + ActualHelper.asDataTypeArgumentFailed(node))); |
| 106 | + return true; |
| 107 | + } |
| 108 | + |
101 | 109 | public boolean isApplicable(JNode node) { |
102 | 110 | return !nested || node instanceof JComposite; |
103 | 111 | } |
|
0 commit comments