Fork me on GitHub
#clojurescript
<
2020-08-24
>
Kasey Speakman01:08:24

For form validation in CLJS app, am I right in thinking I will need: • spec step for raw input strings - e.g. required but blank • transform step - e.g. "asdf" can't convert to number • spec step for converted data (not in range or otherwise logically invalid)

kwladyka08:08:04

So this is how it can be done with spec in cljs

Kasey Speakman14:08:54

Ok, cool. Thanks.

Dmytro Bunin07:09:28

this is another cool library which can help you out https://github.com/alexanderkiel/phrase

👍 3
Kasey Speakman01:08:09

Or I guess just not sure what is good practice there. Previously in F#, I wrote a very small lib to compose validations and/or type conversion. (Failing to type-convert as a validation error.)

Kasey Speakman02:08:40

Hmm, looks like maybe spec-tools or something like it is a common answer. https://cljdoc.org/d/metosin/spec-tools/0.10.4/doc/spec-coercion

Bediako George06:08:56

Hi! Question regarding best practice around configuring values/constants based on deployment. I want to have a different value for variable/constant based on whether the clojurescript app is running in DEV v TEST v PROD. I am running re-frame. Any suggestions or pointers?

Pete Parker00:09:25

Is there a distinction in shadow-cljs between a test release and a prod release? The only options seem to be :dev and :release, which means all release builds will use the same values.

p-himik05:09:44

If your test build must be sufficiently different from the dev build, then you can just add a new build configuration specifically for testing.

Pete Parker18:09:33

It’s different only in the :closure-defines so it would be too much duplicate configuration to have the test as its own config.

p-himik18:09:27

I don't know shadow-cljs that well to give a better answer - you should definitely ask in #shadow-cljs

restenb10:08:58

anybody know how the clear the browser dev console from ClojureScript?

thheller10:08:41

(js/console.clear)

restenb10:08:58

tried that, it fails (console is not a function)

thheller10:08:26

console is not expected to be a function? sounds like you maybe tried (js/console)?

restenb10:08:01

thought i'd add it to the :before-jsloadfigwheel hook

restenb10:08:25

it's weird, "my" js/consoleonly has error, info, log, and warn methods available

p-himik10:08:07

I think you either have a very strange browser or something overrides the global console object.

restenb10:08:09

must be something with how or when Figwheel calls my on-jsload function. If I just add (js.console/clear)to my "main" method it works

rickmoynihan13:08:13

So I just added some specs into a clojurescript project, that uses (st/instrument) to instrument the specs in dev. This is in a browser, and the spec errors are almost unusable in the console — like far worse than clj is. I’m currently using the binaryage/cljs-devtools, and suspect that might be contributing to making them worse. How do people get useful errors out of spec instrumentation in cljs?

rickmoynihan13:08:14

I already include expound actually, thanks - i’ll give it a try

bbrinck13:08:25

@rickmoynihan This ticket includes some code for an error formatter for devtools. https://github.com/bhb/expound/issues/152

👀 3
rickmoynihan14:08:48

ahh it’s the same as what I’ve already taken from the README 🙂 Thanks for the heads up though

👍 3
bbrinck14:08:06

If that doesn’t work for you, I’d be curious to see what you end up doing. I am interested in expanding the Expound documentation to include some additional ideas on this topic.

rickmoynihan14:08:56

Looks fine for now, but I’ll let you know if I think of anything

👍 3
rickmoynihan10:08:54

Hmmm I swear this was working yesterday, but today my spec errors aren’t being pretty printed 😕

rickmoynihan10:08:47

I have:

(ns myapp.error-formatter
  (:require [cljs.repl :as repl]
            [expound.alpha :as expound]
            [cljs.spec.alpha :as s :include-macros true]))

(set! s/*explain-out* expound/printer)

(println "error formatter set")

(def devtools-error-formatter
  "Uses cljs.repl utilities to format ExceptionInfo objects in Chrome devtools console."
  #js{:header
      (fn [object _config]
        (println "custom formatter called")
        (when (instance? ExceptionInfo object)
          (let [message (some->> (repl/error->str object)
                                 (re-find #"[^\n]+"))]
            #js["span" message])))
      :hasBody (constantly true)
      :body (fn [object _config]
              #js["div" (repl/error->str object)])})
(defonce _
         (some-> js/window.devtoolsFormatters
                 (.unshift devtools-error-formatter)))
in a namespace, and shadow-cljs incuding this via a preload:
:preloads [devtools.preload
                              myapp.error-formatter ;; <- pretty print spec errors with expound
                              myapp.front-end-specs ;; <- spec instrumentation
                              ]

rickmoynihan10:08:42

Ok I seemed to have an out dated install of cljs-devtools… upgrading seems to have fixed this

👍 3
rickmoynihan14:08:11

hmm… just tweaked a spec, and now getting:

Var clojure.test.check.generators/simple-type-printable does not exist, clojure.test.check.generators never required
reporter.cljs:32 In CLJS DevTools 1.0.0, an exception was raised during value formatting.

 Error: Var clojure.test.check.generators/simple-type-printable does not exist, clojure.test.check.generators never required
    at eval (alpha.cljs:74)
    at Object.eval [as cljs$core$IDeref$_deref$arity$1] (alpha.cljs:22)
    at Object.cljs$core$_deref [as _deref] (core.cljs:686)
    at Object.cljs$core$deref [as deref] (core.cljs:1475)
    at Function.eval [as cljs$core$IFn$_invoke$arity$variadic] (alpha.cljs:74)
    at Object.cljs$spec$gen$alpha$simple_type_printable [as simple_type_printable] (alpha.cljs:74)
    at eval (alpha.cljs:90)
    at Object.eval [as cljs$core$IDeref$_deref$arity$1] (core.cljs:10572)
    at Object.cljs$core$_deref [as _deref] (core.cljs:686)
    at Object.cljs$core$deref [as deref] (core.cljs:1475)

---
Please report the issue here: 

rickmoynihan14:08:11

I’m assuming this is because I have some fspec s on some functions that are used in an fdef

rickmoynihan14:08:37

I’d thought that cljs had stopped requiring you to use clojure.test.check generators 👀

rickmoynihan14:08:51

https://clojurescript.org/news/2019-01-31-release ahh I guess you do require them if you have an fspec?

rickmoynihan14:08:09

though I don’t define any :ret specs

rickmoynihan14:08:24

yeah ok just adding a require for clojure.test.check fixes this