Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
Enforce alphabetic ISO-4217 currency format validation
Browse files Browse the repository at this point in the history
  • Loading branch information
1u0 committed Jul 31, 2023
1 parent 0cd8ec7 commit 23c2ee8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
package org.zalando.nakadi.validation;

import org.everit.json.schema.FormatValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Currency;
import java.util.Optional;

public class ISO4217CurrencyCodeValidator implements FormatValidator {
private static final Logger LOG = LoggerFactory.getLogger(ISO4217CurrencyCodeValidator.class);
private final String eventTypeName;
private final String formatName;

ISO4217CurrencyCodeValidator(final String eventTypeName) {
this(eventTypeName, "iso-4217");
ISO4217CurrencyCodeValidator() {
this("iso-4217");
}

ISO4217CurrencyCodeValidator(final String eventTypeName, final String formatName) {
this.eventTypeName = eventTypeName;
ISO4217CurrencyCodeValidator(final String formatName) {
this.formatName = formatName;
}

Expand All @@ -26,9 +21,7 @@ public Optional<String> validate(final String value) {
try {
Currency.getInstance(value);
} catch (IllegalArgumentException ex) {
// Note: not returning this as validation error (yet), but just log it.
LOG.warn("Currency format violation (format_name={}): [event_type={}, value={}]",
formatName, eventTypeName, value);
return Optional.of(String.format("[%s] is not a valid alphabetic ISO 4217 currency code", value));
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.zalando.nakadi.utils.IsOptional;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;

public class ISO4217CurrencyCodeValidatorTest {

Expand Down Expand Up @@ -31,10 +32,13 @@ public void testValidator() {
"SEK",
};

final var validator = new ISO4217CurrencyCodeValidator("an-event-type");
final var validator = new ISO4217CurrencyCodeValidator();
for (final String value : invalidValues) {
// For invalid values, the validator should return empty result.
assertThat("Test: " + value, validator.validate(value), IsOptional.isAbsent());
assertThat(
"Test: " + value,
validator.validate(value),
IsOptional.matches(containsString(" is not a valid alphabetic ISO 4217 currency code"))
);
}

for (final String value : validValues) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public JsonSchemaValidator build(final EventType eventType) {
final Schema schema = SchemaLoader.builder()
.schemaJson(loader.effectiveSchema(eventType, jsonSchema.get().getSchema()))
.addFormatValidator(new RFC3339DateTimeValidator())
.addFormatValidator(new ISO4217CurrencyCodeValidator(eventType.getName()))
.addFormatValidator(new ISO4217CurrencyCodeValidator(eventType.getName(), "ISO-4217"))
.addFormatValidator(new ISO4217CurrencyCodeValidator())
.addFormatValidator(new ISO4217CurrencyCodeValidator("ISO-4217"))
.build()
.load()
.build();
Expand Down

0 comments on commit 23c2ee8

Please sign in to comment.