Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Validating atoms #1198

Closed
BenBeattieHood opened this issue Jun 1, 2022 · 7 comments · Fixed by #2336
Closed

Validating atoms #1198

BenBeattieHood opened this issue Jun 1, 2022 · 7 comments · Fixed by #2336
Assignees
Labels
enhancement New feature or request

Comments

@BenBeattieHood
Copy link

BenBeattieHood commented Jun 1, 2022

Thanks for your superb work on this package! I'm enjoying using it.

Where an atom reads from an API (local storage, react query response, form input) rather than an in-memory value, we can't guarantee the value matches the expected type. 

An example of this can be seen here: https://d5kn6.csb.app/#count=%22stored+value+may+not+match+your+expected+type%2C+if+it+has+been+written+by+previously+deployed+code%22

(Pressing the +1 button on the above example shows how the business logic will fail with an invalid input type)

The current implementation of atomWithValidate is written to apply validation on write, with a synchronous initialValue; but could we instead have validation nested into atoms (eg atomWithValidate's initialValue is an atom, therefore requiring onMount to provide a getter as well as a setter), and in this way perhaps compose the validation atom so that API responses could be validated during load?

@dai-shi
Copy link
Member

dai-shi commented Jun 1, 2022

Thanks for opening this up.

In terms of atomWithStorage, I have thought about validation/type guard and custom storage is a solution. While I agree that docs are missing, so far nobody seems interested. #805 (comment)
So, let's improve docs on this.

atomWithHash is a sub util based on atomWithStorage, and deserialize option is the place to validate.

I'm not sure about jotai/query. I assume react-query should be responsible about it?

For forms, we have an experimental project jotai-form, FYI.

@BenBeattieHood
Copy link
Author

Thanks @dai-shi 👍I'll try out the custom storage solution, as well as adding react-query validation.

As to jotai-form, I was hoping its atomWithValidate could be adjusted here to accept either initialValue OR another atom, similar to how focusAtom or atom.ts does it

@dai-shi
Copy link
Member

dai-shi commented Jun 2, 2022

jotaijs/jotai-form#4 is a new function related it. I don't think deriving atoms is very easy, and it may overlap with other native functions. Though, I' might be missing something. Would be great if you open a new issue there with some use cases.

@dai-shi
Copy link
Member

dai-shi commented Jun 2, 2022

I think what's remaining in this issue is documentation for atomWithStorage and atomWithHash. Not sure about jotai/query, but maybe.

@dai-shi dai-shi added the documentation Improvements or additions to documentation label Jun 2, 2022
@dai-shi
Copy link
Member

dai-shi commented Apr 10, 2023

Closing this as it's a bit stale now.

@dai-shi dai-shi closed this as completed Apr 10, 2023
@KutnerUri
Copy link

KutnerUri commented Jan 9, 2024

@dai-shi - this seems very necessary, there's no way to drop old values when changing data structure. This is storage agnostic.
We could also change the key every time the structure changes, but I think it's safer to re-validate when deserializing.

Should be something like:

export const atom = atomWithStorage("cat", ["meow"], undefined, {
  getOnInit: true,
  // return false to skip storage and use default value
  validateStorage: value => Array.isArray(value)
});

@dai-shi
Copy link
Member

dai-shi commented Jan 10, 2024

This is storage agnostic.

Yeah, that's convincing. But, I'm not convinced to add an option. Let's add a util, which is less comfortable, but more flexible.

@dai-shi dai-shi reopened this Jan 10, 2024
@dai-shi dai-shi added enhancement New feature or request and removed documentation Improvements or additions to documentation labels Jan 10, 2024
@dai-shi dai-shi self-assigned this Jan 10, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants