1
|
|
|
package com.hltech.vaunt.validator.schema; |
2
|
|
|
|
3
|
|
|
import com.fasterxml.jackson.module.jsonSchema.JsonSchema; |
4
|
|
|
import com.google.common.collect.Lists; |
5
|
|
|
import com.google.common.collect.Sets; |
6
|
|
|
import com.hltech.vaunt.validator.VauntValidationException; |
7
|
|
|
|
8
|
|
|
import java.util.HashSet; |
9
|
|
|
import java.util.List; |
10
|
|
|
import java.util.Set; |
11
|
|
|
import java.util.stream.Collectors; |
12
|
|
|
|
13
|
|
|
public class SchemaValidator { |
14
|
|
|
|
15
|
|
|
static final String UNMATCHING_SCHEMA_TYPE = |
16
|
|
|
"Consumer schema with id %s and type %s does not match provider schema with id %s and type %s"; |
17
|
|
|
private static final String VALIDATOR_SEARCH_ERROR = |
18
|
|
|
"Exactly one validator should exist for consumer and provider of type %s"; |
19
|
|
|
|
20
|
|
|
private static final Set<JsonSchemaValidator> schemaValidators = new HashSet<>(); |
21
|
|
|
|
22
|
|
|
static { |
23
|
|
|
schemaValidators.addAll(Sets.newHashSet( |
24
|
|
|
new StringSchemaValidator(), |
25
|
|
|
new ObjectSchemaValidator(), |
26
|
|
|
new BooleanSchemaValidator(), |
27
|
|
|
new IntegerSchemaValidator(), |
28
|
|
|
new NumberSchemaValidator(), |
29
|
|
|
new ArraySchemaValidator() |
30
|
|
|
)); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public static List<String> validate(JsonSchema consumerSchema, JsonSchema providerSchema) { |
34
|
|
|
|
35
|
|
|
if (!(consumerSchema.getClass() == providerSchema.getClass())) { |
36
|
|
|
return Lists.newArrayList( |
37
|
|
|
String.format(UNMATCHING_SCHEMA_TYPE, |
38
|
|
|
consumerSchema.getId(), |
39
|
|
|
consumerSchema.getClass().getSimpleName(), |
40
|
|
|
providerSchema.getId(), |
41
|
|
|
providerSchema.getClass().getSimpleName())); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
List<JsonSchemaValidator> matchingValidators = schemaValidators.stream() |
45
|
|
|
.filter(v -> v.supportsSchemaType().equals(consumerSchema.getClass())) |
46
|
|
|
.collect(Collectors.toList()); |
47
|
|
|
|
48
|
|
|
if (matchingValidators.size() != 1) { |
49
|
|
|
throw new VauntValidationException(String.format( |
50
|
|
|
VALIDATOR_SEARCH_ERROR, consumerSchema.getClass().getSimpleName())); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return matchingValidators.get(0).validate(consumerSchema, providerSchema); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|