1
+ package com .relogiclabs .jschema .node ;
2
+
3
+ import com .relogiclabs .jschema .exception .DefinitionNotFoundException ;
4
+ import com .relogiclabs .jschema .exception .JsonSchemaException ;
5
+ import com .relogiclabs .jschema .internal .builder .JDataTypeBuilder ;
6
+ import com .relogiclabs .jschema .internal .message .ActualHelper ;
7
+ import com .relogiclabs .jschema .internal .message .ExpectedHelper ;
8
+ import com .relogiclabs .jschema .message .ErrorDetail ;
9
+ import com .relogiclabs .jschema .util .Reference ;
10
+ import lombok .EqualsAndHashCode ;
11
+ import lombok .Getter ;
12
+
13
+ import static com .relogiclabs .jschema .internal .message .MessageHelper .DataTypeArgumentFailed ;
14
+ import static com .relogiclabs .jschema .internal .message .MessageHelper .DataTypeMismatch ;
15
+ import static com .relogiclabs .jschema .internal .util .CollectionHelper .asList ;
16
+ import static com .relogiclabs .jschema .internal .util .StringHelper .concat ;
17
+ import static com .relogiclabs .jschema .message .ErrorCode .DEFI03 ;
18
+ import static com .relogiclabs .jschema .message .ErrorCode .DEFI04 ;
19
+ import static com .relogiclabs .jschema .message .ErrorCode .DTYP04 ;
20
+ import static com .relogiclabs .jschema .message .ErrorCode .DTYP05 ;
21
+ import static com .relogiclabs .jschema .message .ErrorCode .DTYP06 ;
22
+ import static com .relogiclabs .jschema .message .ErrorCode .DTYP07 ;
23
+ import static com .relogiclabs .jschema .message .MessageFormatter .formatForSchema ;
24
+ import static java .util .Objects .requireNonNull ;
25
+ import static org .apache .commons .lang3 .StringUtils .isEmpty ;
26
+ import static org .apache .commons .lang3 .StringUtils .uncapitalize ;
27
+
28
+ @ Getter
29
+ @ EqualsAndHashCode
30
+ public final class JDataType extends JBranch implements NestedMode {
31
+ static final String NESTED_MARKER = "*" ;
32
+ static final String DATA_TYPE_NAME = "DATA_TYPE_NAME" ;
33
+ private final JsonType jsonType ;
34
+ private final JAlias alias ;
35
+ private final boolean nested ;
36
+
37
+ private JDataType (JDataTypeBuilder builder ) {
38
+ super (builder );
39
+ jsonType = requireNonNull (builder .jsonType ());
40
+ nested = requireNonNull (builder .nested ());
41
+ alias = builder .alias ();
42
+ children = asList (alias );
43
+ }
44
+
45
+ public static JDataType from (JDataTypeBuilder builder ) {
46
+ return new JDataType (builder ).initialize ();
47
+ }
48
+
49
+ @ Override
50
+ public boolean match (JNode node ) {
51
+ Reference <String > error = new Reference <>();
52
+ if (!jsonType .match (node , error )) return failTypeWith (new JsonSchemaException (
53
+ new ErrorDetail (nested ? DTYP06 : DTYP04 ,
54
+ formatMessage (DataTypeMismatch , error .getValue ())),
55
+ ExpectedHelper .asDataTypeMismatch (this ),
56
+ ActualHelper .asDataTypeMismatch (node )));
57
+ if (alias == null ) return true ;
58
+ var validator = getRuntime ().getDefinitions ().get (alias );
59
+ if (validator == null ) return fail (new DefinitionNotFoundException (formatForSchema (
60
+ nested ? DEFI04 : DEFI03 , concat ("No definition found for '" , alias , "'" ), this )));
61
+ if (!validator .match (node )) return fail (new JsonSchemaException (
62
+ new ErrorDetail (nested ? DTYP07 : DTYP05 , DataTypeArgumentFailed ),
63
+ ExpectedHelper .asDataTypeArgumentFailed (this ),
64
+ ActualHelper .asDataTypeArgumentFailed (node )));
65
+ return true ;
66
+ }
67
+
68
+ private boolean failTypeWith (JsonSchemaException exception ) {
69
+ exception .setAttribute (DATA_TYPE_NAME , toString (true ));
70
+ return fail (exception );
71
+ }
72
+
73
+ private static String formatMessage (String main , String optional ) {
74
+ return isEmpty (optional ) ? main : concat (main , " (" , uncapitalize (optional ), ")" );
75
+ }
76
+
77
+ boolean isApplicable (JNode node ) {
78
+ return !nested || node instanceof JComposite ;
79
+ }
80
+
81
+ boolean isMatchNull () {
82
+ return !nested && jsonType .isNullType ();
83
+ }
84
+
85
+ @ Override
86
+ public String toString () {
87
+ return toString (false );
88
+ }
89
+
90
+ public String toString (boolean baseForm ) {
91
+ var builder = new StringBuilder (jsonType .toString ());
92
+ if (nested && !baseForm ) builder .append (NESTED_MARKER );
93
+ if (alias != null && !baseForm ) builder .append ("(" ).append (alias ).append (")" );
94
+ return builder .toString ();
95
+ }
96
+ }
0 commit comments