Allow providing an external validator
Signed-off-by: Ruben Romero Montes <rromerom@redhat.com>
This commit is contained in:
parent
b0a6101b23
commit
1cff239ab4
|
@ -42,8 +42,15 @@ 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;
|
||||
|
||||
public static final String SPEC_VERSION = "0.3";
|
||||
|
@ -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,54 +86,62 @@ 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, Validator validator) {
|
||||
Objects.requireNonNull(base);
|
||||
|
||||
CloudEventBuilder<T> result = new CloudEventBuilder<>();
|
||||
|
||||
|
||||
CloudEventBuilder<T> result = new CloudEventBuilder<>(validator);
|
||||
|
||||
AttributesImpl attributes = base.getAttributes();
|
||||
|
||||
|
||||
result
|
||||
.withId(attributes.getId())
|
||||
.withSource(attributes.getSource())
|
||||
.withType(attributes.getType());
|
||||
|
||||
|
||||
attributes.getTime().ifPresent(time -> {
|
||||
result.withTime(time);
|
||||
});
|
||||
|
||||
|
||||
attributes.getSchemaurl().ifPresent((schema) -> {
|
||||
result.withSchemaurl(schema);
|
||||
});
|
||||
|
||||
|
||||
attributes.getDatacontenttype().ifPresent(dc -> {
|
||||
result.withDatacontenttype(dc);
|
||||
});
|
||||
|
||||
|
||||
attributes.getDatacontentencoding().ifPresent(dce -> {
|
||||
result.withDatacontentencoding(dce);
|
||||
});
|
||||
|
||||
|
||||
attributes.getSubject().ifPresent(subject -> {
|
||||
result.withSubject(subject);
|
||||
});
|
||||
|
||||
|
||||
Accessor.extensionsOf(base)
|
||||
.forEach(extension -> {
|
||||
result.withExtension(extension);
|
||||
});
|
||||
|
||||
|
||||
base.getData().ifPresent(data -> {
|
||||
result.withData(data);
|
||||
});
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Build an event from data and attributes
|
||||
* @param <T> the type of 'data'
|
||||
|
@ -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