-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: ability to validate atoms , validateAtoms
#5
Conversation
@dai-shi there is a |
Types are fixed. You can't have a union type for getter. I'm not sure if it's possible to improve. |
will change the API to avoid adding a helper to reduce developer work for basic interaction
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
quick review
remove the un-needed types from the API change and also make the validator function consistent with atomWithValidate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure the explanation is enough, but let it go.
Do not merge even if it suffices, have to write tests for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
other than that, the code looks okay.
src/validateAtoms.ts
Outdated
|
||
export type Validator = <Values extends Record<string, unknown>>( | ||
values: Values, | ||
) => Promise<unknown>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this should be unknown | Promise<unknown>
or void | Promise<void>
. We support sync validator, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that totally depends on the loadable
API but I think the sync one should be working, i'll check in the evening
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
Though, there's an issue with using mixed typed atoms right now. The types for validator and labelled atoms needs to be fixed for that.
On it next.
will add sync validator function in types and adds a test trying it out with an async test library
will infer the atom's internal generic and use that to generate the type sent to the validator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
final review.
src/validateAtoms.ts
Outdated
const $getSourceAtomVals = (get: Getter) => { | ||
const values = Object.fromEntries( | ||
Object.entries(labeledAtoms).map(([k, v]) => { | ||
const atomValue = get(v); | ||
return [k, atomValue.value]; | ||
}), | ||
); | ||
return values as Record<Keys, inferGeneric<Vals>>; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thinks it's better to make this an atom.
const $getSourceAtomVals = (get: Getter) => { | |
const values = Object.fromEntries( | |
Object.entries(labeledAtoms).map(([k, v]) => { | |
const atomValue = get(v); | |
return [k, atomValue.value]; | |
}), | |
); | |
return values as Record<Keys, inferGeneric<Vals>>; | |
}; | |
const valsAtom = atom((get) => { | |
const values = Object.fromEntries( | |
Object.entries(labeledAtoms).map(([k, v]) => { | |
const atomValue = get(v); | |
return [k, atomValue.value]; | |
}), | |
); | |
return values as Record<Keys, inferGeneric<Vals>>; | |
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
too many listeners on the store?
cause it's being used in 2 places as a simple helper right now
const valsAtom = atom((get: Getter) => {
const values = Object.fromEntries(
Object.entries(labeledAtoms).map(([k, v]) => {
const atomValue = get(v);
return [k, atomValue.value];
}),
);
return values as Record<Keys, inferGeneric<Vals>>;
});
const baseAtom = atom(async (get) => {
// extract value from each atom and assign to the given key as label
return validator(get(valsAtom));
});
const normalizerAtom = atom((get) => {
const values = get(valsAtom);
const state = get(loadable(baseAtom));
return {
...state,
values,
};
});
const derv = atom((get) => {
const validatorState = get(normalizerAtom);
//.... other code
})
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
valsAtom
can be more efficient because it caches the result, than a simple helper.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense, pushed
src/validateAtoms.ts
Outdated
}); | ||
|
||
const derv = atom((get) => { | ||
const validatorState = get(normalizerAtom); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const validatorState = get(normalizerAtom); | |
const values = get(valsAtom); | |
const state = get(loadable(baseAtom)); | |
const validatorState = { | |
...state, | |
values, | |
}; |
Does this work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the test's say it's working,
that's nice, 1 listener reduced.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pushed with other simplifications
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Please update the PR description.
Feel free to merge it and ship it.
validateAtoms
Oh wow! I go away for a break, I come back and you've solved this 🤩 Love your work, thank you 👍 |
Adds the
validateAtoms
implementation for overall form atom validation.Usage example: