Comments (3)
Same problem as #4.
The issue is that the library is 'BYOP'; Bring Your Own Parser. The 'data1' and 'data2' strings will need to be parsed to objects before validation.
For example
new ObjectMapper().readValue(data1, Map.class)
Rewriting your example:
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;
public class Test {
static String schemaStr = "{\n" +
" \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n" +
" \"properties\": {\n" +
" \"name\": {\n" +
" \"type\": \"string\",\n" +
" \"minLength\": 2\n" +
" }\n" +
" }\n" +
"}";
static String data1 = "{\n" +
" \"name\": \"Bill\"\n" +
"}\n";
static String data2 = "{\n" +
" \"name\": \"\"\n" +
"}\n";
public static void main(String[] args) {
try {
ObjectMapper mapper = new ObjectMapper();
SchemaStore schemaStore = new SchemaStore(); // Initialize a SchemaStore.
Map map = mapper.readValue(schemaStr, HashMap.class);
Schema schema = schemaStore.loadSchema(map);
Validator validator = new Validator();
// Will not throw an exception.
validator.validate(schema, new ObjectMapper().readValue(data1, Map.class));
// Will throw a ValidationException.
validator.validate(schema, new ObjectMapper().readValue(data2, Map.class));
} catch (Exception e) {
e.printStackTrace();
}
}
}
I've done a bad job of making this clear, I will have a quick attempt to clarify it in the README.
The reason I've done it this way is you can make your own choice to use Jackson, GSON, org.json parser, or have data in a compatible format like JSON5 (with my usejson library) or even YAML. All these parsers have their own tradeoffs, plus you might want to integrate jsonschemafriend into an app without having a different parser forced on you. Another benefit is that you might want to validate an object before serialization; BYOP avoids an extra step.
from jsonschemafriend.
Thanks for your reply.
In some cases, I just want to verify that a json string meets the JSON scema standard. But the following interface makes me not know what type of parameters to pass in, and there is no description in the comments.
public void validate(Schema schema, Object document) throws ValidationException {
Collection<ValidationError> errors = new ArrayList<>();
validate(schema, document, errors::add);
if (!errors.isEmpty()) {
throw new ListValidationException(errors);
}
}
I suggest adding comments to this interface to explain what type the second parameter accepts.
from jsonschemafriend.
0.11.0 now includes entry points for JSON strings (both schema and document).
See https://github.com/jimblackler/jsonschemafriend#basic-example-using-json-strings
from jsonschemafriend.
Related Issues (20)
- Issue in Android 5 HOT 1
- string date-time format HOT 1
- Support for custom error message in schema
- Parsing composite schema object definition HOT 5
- Support for JSON Schema simplification? HOT 3
- com.ibm.icu:icu4j is too heavy
- Failing to load draft-07 type schemas (probably lower ones too) HOT 1
- net.jimblackler.usejson.SyntaxError: JSON5: invalid end of input at 1:1 HOT 15
- Performance and functional comparison HOT 1
- Incorrect handling of format-assertions
- Set module name HOT 2
- type: integer does not match specification HOT 2
- The $ref is not resolved when referred schema is located in non-standard keywords HOT 1
- Schema with a wrong pointers to subschema passes the validation HOT 2
- SchemaStore enters infinite loop when $schema returns no content HOT 2
- Add support for custom formats
- 该库不支持安卓26以前的版本,请慎用。(This library does not support versions prior to Android 26, please use with caution.) HOT 1
- Support for custom types when document to be validated passed via a Map HOT 7
- Regular expression validation with CachedRegExPatternSupplier
- Is it possible to use openapi 3.x as schema HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonschemafriend.