forge42dev / remix-hook-form

Open source wrapper for react-hook-form aimed at Remix.run
MIT License
323 stars 24 forks source link

validateFormData broke #43

Closed adaboese closed 10 months ago

adaboese commented 10 months ago
const FormDataZodSchema = z.object({
  action: z.string(),
  outline: z.string().min(2, {
    message: 'Outline must be at least 2 characters.',
  }),
});

type FormData = z.infer<typeof FormDataZodSchema>;

const resolver = zodResolver(FormDataZodSchema);
export const action: ActionFunction = async ({ params, request }) => {
  const articleId = Number(params.id);

  if (!articleId) {
    throw new Error('Article ID is required.');
  }

  const formData = await request.formData();

  if (formData.get('action') === 'generate') {
    return json(await replaceOutline(articleId));
  }

  if (formData.get('action') === 'accept') {
    console.log('formData', Object.fromEntries(formData));

    const { data, errors } = await validateFormData<FormData>(
      formData,
      resolver,
    );

    console.log('errors', errors);

    if (errors) {
      return json(errors);
    }

    return await acceptOutline(articleId, data.outline);
  }

  throw new Error('Invalid action.');
};
formData {
  action: 'accept',
  outline: '* Introduction\n' +
    '  * Overview of Threats Affecting Polar Bears\n' +
    '  * Significance of the Circumpolar Action Plan\n' +
    '  * The Compound Effects of these Threats\n' +
    '* Climate Change As A Major Threat\n' +
    "  * Effects of Climate Change on Polar Bear's Habitat\n" +
    '  * Relation of Climate Change with Other Threats\n' +
    '* Human-Caused Mortality\n' +
    '  * Causes of Human-Induced Polar Bear Deaths\n' +
    '  * Connection with Other Threats and Climate Change\n' +
    '* Industrial Threats: Mineral and Energy Resource Exploration and Development\n' +
    "  * Impacts of Industrial Activities On Polar Bears' Habitats\n" +
    '  * Combination with Climate Change and Other Threats\n' +
    '* Contaminants and Pollution\n' +
    '  * effect of Pollution on Polar Bears\n' +
    '  * Connection with Climate Change and Other Threats\n' +
    '* Shipping and Tourism\n' +
    '  * Impacts of Shipping and Tourism on Polar Bear Habitat\n' +
    '  * Connection with Climate Change and Other Threats\n' +
    '* Diseases and Parasites\n' +
    '  * Effects of Diseases and Parasites on Polar Bears\n' +
    '  * connection with Climate Change and Other Threats\n' +
    '* Conclusion\n' +
    '  * Summary of Challenges and their Impacts\n' +
    '  * Importance of Protecting Polar Bears for Biodiversity\n'
}
errors {
  action: { message: 'Required', type: 'invalid_type', ref: undefined },
  outline: { message: 'Required', type: 'invalid_type', ref: undefined }
}

I believe I am providing the expected inputs, but validator is still producing an error.

AlemTuzlak commented 10 months ago

Can you try passing in a third param as true, I want to see if that will work

adaboese commented 10 months ago

Third parameter to which function? validateFormData only accept to parameters

adaboese commented 10 months ago

I tried but no effect.

adaboese commented 10 months ago

I am using version 3.0.0 just in case

AlemTuzlak commented 10 months ago

I will test first thing in the morning, can you try with ver 2.*

adaboese commented 10 months ago

v2 is breaking with other issues 🙈 but thank you!

I will just workaround for now

AlemTuzlak commented 10 months ago

@adaboese fixed, try using it now, you were originally supposed so pass in something like Object.fromEntries(formData) for it to work but now I've made it accept either the object or the formData instance so should work

adaboese commented 10 months ago

Thank you

On Sat, Oct 28, 2023 at 5:36 AM Alem Tuzlak @.***> wrote:

@adaboese https://github.com/adaboese fixed, try using it now, you were originally supposed so pass in something like Object.fromEntries(formData) for it to work but now I've made it accept either the object or the formData instance so should work

— Reply to this email directly, view it on GitHub https://github.com/Code-Forge-Net/remix-hook-form/issues/43#issuecomment-1783785687, or unsubscribe https://github.com/notifications/unsubscribe-auth/BDQ7U42QPDO6KAME6KLLWQDYBTU2XAVCNFSM6AAAAAA6TI5KVSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTOOBTG44DKNRYG4 . You are receiving this because you were mentioned.Message ID: @.***>