Fork me on GitHub
#clojure-spec
<
2018-05-15
>
jimbob15:05:51

so mostly just curious if there are ways to spec collections of things, but spec them in terms of the collection, not in terms of the individual items in the collection (so whether or not only certain fields for the items are unique, or that summing fields for all items is within a certain range, etc)

ghadi16:05:06

custom predicates with s/and is the common mechanism for doing this

👍 4
lilactown20:05:42

so I'm trying to do some work where I validate a JSON blob against a swagger spec. I'm thinking about trying to translate the swagger spec into clojure.spec and do it that way

lilactown20:05:58

basically the opposite of what spec-tools does with it's swagger stuff

lilactown20:05:28

however, it seems like there's an assumption that clojure.spec's are registered globally with a name and everything. I would be generating these specs dynamically, on the fly

lilactown20:05:34

is this a good idea?

hiredman20:05:45

any predicate is a spec

hiredman20:05:24

so if you have a function that given a swagger spec and some data returns true or false, then you just partial that with the swagger spec and you have a spec

hiredman20:05:55

it isn't a very nice spec, it won't generate and you won't get vary granular error messages, but it is a spec

lilactown20:05:15

okay. I was thinking of trying to use clojure.spec to do some of the heavy lifting around e.g. checking whether a map conforms to a set of expected values

lilactown20:05:51

so like

x-allOf: [
{
type: "object",
properties: {
personId: {
type: "integer",
format: "int64"
},
familyMemberId: {
type: "integer",
format: "int64"
}
},
required: [
"personId",
"familyMemberId"
]
},
gets turned into
(s/def ::personId integer?)
(s/def ::familyMemberId integer?)
(s/def ::my-obj (s/keys :req-un [::personId ::familyMemberId]))

lilactown20:05:46

but obvi this is made more difficult by the fact that this would need to be done for arbitrary number of swagger specs at runtime, so I would have to make sure I'm not clobbering things in the registry...

guy20:05:17

out of interest why couldn’t you model the data in spec? would it really be dynamic?

guy20:05:17

I’m probably using the wrong definition of dynamic, but i thought you would know what sort of data you would be getting? so you could potentially spec out the shape of the data?

lilactown20:05:32

I'm writing some code that you give it a URL to a swagger.json, a URL path, example request and an example response

lilactown20:05:49

and it validates that the URL path, request and response are valid based on the swagger.json

guy20:05:53

ohhh i see

guy20:05:08

so you can give it basically anything, and then you use the swagger to create a spec

lilactown20:05:19

that's the idea. not sure it's a good one

guy20:05:25

it sounds pretty cool 🙂

guy20:05:38

i see ur worries about the registry now

guy20:05:54

i guess you could test it by creating multiple unique specs and seeing how many you could create

lilactown20:05:16

the whole thing seems gross. I wish you didn't have to register them 🙃

simple_smile 4
seancorfield20:05:42

@lilactown Would it be easier/better to have something that generated spec source code from a swagger URL? After all, you only want to generate the spec once per API end point, but you'll want to validate data against that spec repeatedly...

hiredman20:05:07

it seems like it would be a better idea to use some swagger validator to validate the swagger, for a swagger validation service

hiredman20:05:58

I think the direction you see in spec-utils (spec -> swagger) makes sense because the idea is, you can consolidate different ways of specifying data (database schemas, swagger, json schema, etc) as spec specs, and then generate (or using spec to make sure you data matches the contract) those other things from spec as needed. But it doesn't seem like turning swagger in to specs, if all you care about is validating arbitrary swagger specs against arbitrary data, makes a lot of sense

lilactown20:05:18

yeah I guess I'm looking for a swagger validator, and I decided to try and build my own using clojure.spec? and that sounds like it's not a good idea

lilactown21:05:18

I'm actually struggling to find such a thing that exists, which is why I'm trying to build one in the first place

guy21:05:01

If you are doing it for fun, why not give it ago and see how it goes

guy21:05:07

You’ll defo learn something

lilactown21:05:37

eh it's not exactly for fun 😅

lilactown21:05:00

it happens to be fun but also, sprint ends in a week and a half

guy21:05:55

ah well thats a different matter, if ur doing it for work, i might try and find a different solution

hiredman21:05:42

googling java swagger validator and jvm swagger validator both seem to turn up good leads

lilactown21:05:52

@U055NJ5CC scjsv is exactly what i'm looking for! 😄 thanks!