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
|
|
|
|
13
|
|
|
@Override |
14
|
|
|
public List<String> validate(JsonSchema consumerSchema, JsonSchema providerSchema) { |
15
|
|
|
List<String> errors = super.validate(consumerSchema, providerSchema); |
16
|
|
|
|
17
|
|
|
ObjectSchema consumerObjectSchema = consumerSchema.asObjectSchema(); |
18
|
|
|
ObjectSchema providerObjectSchema = providerSchema.asObjectSchema(); |
19
|
|
|
|
20
|
|
|
if (!isValid(consumerObjectSchema.getAdditionalProperties(), providerObjectSchema.getAdditionalProperties())) { |
21
|
|
|
errors.add(String.format(ERROR_FORMAT_SHORT, |
22
|
|
|
consumerSchema.getId(), |
23
|
|
|
"additionalProperties")); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
if (!isMapValid(consumerObjectSchema.getDependencies(), providerObjectSchema.getDependencies())) { |
27
|
|
|
errors.add(String.format(ERROR_FORMAT, |
28
|
|
|
consumerSchema.getId(), |
29
|
|
|
"dependencies", |
30
|
|
|
consumerObjectSchema.getDependencies(), |
31
|
|
|
providerObjectSchema.getDependencies())); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (!isMapValid(consumerObjectSchema.getPatternProperties(), providerObjectSchema.getPatternProperties())) { |
35
|
|
|
errors.add(String.format(ERROR_FORMAT, |
36
|
|
|
consumerSchema.getId(), |
37
|
|
|
"patternProperties", |
38
|
|
|
mapToString(consumerObjectSchema.getPatternProperties()), |
39
|
|
|
mapToString(providerObjectSchema.getPatternProperties()))); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
errors.addAll(compareObjectProperties( |
43
|
|
|
consumerSchema.getId(), |
44
|
|
|
consumerObjectSchema.getProperties(), |
45
|
|
|
providerObjectSchema.getProperties())); |
46
|
|
|
|
47
|
|
|
return errors; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
@Override |
51
|
|
|
public Class<ObjectSchema> supportsSchemaType() { |
52
|
|
|
return ObjectSchema.class; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private List<String> compareObjectProperties(String id, Map<String, JsonSchema> consumerProperties, |
56
|
|
|
Map<String, JsonSchema> providerProperties) { |
57
|
|
|
|
58
|
|
|
if (!providerProperties.keySet().containsAll(consumerProperties.keySet())) { |
59
|
|
|
return Lists.newArrayList(String.format(ERROR_FORMAT, |
60
|
|
|
id, |
61
|
|
|
"properties", |
62
|
|
|
"ids of properties: " + consumerProperties.keySet(), |
63
|
|
|
"ids of properties: " + providerProperties.keySet() |
64
|
|
|
)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return consumerProperties.keySet().stream() |
68
|
|
|
.flatMap(key -> validateProperty(consumerProperties.get(key), providerProperties.get(key)).stream()) |
69
|
|
|
.collect(Collectors.toList()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private List<String> validateProperty(JsonSchema consumerProperty, JsonSchema providerProperty) { |
73
|
|
|
return SchemaValidator.validate(consumerProperty, providerProperty); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private String mapToString(Map<String, JsonSchema> props) { |
77
|
|
|
String content = props.entrySet() |
78
|
|
|
.stream() |
79
|
|
|
.map(e -> e.getKey() + '=' + jsonToString(e.getValue())) |
80
|
|
|
.collect(Collectors.joining(", ")); |
81
|
|
|
|
82
|
|
|
return "{" + content + "}"; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|