Comments (4)
To support custom vocabularies, for V201909 and V202012 the keywords are moved into their respective vocabularies.
I cannot replicate your issue so you will likely need to supply more information about what you are doing
package com.networknt.schema;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.networknt.schema.SpecVersion.VersionFlag;
import com.networknt.schema.serialization.JsonMapperFactory;
public class Issue994Test {
@Test
void test() throws JsonMappingException, JsonProcessingException {
String schemaData = "{\r\n"
+ " \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\r\n"
+ " \"type\": \"object\",\r\n"
+ " \"properties\": {\r\n"
+ " \"textValue\": {\r\n"
+ " \"type\": [\r\n"
+ " \"string\",\r\n"
+ " \"null\"\r\n"
+ " ],\r\n"
+ " \"isMandatory\": true\r\n"
+ " }\r\n"
+ " }\r\n"
+ "}";
JsonNode schemaNode = JsonMapperFactory.getInstance().readTree(schemaData);
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaNode);
String inputData = "{\r\n"
+ " \"textValue\": \"hello\"\r\n"
+ "}";
System.out.println(schema.validate(inputData, InputFormat.JSON, OutputFormat.HIERARCHICAL));
}
}
from json-schema-validator.
The hint with the vocabularies made my code working 👍
To give more context:
I build a schema factory with a custom meta schema like this now
// base on JsonMetaSchema.V202012 copy code below
JsonMetaSchema customMetaSchema = new JsonMetaSchema.Builder("https://json-schema.org/draft/2020-12/schema")
.idKeyword("$id")
.formats(Formats.DEFAULT)
.keywords(ValidatorTypeCode.getKeywords(SpecVersion.VersionFlag.V202012))
.keywords(Vocabulary.V202012_VALIDATION.getKeywords())
// keywords that may validly exist, but have no validation aspect to them
.keywords(Arrays.asList(
new NonValidationKeyword("$schema"),
new NonValidationKeyword("$id"),
new NonValidationKeyword("title"),
new NonValidationKeyword("description"),
new NonValidationKeyword("default"),
new NonValidationKeyword("definitions"),
new NonValidationKeyword("$comment"),
new NonValidationKeyword("$defs"),
new NonValidationKeyword("$anchor"),
new NonValidationKeyword("deprecated"),
new NonValidationKeyword("contentMediaType"),
new NonValidationKeyword("contentEncoding"),
new NonValidationKeyword("examples"),
new NonValidationKeyword("then"),
new NonValidationKeyword("else"),
new NonValidationKeyword("additionalItems")
))
// add your custom keyword
.keyword(new IsMandatoryKeyword())
.build();
JsonSchemaFactory jsonSchemaFactory = new JsonSchemaFactory.Builder().defaultMetaSchemaIri(customMetaSchema.getIri())
.metaSchema(customMetaSchema)
.build();
JsonSchema jsonSchema = jsonSchemaFactory.getSchema(jsonNode);
Maybe there is some more room for improvement?
from json-schema-validator.
The JsonMetaSchema
builder accepts a JsonMetaSchema
instance as a base if you want to modify an existing one.
JsonMetaSchema metaSchema = JsonMetaSchema.builder(JsonMetaSchema.getV202012()).keyword(new IsMandatoryKeyword())).build();
from json-schema-validator.
Thank you. This works very well 🥳
from json-schema-validator.
Related Issues (20)
- Downgrade maven-source-plugin to 3.2.1
- Resolve javadoc warning messages
- `JsonSchema.getSubSchema()` fails with JsonPointer containing a number HOT 2
- Possible performance improvement: JsonNodePath.hashCode() HOT 3
- How to generate the json structure acording to the schema? HOT 2
- oneOf with discriminator does not verify json correctly HOT 4
- The location of the `module-info.class` in the jar file is wrong HOT 1
- anyOf and OneOf unhelpful error messages HOT 2
- Exponential algorithm in BaseJsonValidator.hasAdjacentKeywordInEvaluationPath HOT 13
- Validation error for "id" field in referenced schema HOT 1
- Missing error text details for values of enumeration when enum type is JSON object HOT 3
- is date-time supported in V201909 and latest version of jar HOT 1
- `oneOf` for empty array. HOT 1
- SLF4J 2 dependency, despite being fulfilled by older version HOT 9
- Unevaluated properties and subschemas HOT 4
- Unable to validate all elements of the array using 2020-12 Json schema HOT 4
- Schema validate always print "Failed to apply pattern" when validate fail HOT 1
- Invalid Schema Structure Not Being Enforced HOT 4
- Undertow version containing CVEs HOT 3
- OpenAPI Spec 3.0 validation should not require exclusiveMinimum to be a number 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 json-schema-validator.