Passed
Push — master ( 770833...886a8d )
by Filip
02:26
created

validate(StringSchema,StringSchema)   A

Complexity

Conditions 4

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 21
dl 0
loc 28
rs 9.376
c 1
b 0
f 0
1
package com.hltech.vaunt.validator;
2
3
import com.fasterxml.jackson.module.jsonSchema.types.StringSchema;
4
5
import java.util.ArrayList;
6
import java.util.List;
7
8
import static com.hltech.vaunt.validator.JsonSchemaValidator.ERROR_FORMAT;
9
10
public class StringSchemaValidator {
11
12
    public static List<String> validate(StringSchema consumerSchema, StringSchema providerSchema) {
13
        List<String> errors = new ArrayList<>(ValueTypeSchemaValidator.validate(consumerSchema, providerSchema));
14
15
        if (!equals(consumerSchema.getMinLength(), providerSchema.getMinLength())) {
16
            errors.add(String.format(ERROR_FORMAT,
17
                    consumerSchema.getId(),
18
                    "minLength",
19
                    consumerSchema.getMinLength(),
20
                    providerSchema.getMinLength()));
21
        }
22
23
        if (!equals(consumerSchema.getMaxLength(), providerSchema.getMaxLength())) {
24
            errors.add(String.format(ERROR_FORMAT,
25
                    consumerSchema.getId(),
26
                    "maxLength",
27
                    consumerSchema.getMaxLength(),
28
                    providerSchema.getMaxLength()));
29
        }
30
31
        if (!equals(consumerSchema.getPattern(), providerSchema.getPattern())) {
32
            errors.add(String.format(ERROR_FORMAT,
33
                    consumerSchema.getId(),
34
                    "pattern",
35
                    consumerSchema.getPattern(),
36
                    providerSchema.getPattern()));
37
        }
38
39
        return errors;
40
    }
41
42
    private static boolean equals(Object object1, Object object2) {
43
        if (object1 == null) {
44
            return object2 == null;
45
        } else {
46
            return object1.equals(object2);
47
        }
48
    }
49
}
50