|
| 1 | +package org.everit.json.schema; |
| 2 | + |
| 3 | + |
| 4 | +import java.util.Arrays; |
| 5 | + |
| 6 | +import static java.util.Objects.requireNonNull; |
| 7 | + |
| 8 | +public class ConditionalSchemaValidatingVisitor extends Visitor { |
| 9 | + |
| 10 | + private final Object subject; |
| 11 | + |
| 12 | + private final ValidatingVisitor owner; |
| 13 | + |
| 14 | + public ConditionalSchemaValidatingVisitor(Object subject, ValidatingVisitor owner) { |
| 15 | + this.subject = subject; |
| 16 | + this.owner = requireNonNull(owner, "owner cannot be null"); |
| 17 | + } |
| 18 | + |
| 19 | + @Override |
| 20 | + void visitConditionalSchema(ConditionalSchema conditionalSchema) { |
| 21 | + if (!conditionalSchema.getIfSchema().isPresent() || |
| 22 | + (!conditionalSchema.getThenSchema().isPresent() && !conditionalSchema.getElseSchema().isPresent())) { |
| 23 | + return; |
| 24 | + } |
| 25 | + ValidationException ifSchemaException = owner.getFailureOfSchema(conditionalSchema.getIfSchema().get(), subject); |
| 26 | + if (ifSchemaException == null) { |
| 27 | + visitThenSchema(conditionalSchema); |
| 28 | + } else { |
| 29 | + visitElseSchema(conditionalSchema, ifSchemaException); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + private void visitThenSchema(ConditionalSchema conditionalSchema) { |
| 34 | + if (conditionalSchema.getThenSchema().isPresent()) { |
| 35 | + ValidationException thenSchemaException = owner.getFailureOfSchema(conditionalSchema.getThenSchema().get(), subject); |
| 36 | + if (thenSchemaException != null) { |
| 37 | + owner.failure(new ValidationException(conditionalSchema, |
| 38 | + new StringBuilder(new StringBuilder("#")), |
| 39 | + "Data is invalid for schema of \"then\" ", |
| 40 | + Arrays.asList(thenSchemaException), |
| 41 | + "then", |
| 42 | + conditionalSchema.getSchemaLocation())); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private void visitElseSchema(ConditionalSchema conditionalSchema, ValidationException ifSchemaException) { |
| 48 | + if (conditionalSchema.getElseSchema().isPresent()) { |
| 49 | + ValidationException elseSchemaException = owner.getFailureOfSchema(conditionalSchema.getElseSchema().get(), subject); |
| 50 | + if (elseSchemaException != null) { |
| 51 | + owner.failure(new ValidationException(conditionalSchema, |
| 52 | + new StringBuilder(new StringBuilder("#")), |
| 53 | + "Data is invalid for schema of both \"if\" and \"else\" ", |
| 54 | + Arrays.asList(ifSchemaException, elseSchemaException), |
| 55 | + "else", |
| 56 | + conditionalSchema.getSchemaLocation())); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | +} |
0 commit comments