What is the best/recommended way to validate that a JSON payload is valid swagger (Java library)

3,672 views
Skip to first unread message

Gautam Priya

unread,
Oct 2, 2015, 9:09:45 PM10/2/15
to Swagger
I know that several options exist to achieve this. Looking for recommendations from the swagger community.

I know swagger 2.0 is compliant with json schema draft v4

Jackson's validation module is still roughly on to draft v3 - per their github README.

So I was thinking about going with with Francis's (fge on github) java library that supports draft v4 and is the recommended library on json-schema.org 

However, I know about swagger-parser and swagger-validator (new in 2.0). So I am wondering if there is a way to use either of those as a java library for doing server side validation of json payloads to ensure that they are valid swagger 2.0.

Guidance appreciated.
Thanks much,
gautam


Diff

unread,
Feb 1, 2016, 1:10:04 PM2/1/16
to Swagger
gautam,
Did you ever figure out how to validate the JSON payload?

tony tam

unread,
Feb 1, 2016, 3:08:34 PM2/1/16
to swagger-sw...@googlegroups.com
Diff and others,
You have choices.  If you’re working with Java, you can use the inflector project, or the logic that it uses to do the validation.  It’s quite easy to do so.

This class:

validates an object, input or output, against a portion of the schema.

The schema can be fully resolved by the swagger-parser so it can be used by the SchemaValidator:

new ResolverUtil().resolveFully(swagger);

Finally, invoke the SchemaValidator with the input + schema required, and you’ll get your answer:



--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Diff

unread,
Feb 1, 2016, 3:13:39 PM2/1/16
to Swagger
The hesitation with using the inflector is that the service code that is heavily coupled with the inflector code.  If my understanding is correct, each API would be required to have the RequestContext and Response Context.  Or can the validation piece be done without any of that?
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

tony tam

unread,
Feb 1, 2016, 3:14:22 PM2/1/16
to swagger-sw...@googlegroups.com
The code is all apache 2.0.  Use the logic as you see fit, I’m trying to point you to where the work gets done.

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.

Diff

unread,
Feb 1, 2016, 3:16:58 PM2/1/16
to Swagger
Is the validation logic able to used without having the RequestContext and related couplings?  In other words, could I just call the SchemaValidator and pass in my own objects to validate?

tony tam

unread,
Feb 1, 2016, 3:18:15 PM2/1/16
to swagger-sw...@googlegroups.com
Take a look at the code.  I’ve pointed you to exactly where that happens.

We built the inflector to do this for you, but if you want to do it differently, you’ll have to dig in to the code a bit.

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.

Diff

unread,
Feb 1, 2016, 3:23:21 PM2/1/16
to Swagger
Ok will do. I thought it was a quick/simple question that could be answered with a yes/no, but thanks for the help anyway...

Gautam Priya

unread,
Feb 1, 2016, 7:15:10 PM2/1/16
to Swagger
Diff - we ended up using the TV4 JS ( http://geraintluff.github.io/tv4/) validator for our needs using it via nashorn. 

Tony - Thanks for the pointers to the inflector project. I did look at it. It is possible to use the https://github.com/swagger-api/swagger-inflector/blob/master/src/main/java/io/swagger/inflector/schema/SchemaValidator.java class to validate a JSON payload against the swagger 20 schema. I did notice that it uses Francis's (fge) JSON validator to do so - 

com.github.fge.jsonschema.main.JsonSchema jsonSchema = factory.getJsonSchema(schemaObject);
ProcessingReport report = jsonSchema.validate(content);

Now since Francis is no longer actively maintaining this project, hence, we were a little hesitant to use it. 

Thanks,
gautam
Reply all
Reply to author
Forward
0 new messages