Fork me on GitHub
#ring-swagger
<
2017-02-08
>
abarylko00:02:18

Let's say I have a schema customer (defschema Customer .....), that has a field that may come invalid but I want to return null when is invalid... is there a way I could do that with coercion?

abarylko00:02:39

I'm using a custom-coercion-matcher that uses json-schema-coercion-matcher

abarylko00:02:05

but the matcher is called after checking for nil ... so I cannot return nil ...

leena06:02:21

Bumped into something a bit nasty with compojure-api (and compojure). If one calls a method with wrong number of arguments inside the endpoint declarations eg. (GET "/foo/bar" ... the compiler doesn't say anything. 😕

ikitommi06:02:44

@leena thats a Clojure-thing.

leena06:02:01

cause macros?

ikitommi06:02:26

You can use static analysis tools (Cursive does it automatically).

leena06:02:59

the trouble was I did not see the red underlining 😄

ikitommi06:02:22

no, it should work ok. Thanks to awesomeness of @cfleming, Cursive also does static analysis on compojure & compojure-api syntax.

ikitommi06:02:46

y not used, plus call is invalid.

ikitommi06:02:41

@abarylko can you use s/maybe? it allows nils...

leena06:02:39

it would be cool if the compiler could complain as well @ikitommi, then it wouldn't be just relying on a coders tired eyes 😉

ikitommi06:02:38

I agree! You have to poke Rich Hickey thou 😉 Spec should help, with help of tools like Spectrum (https://github.com/arohner/spectrum). I think Eastwood can help here already now (https://github.com/jonase/eastwood).

abarylko06:02:13

@ikitommi I am using s/maybe, for example s/maybe IpAddress .... however some of my data contains invalid IpAddress .... and I want to return in that case nil .... I was wondering if I can do it with coercion

ikitommi06:02:30

@abarylko I think you can. You can add a matcher that matches for IpAddress and does custom validation for it.

abarylko07:02:54

I did that by defining :coercion and using a custom coercion matcher

abarylko07:02:06

however the matcher is called after checking for nil

abarylko07:02:14

and then I can't return nil ....

abarylko07:02:27

is that what you are suggesting @ikitommi ?

ikitommi07:02:00

You have to compose a matcher with your matcher + json-matcher. something like this:

abarylko07:02:31

what's st/select-schema?

abarylko07:02:07

sorry... schema-tools .... right?

ikitommi07:02:26

it’s a coercion thing. strips out extra keys. but it was just to demonstrate how to compose matchers. c-api coercion takas a matcher, so you can use that in there.

ikitommi07:02:43

but there is no such thing as “post-coercion” step to change values to something that is not in the schema, so, a s/maybe is needed here.

abarylko07:02:17

Cool, again thanks a lot @ikitommi ! I think I owe you at least a few 🍺 !!!!