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 cursor validation issue #24

Open
vseguip opened this issue Jul 14, 2014 · 1 comment
Open

Schema cursor validation issue #24

vseguip opened this issue Jul 14, 2014 · 1 comment

Comments

@vseguip
Copy link

vseguip commented Jul 14, 2014

Hello I attach the following minimal test case:


(ns pvhmi.core
  (:import [goog.dom query])
  (:require-macros [cljs.core.async.macros :refer [go]]
                   [schema.macros :as sm])
  (:use [jayq.core :only [$]])
  (:require [om.core :as om :include-macros true]
            [om.dom  :include-macros true]
            [clojure.data :as data]
            [clojure.string :as string]
            [om-tools.schema :as schema]
            [schema.core :as s]
            [om-tools.core :refer-macros [defcomponent defcomponentk]]
            [om-tools.dom :as dom :include-macros true]))


(sm/defrecord V [x :- s/Int 
                 y :- s/Int])

(def Simple 
  {s/Keyword V})

(def Composed 
  {:vecs Simple})

(def Data (atom {:vecs {:a (V. 1 2) :b (V. 3 4)}}))

(defcomponent child
  [v :- Simple owner]
  (render-state [this state]
                (dom/div nil
                         (dom/h3 nil "Child Results")
                         (if-let [err (s/check (schema/cursor Simple) v)] (dom/h4 nil (str "Error validating child cursor: " err)) (dom/h4 "Success validating child cursor"))
                         (if-let [err (s/check Simple v)] (dom/h4 nil (str "Error validating child cursor vs value: " err)) (dom/h4 "Success validating child cursor"))
                         (if-let [err (s/check Simple (.-value v))] (dom/h4 nil (str "Error validating child value: " err)) (dom/h4 "Success validating child value"))) ))

(defcomponent parent
  [d :- Composed owner]
  (render-state [this state]
     (dom/div nil
              (dom/h1 nil "Parent results")
              (if-let [err (s/check (schema/cursor Composed) d)] (dom/h2 nil (str "Error validating parent cursor: " err)) (dom/h2 "Success validating parent cursor"))
              (if-let [err (s/check Composed d)] (dom/h2 nil (str "Error validating parent cursor vs value: " err)) (dom/h2 "Success validating parent cursor"))

              (if-let [err (s/check  Composed (.-value d))] (dom/h2 nil (str "Error validating parent value: " err)) (dom/h2 "Success validating parent value"))

                (om/build child (:vecs d)) )))


(om/root
 parent
 Data
 {:target (. js/document (getElementById "main"))
  })

Project dependencies:

 :dependencies [[org.clojure/clojure "1.5.1"]
                 [org.clojure/clojurescript "0.0-2227"]
                 [org.clojure/core.async "0.1.267.0-0d7780-alpha"]
                 [jayq "2.5.1"]
                 [om "0.6.4"]
                 [com.cemerick/clojurescript.test "0.3.1"]
                 [prismatic/om-tools "0.2.2"]
                 ]

This renders the following page

Parent results

Error validating parent cursor: {:vecs {:a (not (instance? pvhmi.core/V #pvhmi.core.V{:x 1, :y 2})), :b (not (instance? pvhmi.core/V #pvhmi.core.V{:x 3, :y 4}))}}

Error validating parent cursor vs value: {:vecs {:a (not (instance? pvhmi.core/V #pvhmi.core.V{:x 1, :y 2})), :b (not (instance? pvhmi.core/V #pvhmi.core.V{:x 3, :y 4}))}}

Success validating parent value

Child Results

Error validating child cursor: {:a (not (instance? pvhmi.core/V #pvhmi.core.V{:x 1, :y 2})), :b (not (instance? pvhmi.core/V #pvhmi.core.V{:x 3, :y 4}))}

Error validating child cursor vs value: {:a (not (instance? pvhmi.core/V #pvhmi.core.V{:x 1, :y 2})), :b (not (instance? pvhmi.core/V #pvhmi.core.V{:x 3, :y 4}))}

Success validating child value

Which seems counterintuitive to me. I would expect the first and third if-let to succeed and the middle one to fail. However it seems to fail whenever trying to validate a cursor. What am I doing wrong?

@loganlinn
Copy link
Member

Thanks for the report. I agree that this is not ideal behavior. I suspect that om-tools.schema/cursor will need to be modified to correctly handle Records. It's current implementation is extremely simple. I hope to take a deeper look later this week or weekend, but pull requests are certainly welcome.

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

No branches or pull requests

2 participants