Skip to content

Commit

Permalink
throw exception if invalid option value
Browse files Browse the repository at this point in the history
  • Loading branch information
pkwarren committed Oct 10, 2023
1 parent e0dedbe commit 3ed122d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package build.buf.protovalidate.internal.evaluator;

import build.buf.protovalidate.exceptions.CompilationException;
import build.buf.validate.FieldConstraints;
import build.buf.validate.MessageConstraints;
import build.buf.validate.OneofConstraints;
Expand All @@ -35,7 +36,7 @@ class ConstraintResolver {
* @return the resolved {@link MessageConstraints}.
*/
MessageConstraints resolveMessageConstraints(Descriptor desc)
throws InvalidProtocolBufferException {
throws InvalidProtocolBufferException, CompilationException {
DescriptorProtos.MessageOptions options = desc.getOptions();
if (!options.hasExtension(ValidateProto.message)) {
return MessageConstraints.getDefaultInstance();
Expand All @@ -51,7 +52,7 @@ MessageConstraints resolveMessageConstraints(Descriptor desc)
// java_package.
return MessageConstraints.parseFrom(((MessageLite) value).toByteString());
}
return MessageConstraints.getDefaultInstance();
throw new CompilationException("unexpected message constraint option type: " + value);
}

/**
Expand All @@ -61,7 +62,7 @@ MessageConstraints resolveMessageConstraints(Descriptor desc)
* @return the resolved {@link OneofConstraints}.
*/
OneofConstraints resolveOneofConstraints(OneofDescriptor desc)
throws InvalidProtocolBufferException {
throws InvalidProtocolBufferException, CompilationException {
DescriptorProtos.OneofOptions options = desc.getOptions();
if (!options.hasExtension(ValidateProto.oneof)) {
return OneofConstraints.getDefaultInstance();
Expand All @@ -77,7 +78,7 @@ OneofConstraints resolveOneofConstraints(OneofDescriptor desc)
// java_package.
return OneofConstraints.parseFrom(((MessageLite) value).toByteString());
}
return OneofConstraints.getDefaultInstance();
throw new CompilationException("unexpected oneof constraint option type: " + value);
}

/**
Expand All @@ -87,7 +88,7 @@ OneofConstraints resolveOneofConstraints(OneofDescriptor desc)
* @return the resolved {@link FieldConstraints}.
*/
FieldConstraints resolveFieldConstraints(FieldDescriptor desc)
throws InvalidProtocolBufferException {
throws InvalidProtocolBufferException, CompilationException {
DescriptorProtos.FieldOptions options = desc.getOptions();
if (!options.hasExtension(ValidateProto.field)) {
return FieldConstraints.getDefaultInstance();
Expand All @@ -103,6 +104,6 @@ FieldConstraints resolveFieldConstraints(FieldDescriptor desc)
// java_package.
return FieldConstraints.parseFrom(((MessageLite) value).toByteString());
}
return FieldConstraints.getDefaultInstance();
throw new CompilationException("unexpected field constraint option type: " + value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private void processMessageExpressions(
}

private void processOneofConstraints(Descriptor desc, MessageEvaluator msgEval)
throws InvalidProtocolBufferException {
throws InvalidProtocolBufferException, CompilationException {
List<Descriptors.OneofDescriptor> oneofs = desc.getOneofs();
for (Descriptors.OneofDescriptor oneofDesc : oneofs) {
OneofConstraints oneofConstraints = resolver.resolveOneofConstraints(oneofDesc);
Expand Down

0 comments on commit 3ed122d

Please sign in to comment.