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

Float coercion #69

Open
deg opened this issue Apr 6, 2017 · 1 comment
Open

Float coercion #69

deg opened this issue Apr 6, 2017 · 1 comment

Comments

@deg
Copy link
Contributor

deg commented Apr 6, 2017

Javascript does not have distinct integer and float types. Therefore, 2.0 and 2 are equal.

When 2.0 is sent to the server (at least in JSON; probably also in Transit, though I've not tested), the server sees it as 2.

If my Vase schema has an attribute of type :float, a run-time error will be generated if the client passes a number that happens to be an integer (even if it was created by code that thinks it is dealing with floats).

This risk of surprise could be avoided if Vase would accept and coerce integers into float fields.

For now, I can work around this with a trivial interceptor, but it would be better if automated.
The following ten lines are a much too verbose way to ensure that one float field works safely!

(def float-conversion
  (i/interceptor
   {:name ::float-conversion
    :enter (fn [context]
             (let [payloads (get-in context [:request :json-params :payload])
                   payloads (map (fn [m] (if (:purchase/price m)
                                           (update m :purchase/price float)
                                           m))
                                 payloads)]
               (assoc-in context [:request :json-params :payload] payloads)))}))

@deg
Copy link
Contributor Author

deg commented Apr 27, 2017

Note that there is also an outstanding feature request to include this in Datomic: https://receptive.io/app/#/case/17714.

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

1 participant