Skip to content
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

Don't lose properties when updating a key #712

Merged
merged 1 commit into from
Jun 23, 2022
Merged

Don't lose properties when updating a key #712

merged 1 commit into from
Jun 23, 2022

Conversation

dmcgillen
Copy link
Contributor

@dmcgillen dmcgillen commented Jun 20, 2022

Fixes #645

Updating a key in a schema that had {:optional true} (or any properties) would lose those properties, and a subsequent attempt to validate against that schema without that key would fail. It appears to me (not looked at this codebase before) that this was simply because the new child was being calculated before calculating the properties.

One thing I admittedly don't understand is that even before my fix the output to update looked ok:

(malli.util/update
  [:map
   [:a {:optional true} int?]
   [:b {:optional true} string?]]
  :a
  identity)
;; => [:map [:a {:optional true} int?] [:b {:optional true} string?]]

This is why the existing tests pass, because they just check this output (I've added a test to run validate).

But if you, say, called malli.util/to-map-syntax on it, you could see the properties had been lost:

{:type :map, :children [[:a nil {:type int?}] [:b {:optional true} {:type string?}]]}

With this PR that is no longer the case.

(let [c [k p s]
p (if i (if override p (nth (children i) 1)) p)
(let [p (if i (if override p (nth (children i) 1)) p)
c [k p s]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No change here, just switched the two lines around

@ikitommi
Copy link
Member

A really good catch! thanks!

@ikitommi ikitommi merged commit c78c86c into metosin:master Jun 23, 2022
@dmcgillen dmcgillen deleted the dont-lose-optional-property-when-updating-key branch June 23, 2022 08:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

When using mu/update-in on a conditional key, it shows itself as optional but it is actually mandatory
2 participants