|
1
|
|
|
package com.hltech.vaunt.validator; |
|
2
|
|
|
|
|
3
|
|
|
import com.fasterxml.jackson.module.jsonSchema.JsonSchema; |
|
4
|
|
|
|
|
5
|
|
|
import java.util.ArrayList; |
|
6
|
|
|
import java.util.Arrays; |
|
7
|
|
|
import java.util.List; |
|
8
|
|
|
|
|
9
|
|
|
public class JsonSchemaValidator { |
|
10
|
|
|
static final String ERROR_FORMAT = "Schema with id %s has not matching %s - consumer: %s, provider: %s"; |
|
11
|
|
|
|
|
12
|
|
|
public static List<String> validate(JsonSchema consumerSchema, JsonSchema providerSchema) { |
|
13
|
|
|
List<String> errors = new ArrayList<>(); |
|
14
|
|
|
|
|
15
|
|
|
if (!equals(consumerSchema.get$ref(), providerSchema.get$ref())) { |
|
16
|
|
|
errors.add(String.format(ERROR_FORMAT, |
|
17
|
|
|
consumerSchema.getId(), |
|
18
|
|
|
"$ref", |
|
19
|
|
|
consumerSchema.get$ref(), |
|
20
|
|
|
providerSchema.get$ref())); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
if (!equals(consumerSchema.get$schema(), providerSchema.get$schema())) { |
|
24
|
|
|
errors.add(String.format(ERROR_FORMAT, |
|
25
|
|
|
consumerSchema.getId(), |
|
26
|
|
|
"$schema", |
|
27
|
|
|
consumerSchema.get$schema(), |
|
28
|
|
|
providerSchema.get$schema())); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
if (!arraysEquals(consumerSchema.getDisallow(), providerSchema.getDisallow())) { |
|
32
|
|
|
errors.add(String.format(ERROR_FORMAT, |
|
33
|
|
|
consumerSchema.getId(), |
|
34
|
|
|
"disallow", |
|
35
|
|
|
jsonArraysToString(consumerSchema.getDisallow()), |
|
36
|
|
|
jsonArraysToString(providerSchema.getDisallow()))); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if (!arraysEquals(consumerSchema.getExtends(), providerSchema.getExtends())) { |
|
40
|
|
|
errors.add(String.format(ERROR_FORMAT, |
|
41
|
|
|
consumerSchema.getId(), |
|
42
|
|
|
"extends", |
|
43
|
|
|
jsonArraysToString(consumerSchema.getExtends()), |
|
44
|
|
|
jsonArraysToString(providerSchema.getExtends()))); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if (isRequired(consumerSchema) && !isRequired(providerSchema)) { |
|
48
|
|
|
errors.add(String.format(ERROR_FORMAT, |
|
49
|
|
|
consumerSchema.getId(), |
|
50
|
|
|
"required", |
|
51
|
|
|
consumerSchema.getRequired(), |
|
52
|
|
|
providerSchema.getRequired())); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if (!equals(consumerSchema.getReadonly(), providerSchema.getReadonly())) { |
|
56
|
|
|
errors.add(String.format(ERROR_FORMAT, |
|
57
|
|
|
consumerSchema.getId(), |
|
58
|
|
|
"readonly", |
|
59
|
|
|
consumerSchema.getReadonly(), |
|
60
|
|
|
providerSchema.getReadonly())); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (!equals(consumerSchema.getDescription(), providerSchema.getDescription())) { |
|
64
|
|
|
errors.add(String.format(ERROR_FORMAT, |
|
65
|
|
|
consumerSchema.getId(), |
|
66
|
|
|
"description", |
|
67
|
|
|
consumerSchema.getDescription(), |
|
68
|
|
|
providerSchema.getDescription())); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return errors; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
private static boolean isRequired(JsonSchema schema) { |
|
75
|
|
|
return schema.getRequired() != null && schema.getRequired(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public static boolean equals(Object object1, Object object2) { |
|
79
|
|
|
if (object1 == null) { |
|
80
|
|
|
return object2 == null; |
|
81
|
|
|
} else { |
|
82
|
|
|
return object1.equals(object2); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
private static <T> boolean arraysEquals(T[] array1, T[] array2) { |
|
87
|
|
|
if (array1 == null) { |
|
88
|
|
|
return array2 == null; |
|
89
|
|
|
} |
|
90
|
|
|
if (array2 == null) { |
|
91
|
|
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return Arrays.equals(array1, array2); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
private static String jsonArraysToString(JsonSchema[] array) { |
|
98
|
|
|
if (array == null) { |
|
99
|
|
|
return "null"; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
int inMax = array.length - 1; |
|
103
|
|
|
if (inMax == -1) { |
|
104
|
|
|
return "[]"; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
StringBuilder stringBuilder = new StringBuilder(); |
|
108
|
|
|
stringBuilder.append('['); |
|
109
|
|
|
for (int i = 0; ; i++) { |
|
110
|
|
|
stringBuilder.append(jsonArrayToString(array[i])); |
|
111
|
|
|
if (i == inMax) { |
|
112
|
|
|
return stringBuilder.append(']').toString(); |
|
113
|
|
|
} |
|
114
|
|
|
stringBuilder.append(", "); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
private static String jsonArrayToString(JsonSchema object) { |
|
119
|
|
|
return "id=" + object.getId(); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|