Allow providing an external validator
Signed-off-by: Ruben Romero Montes <rromerom@redhat.com>
This commit is contained in:
parent
b0a6101b23
commit
1cff239ab4
|
@ -42,7 +42,14 @@ import io.cloudevents.fun.EventBuilder;
|
|||
*/
|
||||
public final class CloudEventBuilder<T> implements
|
||||
EventBuilder<T, AttributesImpl> {
|
||||
private CloudEventBuilder() {}
|
||||
|
||||
private CloudEventBuilder(Validator validator) {
|
||||
if(validator == null) {
|
||||
this.validator = getValidator();
|
||||
} else {
|
||||
this.validator = validator;
|
||||
}
|
||||
}
|
||||
|
||||
private static Validator VALIDATOR;
|
||||
|
||||
|
@ -65,6 +72,7 @@ public final class CloudEventBuilder<T> implements
|
|||
private T data;
|
||||
|
||||
private final Set<ExtensionFormat> extensions = new HashSet<>();
|
||||
private final Validator validator;
|
||||
|
||||
private static Validator getValidator() {
|
||||
if(null== VALIDATOR) {
|
||||
|
@ -78,14 +86,22 @@ public final class CloudEventBuilder<T> implements
|
|||
* @param <T> The 'data' type
|
||||
*/
|
||||
public static <T> CloudEventBuilder<T> builder() {
|
||||
return new CloudEventBuilder<T>();
|
||||
return new CloudEventBuilder<T>(null);
|
||||
}
|
||||
|
||||
public static <T> CloudEventBuilder<T> builder(Validator validator) {
|
||||
return new CloudEventBuilder<T>(validator);
|
||||
}
|
||||
public static <T> CloudEventBuilder<T> builder(
|
||||
CloudEvent<AttributesImpl, T> base) {
|
||||
return builder(base, null);
|
||||
}
|
||||
|
||||
public static <T> CloudEventBuilder<T> builder(
|
||||
CloudEvent<AttributesImpl, T> base) {
|
||||
CloudEvent<AttributesImpl, T> base, Validator validator) {
|
||||
Objects.requireNonNull(base);
|
||||
|
||||
CloudEventBuilder<T> result = new CloudEventBuilder<>();
|
||||
CloudEventBuilder<T> result = new CloudEventBuilder<>(validator);
|
||||
|
||||
AttributesImpl attributes = base.getAttributes();
|
||||
|
||||
|
@ -194,9 +210,9 @@ public final class CloudEventBuilder<T> implements
|
|||
new CloudEventImpl<T>(attributes, data, extensions);
|
||||
|
||||
Set<ConstraintViolation<Object>> violations =
|
||||
getValidator().validate(cloudEvent);
|
||||
validator.validate(cloudEvent);
|
||||
|
||||
violations.addAll(getValidator().validate(cloudEvent.getAttributes()));
|
||||
violations.addAll(validator.validate(cloudEvent.getAttributes()));
|
||||
|
||||
final String errs =
|
||||
violations.stream()
|
||||
|
|
Loading…
Reference in New Issue