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
WIP: Fluent Validation Plugin #97
Conversation
Codecov Report
@@ Coverage Diff @@
## master #97 +/- ##
=======================================
Coverage 90.83% 90.83%
=======================================
Files 1 1
Lines 480 480
Branches 132 132
=======================================
Hits 436 436
Misses 44 44 Continue to review full report at Codecov.
|
i made a attempt at extracting the fluent'ness but ran into some typing issues.. though it may be possible with right magic. also realizing as i go that my scope is increasing a bit (eg: marking things as required) which start to feel more like a form plugin that pure validation. new api addition: Validation(state.properties).conditions.when(({ propertyId }, properties) => {
if (!propertyId) {
return false;
}
const property = properties.find(p => p.id === propertyId);
return property.fieldType === FieldType.SELECT;
}).valueId.required(); the |
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 will try to put in code the other idea, which does not require a proxy. We may come up with 2 new validation plugins :) The one old can remain as deprecated. But give me some time for it.
in retrospect, i don't remember why i actually needed to downgrade anymore.. any validation callbacks should be listening to the state via get() incase of changes... took it out and still works fine |
"any validation callbacks should be listening to the state via get() incase of changes" - exactly - validation functions should read the state and leave traces of what they touched, so hookstate can detect when and what should be rerendered. |
readonly severity: ValidationSeverity; | ||
interface NestedState { | ||
state: State<any>; | ||
parent?: NestedState; |
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.
this relationship may cause large parent object being hold in RAM...
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.
NestedState is transient within the scope of a valid call.. this exists because I ran into issues reusing the State (at root) and the re-render events firing off properly. potentially, this may not be needed the reverse tree walk can be eliminated in conditionalValid
, by just doing a lookup each time, but will need to benchmark the performance hit.
plugins/Validation/src/Validation.ts
Outdated
} | ||
|
||
const PluginID = Symbol('Validate'); | ||
type NestedValidator<Root, T> = T extends string ? SingleValidator<T> : |
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.
Why only string
? where are numbers, booleans, undefined and null?
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.
why undefined/null?
plugins/Validation/src/Validation.ts
Outdated
}; | ||
} | ||
|
||
export function ValidationAttach<T>(state: State<T>, config: ((validator: NestedValidator<T, T>) => void)) { |
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.
this is getting close to what I was thinking about. It would be still possible to achieve the same without proxy, but maybe it is the other story.
As we discussed, you need to move your code to the Validation2 plugin, because it breaks compatibility with the previous one.
Also when you are ready. Add the plugin to docs and a code sample like for other plugins.
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.
yeah, i'll create a new PR once i'm closer to final state with a new plugin folder.
w/r/t object config vs proxy... take a look at the example validation i have in PR description, in particular the conditional behavior. i feel like trying to replicate that as a POJO would be incredibly verbose.
at least this latest version, the cost of the proxy is only in one place (on attach), so don't imagine there is perf loss going a different route.
Potential implementation direction as a solution to #94 and maybe introduce a new Fluent API abstraction.
Problem:
Proposed API syntax: