forge42dev / remix-hook-form

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

null / undefined handling #105

Open TheYoxy opened 1 month ago

TheYoxy commented 1 month ago

As is, when you create a form data with a value that has been defined with a value, even values that represents nothing (null and undefined), it gets serialized in string.

Wouldn't it be better to ignore theses values or return the raw underlined type ?

One idea of test implementation in the case that the choice is to handle base type:

  describe('should handle undefined', () => {
    it('with stringify all', () => {
      const data = {
        undefined: undefined,
      };
      const formData = createFormData(data, true);
      expect(formData.get('undefined')).toEqual(undefined);
    });
    it('with stringify all', () => {
      const data = {
        undefined: undefined,
      };
      const formData = createFormData(data, false);
      expect(formData.get('undefined')).toEqual(undefined);
    });
  });
  describe('should handle null', () => {
    it('with stringify all', () => {
      const data = {
        null: null,
      };
      const formData = createFormData(data, true);
      expect(formData.get('null')).toEqual(null);
    });
    it('with stringify all', () => {
      const data = {
        null: null,
      };
      const formData = createFormData(data, false);
      expect(formData.get('null')).toEqual(null);
    });
  });
theskillwithin commented 2 days ago

+1

theskillwithin commented 3 hours ago

@TheYoxy actually I don't think this can be done in createFormData because this is using FormData

https://developer.mozilla.org/en-US/docs/Web/API/FormData/append#value

What is really desired here is something that conform does with parseWithZod

https://conform.guide/integration/remix