marmelab / react-admin

A frontend Framework for single-page applications on top of REST/GraphQL APIs, using TypeScript, React and Material Design
http://marmelab.com/react-admin
MIT License
24.89k stars 5.24k forks source link

Empty text fields and "" vs null #8537

Closed hiaselhans closed 1 year ago

hiaselhans commented 1 year ago

What you were expecting:

I have a couple of forms with textfields.

An example openapi spec would be:

Person:
  required:
    - name
  type: object
  properties:
    name:
      type: string
   nickname:
      type: string

Here is what i would expect:

create:

update

What happened instead:

When "nickname" was empty, in the request object "nickname" was set to null. this is documented behavior: https://github.com/marmelab/react-admin/blob/master/docs/SimpleForm.md#sanitizeemptyvalues and it is stated that this is expected by most backends. https://github.com/marmelab/react-admin/blob/032e1464cd23cf15dbc4d641e9663f74b37b9563/packages/ra-core/src/form/useInput.ts#L23-L24

From my understanding, this is how an optional text-field would be handled in a backend

So it might make sense to remove the value from the object when the state has not changed, but to make it null when it is empty feels a bit counter-intuitive to me. In pydantic for example this would be an error:

image

Also this reads to me as it would be preferable to use an empty string: https://stackoverflow.com/questions/45575493/what-does-required-in-openapi-really-mean

This seems to be a choice made by react-admin dev's and that*s okay, but I would like to get your motivation on this and maybe you can point me to the backends that implemented this in that particular way?

I will also send a pull-request to update the docs with an example if that's okay.

related issues/prs

https://github.com/marmelab/react-admin/pull/8188 https://github.com/marmelab/react-admin/pull/8262

https://github.com/marmelab/react-admin/issues/8472

Environment

djhi commented 1 year ago

Hi, this article should answer your questions: https://marmelab.com/blog/2022/12/08/react-admin-december-2022-update.html#better-support-for-empty-values-in-inputs