Skip to content

Consider using input schema instead of parsed result schema when computing FieldChainFromSchema type. #61

@fbessou

Description

@fbessou

Context

When working with a schema containing transforms, it happens that the raw values types in the form don't match the schema.parse return type. This is the case in the checkboxes example documented in the readme where the type of the "input" is undefined|string and the resulting value is of type boolean. This also happen when working with text inputs storing arrays of values for example:

<input type="text" value="value1,value2"/>

Problem

With the following schema:

const FormSchema = z.object({
    myarray: z.string().transform(strValues => strValues.split(',')).pipe(z.array(z.string()))
})

The type of zo.fields.myarray is generated from the parse output type (string[] in that case). Which leads to the compiler not allowing to call zo.fields.myarray without an index argument:

<input type="text" name={zo.fields.myarray()/*Error: An argument for 'index' was not provided*/}value="value1,value2"/>

Solution

Changing this line so that the FieldChain is computed from the parse input instead of the parse result would fix this.

export declare type FieldChainFromSchema<T extends GenericSchema> = FieldChain<DeepNonNullable<z.input<T>>>;

The change should probably also be applied to the ErrorChainFromSchema type.

I can make a PR if you want. :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions