1
|
|
|
package com.hltech.vaunt.validator.schema; |
2
|
|
|
|
3
|
|
|
import com.fasterxml.jackson.module.jsonSchema.JsonSchema; |
4
|
|
|
import com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema; |
5
|
|
|
import com.google.common.collect.Lists; |
6
|
|
|
|
7
|
|
|
import java.util.List; |
8
|
|
|
import java.util.Map; |
9
|
|
|
import java.util.stream.Collectors; |
10
|
|
|
|
11
|
|
|
public class ObjectSchemaValidator extends ContainerTypeSchemaValidator { |
12
|
|
|
private static final String UNMATCHING_SCHEMA_TYPE = |
13
|
|
|
"Consumer schema with id %s and type %s does not match provider schema with id %s and type %s"; |
14
|
|
|
|
15
|
|
|
@Override |
16
|
|
|
public List<String> validate(JsonSchema consumerSchema, JsonSchema providerSchema) { |
17
|
|
|
List<String> errors = super.validate(consumerSchema, providerSchema); |
18
|
|
|
|
19
|
|
|
ObjectSchema consumerObjectSchema = consumerSchema.asObjectSchema(); |
20
|
|
|
ObjectSchema providerObjectSchema = providerSchema.asObjectSchema(); |
21
|
|
|
|
22
|
|
|
if (!equals(consumerObjectSchema.getAdditionalProperties(), providerObjectSchema.getAdditionalProperties())) { |
23
|
|
|
errors.add(String.format(ERROR_FORMAT_SHORT, |
24
|
|
|
consumerSchema.getId(), |
25
|
|
|
"additionalProperties")); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if (!equals(consumerObjectSchema.getDependencies(), providerObjectSchema.getDependencies())) { |
29
|
|
|
errors.add(String.format(ERROR_FORMAT, |
30
|
|
|
consumerSchema.getId(), |
31
|
|
|
"dependencies", |
32
|
|
|
consumerObjectSchema.getDependencies(), |
33
|
|
|
providerObjectSchema.getDependencies())); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if (!equals(consumerObjectSchema.getPatternProperties(), providerObjectSchema.getPatternProperties())) { |
37
|
|
|
errors.add(String.format(ERROR_FORMAT, |
38
|
|
|
consumerSchema.getId(), |
39
|
|
|
"patternProperties", |
40
|
|
|
mapToString(consumerObjectSchema.getPatternProperties()), |
41
|
|
|
mapToString(providerObjectSchema.getPatternProperties()))); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
errors.addAll(compareObjectProperties( |
45
|
|
|
consumerSchema.getId(), |
46
|
|
|
consumerObjectSchema.getProperties(), |
47
|
|
|
providerObjectSchema.getProperties())); |
48
|
|
|
|
49
|
|
|
return errors; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
@Override |
53
|
|
|
public Class<ObjectSchema> supportsSchemaType() { |
54
|
|
|
return ObjectSchema.class; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private List<String> compareObjectProperties(String id, Map<String, JsonSchema> consumerProperties, |
58
|
|
|
Map<String, JsonSchema> providerProperties) { |
59
|
|
|
|
60
|
|
|
if (!providerProperties.keySet().containsAll(consumerProperties.keySet())) { |
61
|
|
|
return Lists.newArrayList(String.format(ERROR_FORMAT, |
62
|
|
|
id, |
63
|
|
|
"properties", |
64
|
|
|
"ids of properties: " + consumerProperties.keySet(), |
65
|
|
|
"ids of properties: " + providerProperties.keySet() |
66
|
|
|
)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return consumerProperties.keySet().stream() |
70
|
|
|
.flatMap(key -> validateProperty(consumerProperties.get(key), providerProperties.get(key)).stream()) |
71
|
|
|
.collect(Collectors.toList()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private List<String> validateProperty(JsonSchema consumerProperty, JsonSchema providerProperty) { |
75
|
|
|
return consumerProperty.getClass() == providerProperty.getClass() |
76
|
|
|
? SchemaValidator.validate(consumerProperty, providerProperty) |
77
|
|
|
: Lists.newArrayList(String.format( |
78
|
|
|
UNMATCHING_SCHEMA_TYPE, |
79
|
|
|
consumerProperty.getId(), |
80
|
|
|
consumerProperty.getClass().getSimpleName(), |
81
|
|
|
providerProperty.getId(), |
82
|
|
|
providerProperty.getClass().getSimpleName())); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private String mapToString(Map<String, JsonSchema> props) { |
86
|
|
|
String content = props.entrySet() |
87
|
|
|
.stream() |
88
|
|
|
.map(e -> e.getKey() + '=' + jsonToString(e.getValue())) |
89
|
|
|
.collect(Collectors.joining(", ")); |
90
|
|
|
|
91
|
|
|
return "{" + content + "}"; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|