build<T> method

Future<T> build<T>(
  1. T bind(
    1. Map<String, Object?> values
    ), {
  2. void validate(
    1. T value
    )?,
})

Loads and binds a model, then validates it before returning.

The binder receives a deeply immutable map. Models should also be immutable. Binder failures are redacted; custom ConfigValidationException messages are preserved. Other validator exceptions are redacted.

Implementation

Future<T> build<T>(
  T Function(Map<String, Object?> values) bind, {
  void Function(T value)? validate,
}) async {
  final values = await _load();
  final T model;
  try {
    model = bind(values);
  } catch (_) {
    throw const ConfigBindingException('Unable to bind configuration model.');
  }
  try {
    validate?.call(model);
  } on ConfigValidationException {
    rethrow;
  } catch (_) {
    throw const ConfigValidationException('Configuration validation failed.');
  }
  return model;
}