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

Schema parsing / conform #330

Merged
merged 11 commits into from
Jan 23, 2021
Merged

Schema parsing / conform #330

merged 11 commits into from
Jan 23, 2021

Conversation

nilern
Copy link
Contributor

@nilern nilern commented Jan 8, 2021

Something like this for #241 and the rest of #180?

Builds on top of #317.

  • On failure, (m/conform s v) returns :malli.core/nonconforming
  • For most schemas (m/conform s v) just returns something that is = to v (optimally, v itself).
  • But for the (new) :or* (tagged :or) (= (m/conform [:or* [:s string?] [:n int?]] 0) (miu/-tagged :n 0))
    • Non-identity conforming like this propagates: (= (m/conform [:tuple [:or* [:s string?] [:n int?]]] [0]) [(miu/-tagged :n 0)])
  • Seqexes conform much as in Spec:
(m/conform [:alt string? int?] [0]) ; => 0
(m/conform [:alt* [:s string?] [:n int?]] [0]) ; => (miu/-tagged :n 0)

(m/conform [:cat [:* string?] [:* int?]] ["foo" "bar" 1 2]) ; => [["foo" "bar"] [1 2]]
(m/conform [:cat* [:s [:* string?]] [:n [:* int?]]] ["foo" "bar" 1 2]) ; => {:s ["foo" "bar"], :n [1 2]}

(m/conform [:? string?] ["foo"]) ; => "foo"
(m/conform [:? string?] []) ; => nil

(m/conform [:* string?] ["foo" "bar"]) ; => ["foo" "bar"]
(m/conform [:* string?] []) ; => []

(m/conform [:+ string?] ["foo" "bar"]) ; => ("foo" "bar")
(m/conform [:+ string?] []) ; => :malli.core/nonconforming

(m/conform [:repeat {:min 1, :max 3} string?] []) ; => :malli.core/nonconforming
(m/conform [:repeat {:min 1, :max 3} string?] ["foo" "bar"]) ; => ["foo" "bar"]
(m/conform [:repeat {:min 1, :max 3} string?] ["foo" "bar" "baz" "quux"]) ; => :malli.core/nonconforming

(m/conform [:cat* [:s [:schema [:* string?]]]] [["foo" "bar"]]) ; => {:s ["foo" "bar"]}

@nilern nilern mentioned this pull request Jan 12, 2021
8 tasks
@nilern nilern marked this pull request as ready for review January 19, 2021 13:38
@nilern
Copy link
Contributor Author

nilern commented Jan 19, 2021

At this point it should be quite simple to adjust the result format to taste.

@nilern nilern requested a review from ikitommi January 19, 2021 13:55
@nilern
Copy link
Contributor Author

nilern commented Jan 20, 2021

Also, match, parse or even destructure would be a more usual name, especially if it is not going to be as similar to Spec as it is now.

@ikitommi
Copy link
Member

This is really good. Few suggestions:

  1. the conform should not throw, should fail with ::m/conforming instead, have a code for this (on top of this PR), makes the backtracking order of magnitude faster
  2. still think that we should use explain for conform, the code looks like 90% of c&p now. Can be done later
  3. the conform, I suggest parse, can do a PR on top of this.

@ikitommi ikitommi merged commit 2a504c8 into metosin:master Jan 23, 2021
ikitommi added a commit that referenced this pull request Jan 25, 2021
@nilern nilern mentioned this pull request Jan 27, 2021
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.

2 participants