FieldConfigScope

Provides a DSL-style builder to configure fields in a configuration schema.

This class supports both method chaining and block-style configuration, allowing flexible and type-safe field definitions.

Method Chaining Style

def.field(AppConfig::port)
.default(8080)
.range(1..65535)
.validate { require(it > 0) { "Port must be positive" } }

Block Style

def.field(AppConfig::apiKey) {
required()
validate { require(it.isNotBlank()) { "API key cannot be blank" } }
}

Mixed Style

Both styles can be combined as needed:

def.field(AppConfig::timeout)
.default(30)
.range(1L..300L)

Parameters

V

the type of the value associated with this field

Constructors

Link copied to clipboard
constructor()

Functions

Link copied to clipboard
fun default(value: V): FieldConfigScope<V>

Sets the default value for the configuration field.

Link copied to clipboard
fun ifNull(handler: (Any?, Any?) -> Unit): FieldConfigScope<V>

Sets a handler to be invoked when the configuration field is null or missing.

Link copied to clipboard

Sets a custom loader for the configuration field.

Link copied to clipboard
fun name(yamlName: String): FieldConfigScope<V>

Specifies the YAML/JSON key name for this field if it differs from the property name.

Link copied to clipboard
@JvmName(name = "notBlank")
fun notBlank(): FieldConfigScope<V>

Validates that a String field is not blank (not empty and not just whitespace).

Link copied to clipboard

Sets a range validator for the configuration field using an IntRange.

Sets a range validator for the configuration field using a LongRange.

fun range(min: Long = Long.MIN_VALUE, max: Long = Long.MAX_VALUE): FieldConfigScope<V>

Sets a range validator for the configuration field using explicit min/max values.

Link copied to clipboard
fun required(soft: Boolean = false): FieldConfigScope<V>

Marks the field as required or soft-required during configuration validation.

Link copied to clipboard

Shortcut method to mark a field as required and provide a default value.

Link copied to clipboard
fun validate(block: (V) -> Unit): FieldConfigScope<V>

Adds a custom validator for the configuration field.

Link copied to clipboard
fun validators(vararg blocks: (V) -> Unit): FieldConfigScope<V>

Adds multiple validators at once.