Skip to content

Add transformer concept #149

@ItsHarper

Description

@ItsHarper

Validators are great since they allow the library to handle re-prompts, but using them can lead to duplicating work. If I need to parse a free-form answer and have the library handle re-prompt when the input is invalid, I have to have the validator run the parser, and see if it succeeds. If it does, I'm not given the result, so I have to re-run the parser.

It would be great to have a Transformer type that looks something like this:

interface Transformer<Input, Transformed> {
    transform: (input: Input) => TransformationResponse<Transformed>;
}

interface ValidTransformationResponse<T> {
    isValid: true;
    transformed: T;
}

type TransformationResponse<T> = InvalidResponse | ValidTransformationResponse<T>;

That way, I can perform validation and parsing all in one step, while still letting the library handle re-prompts.

Another thing that's sub-optimal about the duplicate-parsing approach is that when you're re-parsing, if the possibility of errors is embedded in the type of the return value of the parsing function, I have to write code to handle that possibility to satisfy the type system, when I already know that parsing will succeed if the parser is deterministic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions