Comments (11)
For anyone using malli I found a really nice way to integrate helix with malli to get function instrumentation of helix components:
metosin/malli#899
I'm using this currently in my fork of malli. If that PR isn't merged for whatever reason you can also just copy the -map-schema
implementation from malli.core
and add that :xf
option to your own registry.
from helix.
@lilactown yes, it's a great idea! I did something like this. I created defnc
macro based on helix/defnc
. I'm able to pass clojure.spec
to this macro for validation component props and render warning in browser console or generate exception during the tests if props is not valid.
For example:
(s/def ::class-name string?)
(s/def ::size #{300 200})
(s/def ::children ::s.common/children)
(s/def ::props
(s/keys
:req-un [::children]
:opt-un [::class-name ::size]))
(defnc <Badge>
[{:keys [class-name size children]}]
{:spec ::spec/props}
"badge")
In browser console I can see the following messages https://nimb.ws/4Pw3KR
Also in production build I remove spec checks for components.
from helix.
To make a (hopefully small) request -- I'd love to ask that you make spec validation pluggable (or at least overrideable) if you add an internal implementation?
I use malli instead of spec and would love to be able to swap out the spec validator for malli's explainer instead!
from helix.
I've been using the delegate pattern where defnc
calls out to a function:
https://github.com/matterandvoid-space/todomvc-fulcro-subscriptions/blob/766d27be316c3f2ab6a23bd8db30932ec0601a4f/src/main/space/matterandvoid/todomvc/todo/ui.cljs#L13
so that I can use malli instrumentation similar to react's propTypes
@khmelevskii I'm curious - does your macro emit another function for the body so that the specs receive cljs types instead a js object?
from helix.
@dvingo Very nice work... is there somewhere we can discuss this further? I do not quite understand the need to emit an function after the first def. Would be nice to briefly explain in a comment.
I suppose you can comment on the gist?
The goal is to support using [:map [:key1 :string] [:key2 :int]]
etc malli syntax to describe helix props and get malli instrumentation to work with that. The issue is that if you attempt to instrument a plain helix defnc
directly (which emits a function, so can be instrumented) the arguments to that function are a JS object, which will not work with the instrumentation - malli expects a hashmap or something that implements its protocols (cljs bean). The custom macro I wrote will instrument a nested function so that as a user you can go on pretending you're getting a hashmap to your helix component and malli can correctly validate the arguments. Does that make sense? - feel free to leave comments on the gist.
from helix.
@SevereOverfl0w brought up that it would be easy enough to have helix.spec
simply expose a props
spec combinator that could take a predicate / spec / spec-identifier, a la:
(s/fdef greeting
:args (s/cat :props (hs/props map?)))
That seems pretty minimal. Additional helpers could be added on to that if valuable.
from helix.
If you go for internal syntax, I like to suggest using the same from Guardrails/Ghostwheel.
from helix.
I thought y'all might be interested in this in the interim.
Personally, also awaiting a convenient way to swap out arbitrary "type checking systems" at will between spec, malli
/schema
or React.PropTypes
or whatever. The delegation pattern is quite interesting I have to say!
from helix.
I think that discussion of this can continue in helix-spec-alpha that @riotrah
posted above. Closing this for now!
from helix.
The above idea for getting malli instrumentation support didn't work out. Instead I found a solution by changing the defnc
implementation to emit two functions - the inner function will be instrumented correctly by a normal :map
schema:
https://gist.github.com/dvingo/97bae8b33c08b257153946bb82f38a86
from helix.
@dvingo Very nice work... is there somewhere we can discuss this further? I do not quite understand the need to emit an function after the first def. Would be nice to briefly explain in a comment.
from helix.
Related Issues (20)
- Infinite loop with IStateUpdater HOT 1
- "Provider" property access getting mangled by :optimizations :advanced HOT 5
- Remove es6 mention from readme HOT 1
- Update README to use React 18 createRoot api
- No dependency named react-dom/client HOT 3
- Confusing syntax with `wrap` and args destructuring HOT 1
- goog.object/extend is deprecated
- Setup cljstyle config, run in CI
- Any good way to make multimethods work as component functions? HOT 8
- gensym / anonymous function syntax breaks fast refresh HOT 2
- Include resources in deps.edn :paths
- $ clj-kondo hook fails to lint & right when component is not a symbol
- The $ macro and new JSX transform HOT 14
- `wrap-fx` is public HOT 1
- Wrappers for useTransition, useDeferredValue, etc. HOT 1
- Is it possible to bypass the camel-casing of properties? HOT 7
- Add wrapper around createRef with atom protocols HOT 2
- How to use with uikit? HOT 2
- Running tests in nodejs with jsdom HOT 4
- Companies using Helix HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from helix.