Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

:reason doesn't compose as expected #171

Closed
mping opened this issue Feb 26, 2019 · 3 comments · Fixed by #267
Closed

:reason doesn't compose as expected #171

mping opened this issue Feb 26, 2019 · 3 comments · Fixed by #267

Comments

@mping
Copy link

mping commented Feb 26, 2019

Please correct me if I'm wrong. I have the following:

(s/def ::k1 (st/spec {:spec string? :reason "k1"}))
(s/def ::k2 (st/spec {:spec string? :reason "k2"}))
(s/def ::spec (s/keys :req-un [::k1 ::k2]))
(s/def ::spec-with-reason {:reason "missingKey" :spec (s/keys :req-un [::k1 ::k2])})


(s/def ::spec (st/spec {:spec (s/keys :req-un [::k1 ::k2])}))
;; 1 - failed: k1 in: [:k1] at: [:k1] spec: :user/k1

(st/explain ::spec-with-reason {:k2 "2" :k1 1})
;; {:k2 "2", :k1 1} - failed: {:reason "missingKey", :spec (s/keys :req-un [:user/k1 :user/k2])} spec: :user/spec-with-reason

Shouldn't validation of spec-with-reason fail with "k1" instead?

@mping mping changed the title :reason doesn't compose as expected :reason doesn't compose as expected Feb 26, 2019
@ikitommi
Copy link
Member

I would expect that too.

@wandersoncferreira
Copy link
Contributor

In the ::spec-with-reason example is missing a call to spec again, no? The following works fine for me:

(s/def ::spec-with-reason (st/spec {:reason "missingKey" :spec (s/keys :req-un [::k1 ::k2])}))
(st/explain ::spec-with-reason {:k2 "2" :k1 1})

;; => 1 - failed: missingKey in: [:k1] at: [:k1] spec: :spec-tools.core/k1

Let me know if I am missing something here.

@Sardtok
Copy link
Contributor

Sardtok commented Jun 15, 2022

@wandersoncferreira The :reason is set to "missingKey", while it should be "k1". This is still the case with your fix to the spec, but the location of the error is correct here.

The wanted result is:
1 - failed: k1 in: [:k1] at: [:k1] spec: :spec-tools.core/k1
Not:
1 - failed: missingKey in: [:k1] at: [:k1] spec: :spec-tools.core/k1

ikitommi added a commit that referenced this issue Jun 29, 2024
Fixes nested reasons being overwritten by outer spec's reason #171.
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants