55import com .relogiclabs .json .schema .internal .builder .JDataTypeBuilder ;
66import com .relogiclabs .json .schema .internal .message .ActualHelper ;
77import com .relogiclabs .json .schema .internal .message .ExpectedHelper ;
8- import com .relogiclabs .json .schema .internal .message .MatchReport ;
98import com .relogiclabs .json .schema .message .ErrorDetail ;
109import com .relogiclabs .json .schema .util .Reference ;
1110import lombok .EqualsAndHashCode ;
1211import lombok .Getter ;
1312
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 ;
1813import static com .relogiclabs .json .schema .internal .message .MessageHelper .DataTypeArgumentFailed ;
1914import static com .relogiclabs .json .schema .internal .message .MessageHelper .DataTypeMismatch ;
20- import static com .relogiclabs .json .schema .internal .message .MessageHelper .InvalidNestedDataType ;
2115import static com .relogiclabs .json .schema .internal .util .CollectionHelper .asList ;
22- import static com .relogiclabs .json .schema .internal .util .StreamHelper .allTrue ;
2316import static com .relogiclabs .json .schema .internal .util .StringHelper .concat ;
2417import 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 ;
2624import static com .relogiclabs .json .schema .message .MessageFormatter .formatForSchema ;
2725import static java .util .Objects .requireNonNull ;
2826import static org .apache .commons .lang3 .StringUtils .isEmpty ;
3230@ EqualsAndHashCode
3331public final class JDataType extends JBranch implements NestedMode {
3432 static final String NESTED_MARKER = "*" ;
33+ static final String DATA_TYPE_NAME = "DATA_TYPE_NAME" ;
3534 private final JsonType jsonType ;
3635 private final JAlias alias ;
3736 private final boolean nested ;
@@ -50,53 +49,29 @@ public static JDataType from(JDataTypeBuilder builder) {
5049
5150 @ Override
5251 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 ) {
8452 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 ,
8855 formatMessage (DataTypeMismatch , error .getValue ())),
8956 ExpectedHelper .asDataTypeMismatch (this ),
9057 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 ),
9565 ExpectedHelper .asDataTypeArgumentFailed (this ),
9666 ActualHelper .asDataTypeArgumentFailed (node )));
9767 return true ;
9868 }
9969
70+ private boolean failTypeWith (JsonSchemaException exception ) {
71+ exception .setAttribute (DATA_TYPE_NAME , toString (true ));
72+ return failWith (exception );
73+ }
74+
10075 private static String formatMessage (String main , String optional ) {
10176 return isEmpty (optional ) ? main : concat (main , " (" , uncapitalize (optional ), ")" );
10277 }
0 commit comments