Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
fbessou opened this issue Jun 17, 2024 · 0 comments

Comments

@fbessou
Copy link

fbessou commented Jun 17, 2024

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. :)

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

No branches or pull requests

1 participant