Skip to content

Conversation

@frenchy64
Copy link
Collaborator

@frenchy64 frenchy64 commented Jan 13, 2025

This PR adds some schemas to help constrain the keysets of :map schemas. Instead of [:and [:map ..] [:fn <OPAQUE>]], you now write [:and [:map ...] [:xor ..] [:iff ..] ...].

The next step is to improve the generators for these schemas. The basic strategy to generate [:and S T] will be to generate S but propagate T as a hint to the S generator. Most of the work will be to canonicalize T by expanding into disjunctive normal form.

Humanization comes with the usual caveats of not being able to represent the difference between a conjunction or disjunction of errors, so some of the humanizers are a little strange. I'm not sure if this can be fixed without a breaking change to the humanization representation.

There are more general abstractions here (like the idea of simplifying a schema) but we can build towards that later. I have sketches of using such abstractions to compile optimal validators and generators. For now, concentrating on map keyset constraints is helping move this work along.

Note: the :max-tries change is to fix an unrelated flaky test:

ERROR in malli.generator-test/min-max-test (core.cljc:179)
valid properties
Exception: clojure.lang.ExceptionInfo: :malli.generator/distinct-generator-failure
{:type :malli.generator/distinct-generator-failure, :message :malli.generator/distinct-generator-failure, :data {:gen #clojure.test.check.generators.Generator{:gen #object[clojure.test.check.generators$such_that$fn__2964 0x66099385 "clojure.test.check.generators$such_that$fn__2964@66099385"]}, :max-tries 10, :num-elements [15](https://github.com/frenchy64/malli/actions/runs/12751433381/job/35538498628#step:6:16), :schema [:set {:gen/min 10, :max 20} int?]}}

@frenchy64 frenchy64 marked this pull request as ready for review January 13, 2025 09:39
@frenchy64 frenchy64 requested a review from ikitommi January 13, 2025 09:39
@frenchy64 frenchy64 marked this pull request as draft January 13, 2025 18:44
@frenchy64 frenchy64 marked this pull request as ready for review January 13, 2025 20:08
Copy link
Member

@ikitommi ikitommi left a comment

Choose a reason for hiding this comment

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

Hey, thanks for working on this and sorry for being out of reach for a month+. I read the code and tested the branch, like the approach and if this is a step forward in simplification etc, let's do this. But the code should be finished first. Some notes:

  • some TODOs (like transformers)
  • schema walking doesn't work, e.g.
(m/walk
 [:if [:has :user] [:has :pass] [:has :secret]]
 (m/schema-walker identity))

Also I json-schema mappings need to be done.


(deftest xor-test
(is (= [:xor [:= 1] [:= 2]]
(m/form (m/schema [:xor [:= 1] [:= 2]] {:registry (merge (mu/schemas) (m/default-schemas))}))))
Copy link
Member

Choose a reason for hiding this comment

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

PR adds new schemas to default registry, here expectation is that they are in malli.util.

@opqdonut
Copy link
Member

looks like there's still a lot TODO here, labeling as WIP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

WIP work in progress, not ready yet

Projects

Status: ⌛Waiting

Development

Successfully merging this pull request may close these issues.

3 participants