jimblackler / jsonschemafriend

A JSON Schema loader and validator, delivered as a Java library.
Apache License 2.0
49 stars 23 forks source link

Validating a schema using strings #4

Closed ryandietrich closed 2 years ago

ryandietrich commented 3 years ago

I can load a schema from a file just fine, either as a resource, or directly using a java.io.file. When I try to take a string, and transform it into a ByteArrayInputStream, I get this rather cryptic error.

    net.jimblackler.jsonschemafriend.StandardGenerationException: {valid=false, errors=[{error=Unexpected type in data: ByteArrayInputStream, keywordLocation=http://json-schema.org/draft-04/schema, absoluteKeywordLocation=http://json-schema.org/draft-04/schema, instanceLocation=#}]}

I checked out the code, walked through SchemaStore, added some logging, re-compiled, and re-ran and I don't see a clear path on how to fix my issue.

My use case is pretty simple. I have a string that contains a schema, and a string that contains JSON data that I want to validate with the schema mentioned earlier. I can't figure out how to do this with this library, so I am moving on to something else.

jimblackler commented 3 years ago

So you're passing the ByteArrayInputStream into a schema constructor?

jimblackler commented 3 years ago

Or is it the object to test that you're passing in that way? If that's the case and the object is a string of JSON, you'll have to parse it first using a parser library. For example using the org.json library it's

new JSONObject(jsonString).toMap()

ryandietrich commented 3 years ago

I was passing it in as a byteArrayInputStream. I was just confused how the library seemed to assume all schemas came from files and it didn't have a simple "string" based approach.

jimblackler commented 3 years ago

I will add a new method to validate a string containing JSON data. Since there's a parser embedded already (for fetching schemas from http) it's not difficult to do. I can't change the behavior of the existing parse method as validating a string (not necessarily containing JSON data) by a schema is a valid thing to do.

jimblackler commented 2 years ago

Added the new methods, see https://github.com/jimblackler/jsonschemafriend#basic-example-using-json-strings