Skip to content

Commit

Permalink
Separate mapping for validation
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Apr 26, 2024
1 parent 5e230ab commit e264d30
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,139 @@
package io.quarkus.it.smallrye.config;

import java.time.Duration;
import java.time.Period;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;

import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Size;

import io.quarkus.arc.Unremovable;
import io.quarkus.runtime.annotations.RegisterForReflection;
import io.quarkus.runtime.configuration.DurationConverter;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithConverter;
import io.smallrye.config.WithDefault;
import io.smallrye.config.WithName;
import io.smallrye.config.WithParentName;

@Unremovable
@ConfigMapping(prefix = "cloud")
@Prod
public interface Cloud {
String host();

@Min(8000)
int port();

@WithConverter(DurationConverter.class)
Duration timeout();

@WithName("io-threads")
int threads();

@WithParentName
Server server();
Map<String, Form> form();

Optional<Ssl> ssl();

Optional<Proxy> proxy();

Optional<Cors> cors();

Log log();

Info info();

interface Form {
String loginPage();

String errorPage();

String landingPage();

Optional<String> cookie();

@WithDefault("1")
List<Integer> positions();
}

interface Ssl {
int port();

String certificate();

@WithDefault("TLSv1.3,TLSv1.2")
List<String> protocols();
}

interface Proxy {
boolean enable();

@Max(10)
int timeout();
}

interface Log {
@WithDefault("false")
boolean enabled();

@WithDefault(".log")
String suffix();

@WithDefault("true")
boolean rotate();

@WithDefault("COMMON")
Pattern pattern();

Period period();

@Max(15)
int days();

@RegisterForReflection
enum Pattern {
COMMON,
SHORT,
COMBINED,
LONG;
}
}

interface Cors {
List<Origin> origins();

List<@Size(min = 2) String> methods();

@RegisterForReflection
interface Origin {
@Size(min = 5)
String host();

@Min(8000)
int port();
}
}

interface Info {
Optional<@Size(max = 3) String> name();

@Max(3)
OptionalInt code();

Optional<List<@Size(max = 3) String>> alias();

Map<String, List<Admin>> admins();

Map<String, List<@Size(min = 8, max = 15) String>> firewall();

interface Admin {
@Size(max = 4)
String username();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
class Validator implements ConstraintValidator<Prod, Cloud> {
@Override
public boolean isValid(final Cloud value, final ConstraintValidatorContext context) {
return value.server().equals("prod");
return value.host().equals("prod");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
import java.util.Optional;
import java.util.OptionalInt;

import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Size;

import org.eclipse.microprofile.config.spi.Converter;

import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -31,7 +27,6 @@ public interface Server extends Alias {
String host();

@JsonProperty
@Min(8000)
int port();

@JsonProperty
Expand Down Expand Up @@ -89,7 +84,6 @@ interface Proxy {
boolean enable();

@JsonProperty
@Max(10)
int timeout();
}

Expand All @@ -115,7 +109,6 @@ interface Log {
Period period();

@JsonProperty
@Max(15)
int days();

@RegisterForReflection
Expand All @@ -133,42 +126,38 @@ interface Cors {
List<Origin> origins();

@JsonProperty
List<@Size(min = 2) String> methods();
List<String> methods();

@RegisterForReflection
interface Origin {
@JsonProperty
@Size(min = 5)
String host();

@JsonProperty
@Min(8000)
int port();
}
}

@RegisterForReflection
interface Info {
@JsonProperty
Optional<@Size(max = 3) String> name();
Optional<String> name();

@JsonProperty
@Max(3)
OptionalInt code();

@JsonProperty
Optional<List<@Size(max = 3) String>> alias();
Optional<List<String>> alias();

@JsonProperty
Map<String, List<Admin>> admins();

@JsonProperty
Map<String, List<@Size(min = 8, max = 15) String>> firewall();
Map<String, List<String>> firewall();

@RegisterForReflection
interface Admin {
@JsonProperty
@Size(max = 4)
String username();
}
}
Expand Down

0 comments on commit e264d30

Please sign in to comment.