plumatic / schema

Clojure(Script) library for declarative data description and validation
Other
2.41k stars 257 forks source link

`constrained` doesn't work with defrecord schema's `strict-map` #338

Closed rsslldnphy closed 8 years ago

rsslldnphy commented 8 years ago

Not sure if this is by design, but s/constrained post conditions don't seem to work when using the strict-map-> function created by s/defrecord. Using version 1.0.5.

(ns demos.core
  (:require [schema.core :as s]))

(s/defrecord Foo [val :- (s/constrained s/Int pos?)])

(strict-map->Foo {:val -10})
;; => #demos.core.Foo{:val -10}

(s/validate Foo (map->Foo {:val -10}))
;; 1. Unhandled clojure.lang.ExceptionInfo
;;    Value does not match schema: {:val (not (pos? -10))}
   ;; {:type :schema.core/error,
    ;; :schema demos.core.Foo,
    ;; :value #demos.core.Foo{:val -10},
    ;; :error {:val #object[schema.utils.ValidationError
                         ;; "0x5a62d5be"
                         ;; "schema.utils.ValidationError@5a62d5be"]}}
;; 
                  ;; core.clj:  151  schema.core/validator/fn
                  ;; core.clj:  160  schema.core/validate
                  ;; core.clj:  155  schema.core/validate
                      ;; REPL:   42  demos.core/eval23330
                      ;; REPL:   42  demos.core/eval23330
             ;; Compiler.java: 6927  clojure.lang.Compiler/eval
             ;; Compiler.java: 6890  clojure.lang.Compiler/eval
w01fe commented 8 years ago

While I can see the name being confusing, strict-map-> does not do any validation. It's mostly an optimization for quickly creating records with only the base keys from a map.

rsslldnphy commented 8 years ago

Ah, then I totally misunderstood! Thanks for the clarification.

w01fe commented 8 years ago

No problem! I think a schematized constructor (possibly the existing ones) might make sense, but it's not something we have yet.