clojureplumatic-schema

Schema with a min and max limit?


Is it possible to add a min and max limit to the following schema?

I would like schema to throw an exception when Number is < 0 and > 100.

(def Number
  schema/Int)

Solution

  • You can use schema/pred to incorporate arbitrary predicates:

    (schema/pred #(<= 0 % 100))
    

    You can also combine that with a schema using schema/constrained:

    (schema/constrained schema/Int #(<= 0 % 100))
    

    You get better error messages if you name your predicate (e. g. in-range).