5
5
import com .relogiclabs .json .schema .internal .builder .JDataTypeBuilder ;
6
6
import com .relogiclabs .json .schema .internal .message .ActualHelper ;
7
7
import com .relogiclabs .json .schema .internal .message .ExpectedHelper ;
8
- import com .relogiclabs .json .schema .internal .message .MatchReport ;
9
8
import com .relogiclabs .json .schema .message .ErrorDetail ;
10
9
import com .relogiclabs .json .schema .util .Reference ;
11
10
import lombok .EqualsAndHashCode ;
12
11
import lombok .Getter ;
13
12
14
- import static com .relogiclabs .json .schema .internal .message .MatchReport .AliasError ;
15
- import static com .relogiclabs .json .schema .internal .message .MatchReport .ArgumentError ;
16
- import static com .relogiclabs .json .schema .internal .message .MatchReport .Success ;
17
- import static com .relogiclabs .json .schema .internal .message .MatchReport .TypeError ;
18
13
import static com .relogiclabs .json .schema .internal .message .MessageHelper .DataTypeArgumentFailed ;
19
14
import static com .relogiclabs .json .schema .internal .message .MessageHelper .DataTypeMismatch ;
20
- import static com .relogiclabs .json .schema .internal .message .MessageHelper .InvalidNestedDataType ;
21
15
import static com .relogiclabs .json .schema .internal .util .CollectionHelper .asList ;
22
- import static com .relogiclabs .json .schema .internal .util .StreamHelper .allTrue ;
23
16
import static com .relogiclabs .json .schema .internal .util .StringHelper .concat ;
24
17
import static com .relogiclabs .json .schema .internal .util .StringHelper .quote ;
25
- import static com .relogiclabs .json .schema .message .ErrorCode .DTYP03 ;
18
+ import static com .relogiclabs .json .schema .message .ErrorCode .DEFI03 ;
19
+ import static com .relogiclabs .json .schema .message .ErrorCode .DEFI04 ;
20
+ import static com .relogiclabs .json .schema .message .ErrorCode .DTYP04 ;
21
+ import static com .relogiclabs .json .schema .message .ErrorCode .DTYP05 ;
22
+ import static com .relogiclabs .json .schema .message .ErrorCode .DTYP06 ;
23
+ import static com .relogiclabs .json .schema .message .ErrorCode .DTYP07 ;
26
24
import static com .relogiclabs .json .schema .message .MessageFormatter .formatForSchema ;
27
25
import static java .util .Objects .requireNonNull ;
28
26
import static org .apache .commons .lang3 .StringUtils .isEmpty ;
32
30
@ EqualsAndHashCode
33
31
public final class JDataType extends JBranch implements NestedMode {
34
32
static final String NESTED_MARKER = "*" ;
33
+ static final String DATA_TYPE_NAME = "DATA_TYPE_NAME" ;
35
34
private final JsonType jsonType ;
36
35
private final JAlias alias ;
37
36
private final boolean nested ;
@@ -50,53 +49,29 @@ public static JDataType from(JDataTypeBuilder builder) {
50
49
51
50
@ Override
52
51
public boolean match (JNode node ) {
53
- if (!nested ) return isMatchCurrent (node );
54
- if (!(node instanceof JComposite composite )) return false ;
55
- return composite .components ().stream ().map (this ::isMatchCurrent ).allMatch (allTrue ());
56
- }
57
-
58
- private boolean isMatchCurrent (JNode node ) {
59
- return matchCurrent (node , new Reference <>()) == Success ;
60
- }
61
-
62
- private MatchReport matchCurrent (JNode node , Reference <String > error ) {
63
- var result = jsonType .match (node , error ) ? Success : TypeError ;
64
- if (alias == null || result != Success ) return result ;
65
- var validator = getRuntime ().getDefinitions ().get (alias );
66
- if (validator == null ) return AliasError ;
67
- result = validator .match (node ) ? Success : ArgumentError ;
68
- return result ;
69
- }
70
-
71
- boolean matchForReport (JNode node ) {
72
- if (!nested ) return matchForReport (node , false );
73
- if (!(node instanceof JComposite composite ))
74
- return failWith (new JsonSchemaException (
75
- new ErrorDetail (DTYP03 , InvalidNestedDataType ),
76
- ExpectedHelper .asInvalidNestedDataType (this ),
77
- ActualHelper .asInvalidNestedDataType (node )));
78
- boolean result = true ;
79
- for (var c : composite .components ()) result &= matchForReport (c , true );
80
- return result ;
81
- }
82
-
83
- private boolean matchForReport (JNode node , boolean nested ) {
84
52
Reference <String > error = new Reference <>();
85
- var result = matchCurrent (node , error );
86
- if (result == TypeError ) return failWith (new JsonSchemaException (
87
- new ErrorDetail (TypeError .getCode (nested ),
53
+ if (!jsonType .match (node , error )) return failTypeWith (new JsonSchemaException (
54
+ new ErrorDetail (nested ? DTYP06 : DTYP04 ,
88
55
formatMessage (DataTypeMismatch , error .getValue ())),
89
56
ExpectedHelper .asDataTypeMismatch (this ),
90
57
ActualHelper .asDataTypeMismatch (node )));
91
- if (result == AliasError ) return failWith (new DefinitionNotFoundException (formatForSchema (
92
- AliasError .getCode (nested ), "No definition found for " + quote (alias ), this )));
93
- if (result == ArgumentError ) return failWith (new JsonSchemaException (
94
- new ErrorDetail (ArgumentError .getCode (nested ), DataTypeArgumentFailed ),
58
+ if (alias == null ) return true ;
59
+ var validator = getRuntime ().getDefinitions ().get (alias );
60
+ if (validator == null ) return failWith (new DefinitionNotFoundException (
61
+ formatForSchema (nested ? DEFI04 : DEFI03 , "No definition found for "
62
+ + quote (alias ), this )));
63
+ if (!validator .match (node )) return failWith (new JsonSchemaException (
64
+ new ErrorDetail (nested ? DTYP07 : DTYP05 , DataTypeArgumentFailed ),
95
65
ExpectedHelper .asDataTypeArgumentFailed (this ),
96
66
ActualHelper .asDataTypeArgumentFailed (node )));
97
67
return true ;
98
68
}
99
69
70
+ private boolean failTypeWith (JsonSchemaException exception ) {
71
+ exception .setAttribute (DATA_TYPE_NAME , toString (true ));
72
+ return failWith (exception );
73
+ }
74
+
100
75
private static String formatMessage (String main , String optional ) {
101
76
return isEmpty (optional ) ? main : concat (main , " (" , uncapitalize (optional ), ")" );
102
77
}
0 commit comments