createValidatedBuilder()
createValidatedBuilder<
State>(initial): ValidatedBuilderDefinition<State,object>
Creates a builder with validation on build().
Same as createBuilder, but .done(validate) takes a validator.
build() returns Result<State, string>.
Type Parameters
State: State
The product type being built
Parameters
initial: State
The initial state
Returns: ValidatedBuilderDefinition<State, object>
A validated builder definition
Since
2.4.0
Example
const configBuilder = createValidatedBuilder({ host: "", port: 0 })
.step("host", (s, host: string) => ({ ...s, host }))
.step("port", (s, port: number) => ({ ...s, port }))
.done((s) => s.host ? true : "host is required");
configBuilder().port(8080).build(); // Err("host is required")
configBuilder().host("localhost").build(); // Ok({ host: "localhost", port: 0 })
ValidatedBuilderDefinition<State, Methods>
Validated builder definition that accumulates steps.
Since
2.4.0
Type Parameters
State: State
The product state being built
Methods: Methods extends object
Accumulated method signatures
Methods
step()
Adds a step to the builder.
Type Parameters
K: K extends string
Arg: Arg
name:
K
fn:(state, arg) => State
Returns:ValidatedBuilderDefinition<State, Methods & { [P in string]: (arg: Arg) => unknown }>
done()
Finalizes with a validator. build() returns Result<State, string>.
validate:
(state) => string | true— Returnstrueif valid, or an error message string
Returns:> (): ValidatedFinalBuilder<State, Methods>
Returns
ValidatedFinalBuilder<State, Methods>
ValidatedFinalBuilder<State, Methods>
ValidatedFinalBuilder<
State,Methods> ={ [K in keyof Methods]: Methods[K] extends (arg: infer Arg) => unknown ? (arg: Arg) => ValidatedFinalBuilder<State, Methods> : never }&object
Builder with validated build() returning Result.
Type Declaration
build(): () => Result<State, string>
Returns Ok(state) if valid, Err(message) otherwise.
Returns: Result<State, string>
current(): () => State
Returns the current state without validation.
Returns: State
Type Parameters
State: State
The product state being built
Methods: Methods extends object
The step method signatures
Since
2.4.0