-
-
Notifications
You must be signed in to change notification settings - Fork 26
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
Setting guardrails registry as default malli registry doesn't work #43
Comments
Guardrails registry uses malli registry so this is possible:
what is the use case to inject those guardrails schemas back to malli registry? |
Right, now you are talking about the other way around: yes, the way it's set up is that Guardrails uses registry that automatically sees everything from Malli default registry. But I am talking about the common use case, that is, the person is using Guardrails primarily and doesn't touch Malli directly unless needed. So the person is using Guardrails But then you try to use Now, I've solved this by making a new utility function:
So I guess this is fixed, I just have to use and curate a set of functions like this. |
I guess my issue is using malli.util/merge and malli.util/optional-keys with Guardrails. Besides the above mentioned problem with registries, I've now ran into another problem: when guardrails is disabled, the |
@ikitommi
I like using that var as a value in other schemas etc... and this kinda breaks with Guardrails model since these always get compiled whether it's enabled or not, and they will try to resolve their dependants when used with |
I'd be interested in discussing your experiences further. The aim is to make it so that the GR stuff can be added to a project without affecting the default registry (breaking existing programs), but that you can use what's in the default registry in GR. Do you have any specific proposals for change that meet all of the current design goals and won't break existing code? |
Your goal to have GR be drop-in for existing projects using Malli has obstacles:
These functions are very useful but they will eagerly evaluate schemas, and try to resolve them from default registry, which will throw exceptions for schemas defined by The ones I use a lot are:
This change is trivial to do in the codebase, but it is a change.
This was subject of another issue I opened. It is common, and suggested by Malli documentation, that you define chunks of schemas as vars or perhaps functions that generate them. It's all just data anyway, right? See The issue there is again that some of those use
This will error out with GR disabled, work with GR enabled. So here the user has a couple of options:
So you see it's not as easy as just changing
|
So, you can use GR's config ns to read if GR is enabled at compile time. This has been the biggest annoyance in my own use of Malli and GR as well. I've been hitting cases where I've had to copy code out of Malli in order to get it to even let me say which registry to use. I have sort of left this as "an exercise for the reader" because I myself don't see me being able to provide a one-size-fits-all solution to the problems. Perhaps the best solution I can provide is some kind of config that lets you use the default registry instead of the one in GR? |
Hm if I tell GR to use the default registry, how will GR add schemas to the registry? I don't think there's an API for that, the registry itself is opaque and the person who sets the default registry to be some sort of mutable registry, also has to know the mechanism how to add to it. I don't think there's an abstract way to do it. GR cannot add schemas to the default registry with no regard for the implementation type. That's why my first idea was to make GR registry the default registry (since GR knows how to add entries to it). But that got prevented by GR logic of its registry delegating (also) to the default registry, which caused a loop in that case (GR registry was delegating to itself, causing a stack overflow). This mechanism also prevents the default registry being any sort of compound registry where one subregistry is the GR one, you'll get a loop again. You've stated that your main goal here is to have it be usable as a dropin without breaking apps. The main obstacle, as explained above, is that in Malli chunks of schemas are often declared/created as just a datastructure without any registering. See examples above, having a Anyway I've described different possible approaches. Rolling my own macros is not very appealing because they'll be missing from the noop namespace. |
The reason for this is simple, guardrails registry refers to default registry and when it becomes default registry, that's a loop.
Why would one want to set guardrails as default registry? For consistency. Using
>def
orregister!
fills up the guardrails registry and if you want to use those schemas in non-guardrails code you'll want to use that registry. E.g.malli.util/optional-keys
. Of course you can always pick registry on per-call/per-schema basis, so that fixes this, but it's kinda annoying.The text was updated successfully, but these errors were encountered: