modular-forms icon indicating copy to clipboard operation
modular-forms copied to clipboard

type: Allow insert null value to FieldArray

Open Huliiiiii opened this issue 1 year ago • 5 comments

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

Huliiiiii avatar Sep 03 '24 06:09 Huliiiiii

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

fabian-hiller avatar Sep 03 '24 08:09 fabian-hiller

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,
  })
)

Huliiiiii avatar Sep 03 '24 09:09 Huliiiiii

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 avatar Sep 04 '24 08:09 fabian-hiller

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.

fabian-hiller avatar Sep 06 '24 19:09 fabian-hiller

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.

Huliiiiii avatar Sep 07 '24 10:09 Huliiiiii