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

metabase.util.schema to Malli #27471

Merged
merged 19 commits into from
Jan 5, 2023
Merged

metabase.util.schema to Malli #27471

merged 19 commits into from
Jan 5, 2023

Conversation

qnkhuat
Copy link
Contributor

@qnkhuat qnkhuat commented Dec 30, 2022

Migrate metabase.util.schema to use Malli.

Rename all existing Plumatic schema to have a post fix Plumatic and the default name will be reserved for Malli.

Example:

  • NonBlankString -> NonBlankStringPlumatic
  • NonBlankString will be Mallis schema

Ideally, we could change most of defendpoint-schema to defendpoint after this. Hopefully, this is not too ambitious 🤞

Note for reviewers: this PR contains a lot of file changes but most of them are renaming.

The main change is in metabase.util.schema and test is in metabase.util.schema-test


This change is Reviewable

@codecov
Copy link

codecov bot commented Dec 30, 2022

Codecov Report

Base: 64.89% // Head: 64.96% // Increases project coverage by +0.07% 🎉

Coverage data is based on head (15d8627) compared to base (d03cc87).
Patch coverage: 88.97% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #27471      +/-   ##
==========================================
+ Coverage   64.89%   64.96%   +0.07%     
==========================================
  Files        3179     3179              
  Lines       92322    92497     +175     
  Branches    11736    11734       -2     
==========================================
+ Hits        59910    60093     +183     
+ Misses      27574    27568       -6     
+ Partials     4838     4836       -2     
Flag Coverage Δ
back-end 85.39% <88.97%> (+0.04%) ⬆️
front-end 46.13% <ø> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...nterprise/advanced_permissions/api/application.clj 100.00% <ø> (ø)
...end/src/metabase_enterprise/audit_app/api/user.clj 100.00% <ø> (ø)
...uery_processor/middleware/handle_audit_queries.clj 75.00% <0.00%> (ø)
...se/backend/src/metabase_enterprise/sso/api/sso.clj 100.00% <ø> (ø)
src/metabase/api/activity.clj 92.35% <ø> (ø)
src/metabase/api/automagic_dashboards.clj 86.52% <ø> (-0.10%) ⬇️
src/metabase/api/google.clj 100.00% <ø> (ø)
src/metabase/api/login_history.clj 33.33% <ø> (ø)
src/metabase/api/persist.clj 64.89% <0.00%> (ø)
src/metabase/api/premium_features.clj 100.00% <ø> (ø)
... and 151 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@@ -399,6 +609,39 @@
s/Keyword s/Any}
(deferred-tru "parameter must be a map with :id and :type keys")))

(defschema ParameterSourceMalli
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is here just because I want to try to use multi.

Ideally, this should be part of ParameterMalli but I don't know how to merge a [:map] with a [:multi]. Suggestions are greatly appreciated.

Copy link
Contributor

Choose a reason for hiding this comment

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

;; TODO - rename this to `PositiveInt`?
(def IntGreaterThanZero
"Schema representing an integer than must also be greater than zero."
(with-api-error-message
(s/constrained s/Int (partial < 0) (deferred-tru "Integer greater than zero"))
(deferred-tru "value must be an integer greater than zero.")))

(def NonNegativeInt
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is exactly the same as IntGreaterThanOrEqualToZero. so I replaced all the places that use it with IntGreaterThanOrEqualToZero.

@@ -185,10 +193,6 @@
(defmethod accept 'keyword? [_ _ _ _] "keyword")
(defmethod accept :keyword [_ _ _ _] "keyword")

(defmethod accept 'integer? [_ _ _ _] "integer")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this was overriding the 'integer methods above.

@@ -326,6 +329,20 @@
(wrap-response-if-needed
(do ~@body))))))))

(defmacro defendpoint-async-schema
Copy link
Contributor Author

Choose a reason for hiding this comment

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

write this to be asymmetry with defendpoint-schema and defendpoint

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice catch

@deploysentinel
Copy link

deploysentinel bot commented Dec 30, 2022

No failed tests 🎉

[:map
[:id NonBlankStringMalli]
[:type keyword-or-non-blank-str-malli]
;; TODO how to merge this with ParameterSourceMalli above?
Copy link
Contributor

@escherize escherize Dec 30, 2022

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ha. Thanks for letting me know.

@escherize
Copy link
Contributor

escherize commented Dec 30, 2022

What do you think of renaming the schemas in metabase.util.schema like so:

  • NonBlankString -> NonBlankStringPlumatic
  • NonBlankStringMalli -> NonBlankString

That would help highlight the fact that we want to move past the Plumatic things, and make the malli ones nicer to use.

Also, that's what I did with defendpoint.

Copy link
Contributor

@escherize escherize left a comment

Choose a reason for hiding this comment

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

Great work, You caught a few bugs I left in!

I had a few requests and questions but nothing major. It will be great to get more and more things using malli.

I'm not really sure if defschema is worth using, but if you keep it in, please use the Malli version of it from this comment.

Then the renaming would be nice but can be done later

@qnkhuat
Copy link
Contributor Author

qnkhuat commented Jan 3, 2023

NonBlankString -> NonBlankStringPlumatic
NonBlankStringMalli -> NonBlankString

yes, let's do this.

I'm not really sure if defschema is worth using

yea, it was meant to make defining a schema shorter, but now I'm thinking about it doesn't save much lines of code.

@qnkhuat qnkhuat requested review from escherize and a team January 3, 2023 09:32
.clj-kondo/config.edn Outdated Show resolved Hide resolved
@camsaul camsaul requested a review from a team January 4, 2023 06:20
@qnkhuat qnkhuat requested a review from camsaul January 4, 2023 08:21
@@ -501,9 +507,10 @@
"Schema for a string that is a valid representation of a boolean (either `true` or `false`).
Something that adheres to this schema is guaranteed to to work with `Boolean/parseBoolean`."
(mc/schema
[:and {:error/fn (constantly (deferred-tru "value must be a valid boolean string (''true'' or ''false'')."))}
[:and
Copy link
Contributor

Choose a reason for hiding this comment

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

what do you think of using [:enum "true" "false"] here? you did setup those tests to make sure the new m.schemas are equal to the old ones, but if it's too drastic a change that makes sense too

@camsaul camsaul removed their request for review January 4, 2023 17:51
@qnkhuat qnkhuat requested a review from a team January 5, 2023 06:59
@qnkhuat qnkhuat requested a review from escherize January 5, 2023 07:31
Copy link
Contributor

@escherize escherize left a comment

Choose a reason for hiding this comment

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

Looks good. Just saw some failing tests having to do with s/defn being used with malli schemas. Once they're fixed we should be good to go

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.

None yet

5 participants