Fork me on GitHub

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)


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

👍 4

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


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


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


is this a good idea?


any predicate is a spec


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


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


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


so like

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


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...


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


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?


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


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


ohhh i see


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


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


it sounds pretty cool 🙂


i see ur worries about the registry now


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


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

simple_smile 4

@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...


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


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


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


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


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


You’ll defo learn something


eh it's not exactly for fun 😅


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


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


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


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