Skip to content

feat: add a checkRequired builder helper for uniform required-field validation#91

Merged
OmarAlJarrah merged 2 commits into
mainfrom
feat/builder-checkrequired
Jun 16, 2026
Merged

feat: add a checkRequired builder helper for uniform required-field validation#91
OmarAlJarrah merged 2 commits into
mainfrom
feat/builder-checkrequired

Conversation

@OmarAlJarrah

@OmarAlJarrah OmarAlJarrah commented Jun 15, 2026

Copy link
Copy Markdown
Member

Summary

Builders hand-rolled required-field validation with inconsistent messages (e.g. "Method is required." vs "request is required"). This adds a single helper:

public fun <T : Any> checkRequired(name: String, value: T?): T

It returns the value when present and throws IllegalStateException("<name> is required") when null. The non-null guarantee comes from the return type, so call sites assign the result directly. The Request and Response builders are wired onto it as the reference usage, standardizing their messages. New tests cover the present and missing cases; the helper is added to the API snapshot.

Closes #48

SDK builders validated their mandatory fields with hand-rolled
`checkNotNull(field) { "..." }` calls, and the messages had drifted:
the Request builder threw "Method is required." / "URL is required."
(capitalized, trailing period) while the Response builder threw
"request is required" / "protocol is required" / "status is required"
(lowercase, no period). Callers that key off these messages, and the
builders codegen will eventually emit, had no single contract to rely
on.

Add `checkRequired(name, value)` in the `generics` package, alongside
the `Builder` interface. It returns the value as a non-null reference
when present (so it drops straight into builder constructor calls) and
throws IllegalStateException with the uniform message "<name> is
required" when absent. A Kotlin contract lets the compiler smart-cast
the result to non-null.

Wire it into the Request and Response builders as the reference usage,
standardizing both on the lowercase field-name form.

This is a public API addition (BuilderChecksKt.checkRequired); the
sdk-core .api snapshot is regenerated accordingly.
checkNotNull already declares returns() implies (value != null) and
checkRequired returns a non-null T by signature, so the explicit contract only
added smart-casting of the argument variable after the call. Neither builder
call site relies on that (both consume the return value directly), so remove
the contract and its ExperimentalContracts opt-in and collapse to a single
expression.
@OmarAlJarrah OmarAlJarrah merged commit b9e4402 into main Jun 16, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a checkRequired(name, value) builder helper

1 participant