fabian-hiller / modular-forms

The modular and type-safe form library for SolidJS, Qwik and Preact
https://modularforms.dev
MIT License
1.05k stars 55 forks source link

type: Allow insert null value to FieldArray #246

Open Huliiiiii opened 2 months ago

Huliiiiii commented 2 months ago

In my case, I need to insert a new entry without value into FieldArray, but the current type definition does not support this.

fabian-hiller commented 2 months ago

Can't you add an empty value (e.g. empty string)? Feel free to share your use case and code with me.

Huliiiiii commented 2 months ago

Can't you add an empty value (e.g. empty string)? Feel free to share your use case and code with me.

No, the schema is an object, and a key of object is string literal union types.

This is my schema:

v.array(
  v.object({
    language: // Union of language literals,
    localized_name: string,
  })
)
fabian-hiller commented 2 months ago

I understand and will check if we can allow null or undefined in such cases. In the meantime, you can change the input requirements of your schema to allow, for example undefined:

import * as v from 'valibot';

const LanguageSchema = v.picklist(['DE', 'FR', 'US']);

const LanguageArraySchema = v.array(
  v.object({
    language: v.pipe(v.optional(LanguageSchema), LanguageSchema),
    localized_name: v.string(),
  })
);
fabian-hiller commented 2 months ago

Unfortunately, I couldn't find a quick fix as I encountered some more complex TypeScript errors. I may try again when I have more time or consider it when rewriting the library at some point.

Huliiiiii commented 2 months ago

Unfortunately, I couldn't find a quick fix as I encountered some more complex TypeScript errors. I may try again when I have more time or consider it when rewriting the library at some point.

Thanks for your work. For now, changing the schema is a good workaround.