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

:and should short-circuit explanation - in some cases #1001

Open
ikitommi opened this issue Jan 28, 2024 · 1 comment
Open

:and should short-circuit explanation - in some cases #1001

ikitommi opened this issue Jan 28, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@ikitommi
Copy link
Member

pulled from #993

Tangential

Depending on the validation it doesn't make sense to display all error messages. Consider:

(-> [:and
     [:vector :any]
     [:fn {:error/message "should be distinct"} #(= % (distinct %))]]
    (m/explain :invalid)
    (me/humanize))
; => ["invalid type" "should be distinct"]

But in other cases it might, such as a password field:

  • Must contain special characters
  • Must contain numbers
  • Must contain lowercase and uppercase letters

I don't have a suggestion and I'd need some hammock time pondering about this problem.

@frenchy64
Copy link
Contributor

frenchy64 commented Apr 5, 2024

I don't think this is a solvable problem if we require :and to be involved.

If instead we have a :distinct property, we can then have the explainer short circuit if the schema pred fails. Some schemas do this, but not -simple-schema.

If we make the necessary changes to -simple-schema's explainer we can get this behavior:

    (is (= ["invalid type"]
           (me/humanize (m/explain [:string {:distinct true}] nil))))
    (is (= ["should be distinct: \\[ provided 2 times"]
           (me/humanize (m/explain [:string {:distinct true}] "[["))))

To be an equivalent solution to :and + :fn it needs to be extensible (i.e., anyone can implement a property for any schema), which I have some ideas here: #1025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants