@@ -14,19 +14,15 @@ public class StringSchemaValidatingVisitor extends Visitor {
14
14
15
15
private int stringLength ;
16
16
17
- private final ValidationFailureReporter failureReporter ;
17
+ private final ValidatingVisitor owner ;
18
18
19
- public StringSchemaValidatingVisitor (Object subject , ValidationFailureReporter failureReporter ) {
19
+ public StringSchemaValidatingVisitor (Object subject , ValidatingVisitor owner ) {
20
20
this .subject = subject ;
21
- this .failureReporter = requireNonNull (failureReporter , "failureReporter cannot be null" );
21
+ this .owner = requireNonNull (owner , "failureReporter cannot be null" );
22
22
}
23
23
24
24
@ Override void visitStringSchema (StringSchema stringSchema ) {
25
- if (!(subject instanceof String )) {
26
- if (stringSchema .requireString ()) {
27
- failureReporter .failure (String .class , subject );
28
- }
29
- } else {
25
+ if (owner .passesTypeCheck (String .class , stringSchema .requireString (), stringSchema .isNullable ())) {
30
26
stringSubject = (String ) subject ;
31
27
stringLength = stringSubject .codePointCount (0 , stringSubject .length ());
32
28
super .visitStringSchema (stringSchema );
@@ -35,14 +31,14 @@ public StringSchemaValidatingVisitor(Object subject, ValidationFailureReporter f
35
31
36
32
@ Override void visitMinLength (Integer minLength ) {
37
33
if (minLength != null && stringLength < minLength .intValue ()) {
38
- failureReporter .failure ("expected minLength: " + minLength + ", actual: "
34
+ owner .failure ("expected minLength: " + minLength + ", actual: "
39
35
+ stringLength , "minLength" );
40
36
}
41
37
}
42
38
43
39
@ Override void visitMaxLength (Integer maxLength ) {
44
40
if (maxLength != null && stringLength > maxLength .intValue ()) {
45
- failureReporter .failure ("expected maxLength: " + maxLength + ", actual: "
41
+ owner .failure ("expected maxLength: " + maxLength + ", actual: "
46
42
+ stringLength , "maxLength" );
47
43
}
48
44
}
@@ -51,14 +47,14 @@ public StringSchemaValidatingVisitor(Object subject, ValidationFailureReporter f
51
47
if (pattern != null && !pattern .matcher (stringSubject ).find ()) {
52
48
String message = format ("string [%s] does not match pattern %s" ,
53
49
subject , pattern .pattern ());
54
- failureReporter .failure (message , "pattern" );
50
+ owner .failure (message , "pattern" );
55
51
}
56
52
}
57
53
58
54
@ Override void visitFormat (FormatValidator formatValidator ) {
59
55
Optional <String > failure = formatValidator .validate (stringSubject );
60
56
if (failure .isPresent ()) {
61
- failureReporter .failure (failure .get (), "format" );
57
+ owner .failure (failure .get (), "format" );
62
58
}
63
59
}
64
60
0 commit comments