Fork me on GitHub
#clojure-spec
<
2017-03-02
>
thheller10:03:52

so is anyone else constantly doing this: (s/keys ::req-un [::key-a ::key-b])? with all the namespaced keywords I usually don't even notice what is wrong

thheller10:03:13

and keep wondering why my spec doesn't work, any chance this could throw a warning?

thheller11:03:43

doesn't look like it would catch this case as ::req-un would just be ignored again

Yehonathan Sharvit13:03:54

Is there a spec for date?

Yehonathan Sharvit13:03:31

Thanks @souenzzo. What about a date that is stored as a string?

Yehonathan Sharvit16:03:10

I’d like the spec to validate “03/27/2016” but not “Hello World"

Alex Miller (Clojure team)16:03:52

There are date time parsers in the Java std lib

Alex Miller (Clojure team)16:03:19

Either that or a regex pred

Alex Miller (Clojure team)16:03:37

(I was just joking about string? :)

Alex Miller (Clojure team)16:03:57

user=> (import 'java.text.SimpleDateFormat)
java.text.SimpleDateFormat
user=> (def formatter (SimpleDateFormat. "MM/dd/yyyy"))
#'user/formatter
user=> (.parse formatter "03/27/2016")
#inst “2016-03-27T05:00:00.000-00:00”

Alex Miller (Clojure team)16:03:29

you’d want to wrap up the call to .parse to catch ParseException and return false

Alex Miller (Clojure team)16:03:04

(defn valid-date? [s] (try (.parse formatter s) (catch ParseException e false)))

mpenet16:03:15

this is not threadsafe tho

Alex Miller (Clojure team)16:03:48

usually you wrap that in a ThreadLocal

mpenet16:03:59

I had a weird debugging session because of this a few years back

mpenet16:03:27

yes or just use joda.time (or possibly new java api)

Alex Miller (Clojure team)16:03:43

good point - I haven’t used much of the new java 8 stuff

Alex Miller (Clojure team)16:03:25

you’d want to use java.time.format.DateTimeFormatter

Alex Miller (Clojure team)16:03:00

(import [java.time.format DateTimeFormatter DateTimeParseException] java.time.LocalDate)
(def format (DateTimeFormatter/ofPattern “MM/dd/yyyy"))
(defn valid-date? [s] (try (LocalDate/parse s format) (catch DateTimeParseException _ false)))

Alex Miller (Clojure team)16:03:48

that should be thread-safe

Alex Miller (Clojure team)17:03:32

please burn the prior example

Yehonathan Sharvit17:03:02

@mpenet what did u mean by not thread-safe?

gfredericks17:03:06

maybe the formatter isn't thread safe?

Yehonathan Sharvit17:03:40

what does it mean for a fomatter not to be thread-safe?

jr17:03:53

multiple threads using the same formatter is not safe

jr17:03:23

since formatter is a class instance and stores intermediate parse data in instance variables

mpenet17:03:42

yes if you call .parse from multiple threads you'll get garbage back sometimes

mpenet17:03:09

it gets quite obvious (date very much in the future etc etc)

gfredericks17:03:39

it seemed like a good idea in the 90's

zerocooljs20:03:35

hi please can you help me?? how I can make a spec case insensitive:

(s/def :vtex/channel #{"KUSHKIPAGOS"})

Alex Miller (Clojure team)21:03:49

you could replace the spec with #(= (clojure.string/upper-case %) "KUSHKIPAGOS")

bbloom21:03:12

on the topic of accidental double colon on ::req-un - that’s a situtation where the parameters are plainly provided statically, so MAAAAYBE warning on unrecognized keys wouldn’t be totally objectionable - but that would require trusting ppl to not abuse whatever flag enables that check

bbloom21:03:29

i mentioned this before, see https://www.typescriptlang.org/docs/handbook/interfaces.html and search for “excess property checking"

bbloom21:03:08

might be possible to accomplish with metadata or something - if the object has file/line/column info, then it might be a candidate for excess property checking

bbloom21:03:14

just a thought