Skip to content

Commit

Permalink
Add validation to config
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinrobot committed Aug 10, 2023
1 parent 7f4d932 commit edd8087
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/main/java/net/vinrobot/mcemote/config/Option.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.vinrobot.mcemote.config.impl.OptionImpl;

import java.util.Objects;
import java.util.Optional;

public interface Option<T> {
Expand Down Expand Up @@ -71,4 +72,30 @@ default T get() {
* @return The default value of this option.
*/
T getDefault();

/**
* Validate the value of this option.
*
* @param value The value to validate.
* @throws ValidationFailedException If the validation fails.
*/
default void validate(final T value) throws ValidationFailedException {
Objects.requireNonNull(value);
}

/**
* Check if the value of this option is valid.
*
* @see #validate(T)
* @param value The value to check.
* @return True if the value is valid, false otherwise.
*/
default boolean isValid(final T value) {
try {
this.validate(value);
return true;
} catch (ValidationFailedException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.vinrobot.mcemote.config;

public class ValidationFailedException extends RuntimeException {
private static final long serialVersionUID = 1L;

public ValidationFailedException(final String message) {
super(message);
}

public ValidationFailedException(final String message, final Throwable cause) {
super(message, cause);
}

public ValidationFailedException(final Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public final class ConfigurationImpl implements Configuration {
private static final String SCRAPIE_TWITCH_ID = "40646018";

private final Option<String> twitchId = Option.of(SCRAPIE_TWITCH_ID);
private final Option<String> twitchId = new TwitchIdOptionImpl(SCRAPIE_TWITCH_ID);

@Override
public Option<String> twitchId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public OptionImpl(final T defaultValue) {

@Override
public Option<T> set(final Optional<T> value) {
value.ifPresent(this::validate);
this.value = Objects.requireNonNull(value);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package net.vinrobot.mcemote.config.impl;

import net.vinrobot.mcemote.config.ValidationFailedException;

public class TwitchIdOptionImpl extends OptionImpl<String> {
public TwitchIdOptionImpl(final String defaultValue) {
super(defaultValue);
}

@Override
public void validate(final String value) {
super.validate(value);

final int length = value.length();
if (length == 0 || 8 < length) {
throw new ValidationFailedException("Twitch ID must be between 1 and 8 digits");
}

try {
Long.parseLong(value);
} catch (final NumberFormatException e) {
throw new ValidationFailedException("Twitch ID must be a number");
}
}
}

0 comments on commit edd8087

Please sign in to comment.