mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 10:38:19 +08:00
feat(validator): Support validators for config values.
BREAKING CHANGE: `ValueManifest` and `ConfigValue` added a new type "UNIT" to mark the minimal unit value of this instance. link #132
This commit is contained in:
-8
@@ -1,8 +0,0 @@
|
||||
package cc.carm.lib.configuration.validators;
|
||||
|
||||
public class StandardValidators {
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package cc.carm.lib.configuration.validators;
|
||||
|
||||
import cc.carm.lib.configuration.annotation.ValuePattern;
|
||||
import cc.carm.lib.configuration.annotation.ValueRange;
|
||||
import cc.carm.lib.configuration.function.ValueValidator;
|
||||
import cc.carm.lib.configuration.source.ConfigurationHolder;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class Validators {
|
||||
|
||||
public static void initialize(ConfigurationHolder<?> holder) {
|
||||
holder.initializer().registerValidAnnotation(ValueRange.class, r -> (ho, value) -> {
|
||||
if (!(value instanceof Number)) {
|
||||
throw new IllegalArgumentException("Value is not a number: " + value);
|
||||
}
|
||||
|
||||
Number number = (Number) value;
|
||||
if (number.doubleValue() < r.min() || number.doubleValue() > r.max()) {
|
||||
throw new IllegalArgumentException(r.message());
|
||||
}
|
||||
});
|
||||
|
||||
holder.initializer().registerValidAnnotation(ValuePattern.class, r -> new ValueValidator<Object>() {
|
||||
private final Pattern pattern = Pattern.compile(r.value());
|
||||
|
||||
@Override
|
||||
public void validate(@NotNull ConfigurationHolder<?> holder, @Nullable Object value) throws Exception {
|
||||
if (value == null) return;
|
||||
if (!pattern.matcher(value.toString()).matches()) {
|
||||
throw new IllegalArgumentException(r.message());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user