Comments (17)
It's not really clear what you are trying to do but the code that you posted likely doesn't do anything useful.
The first parameter to JsonMetaSchema.builder
should be the IRI of the meta schema ie. the id and not the retrieval URI.
Are you trying to validate a schema using the meta schema? Or are you trying to validate some inputs using a schema?
If you want to validate inputs using a schema you would need to implement the keywords / vocabularies.
If you just want to validate a schema using a meta schema then that is possible without implementing the keywords / vocabularies but you should be providing the mapping of the id eg. https://example.org/meta/schema
to the retrieval URI eg. classpath:schemas/my-schema.json
.
You should use https://json-schema.org/draft/2019-09/schema
instead of http://json-schema.org/draft/2019-09/schema#
if you want to refer to draft 2019-09.
from json-schema-validator.
Hi @justin-tay , thank you for your response.
I am trying to validate a schema using my meta schema, so I guess I should do your second suggestion.
How can I map my meta schema id to the retrieval URI?
from json-schema-validator.
You should define the mappings in the factory.
package com.networknt.schema;
import java.util.Collections;
import org.junit.jupiter.api.Test;
import com.networknt.schema.SpecVersion.VersionFlag;
public class Issue1045Test {
@Test
void metaSchemaValidation() {
String instanceData = "{\r\n"
+ " \"myType\": \"bad\"\r\n"
+ "}";
SchemaValidatorsConfig config = new SchemaValidatorsConfig();
config.setPathType(PathType.JSON_POINTER);
config.setFormatAssertionsEnabled(true);
JsonSchema schema = JsonSchemaFactory
.getInstance(VersionFlag.V201909,
builder -> builder.schemaMappers(
schemaMappers -> schemaMappers.mappings(Collections.singletonMap("https://example.org/meta/schema", "classpath:schemas/my-schema.json"))))
.getSchema(SchemaLocation.of("https://example.org/meta/schema"), config);
System.out.println(schema.validate(instanceData, InputFormat.JSON, OutputFormat.HIERARCHICAL));
}
}
from json-schema-validator.
Thanks for that example. I am still getting the warning unknown keyword message in the log. When I remove the schema mappers line I get a FileNotFoundException so that suggests it is loading my file from the classpath correctly.
Is it possible that unless the keyword is added with the java api that it will always display that warning message?
When I use the below I don't get the message:
JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909) { builder ->
builder
.metaSchema(
JsonMetaSchema.builder(
"https://example.org/meta/schema",
JsonMetaSchema.getV201909(),
)
.keyword(keyword)
.build()
)
.build()
}
from json-schema-validator.
You shouldn't get an unknown keyword warning unless you are attempting to load a schema that uses your custom meta schema.
Are you trying to validate a schema using the meta schema? Or are you trying to validate some inputs using a schema? You aren't showing any code to tell.
from json-schema-validator.
Thanks Justin. I have this code to create the factory and get a schema. In this example, as shown in my first comment, I have a myType keyword in my meta schema that I want to use.
val jsonSchemaFactory: JsonSchemaFactory =
JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909) { builder ->
builder
.schemaMappers {
it.mappings(mapOf("https://example.org/meta/schema" to "classpath:schemas/my-schema.json"))
}
.build()
}
private fun getJsonSchema(jsonSchema: String): JsonSchema {
val config = SchemaValidatorsConfig().apply {
isHandleNullableField = true
isJavaSemantics = true
}
return jsonSchemaFactory.getSchema(jsonSchema, config)
}
And wanting to create a schema which inherits from my meta schema, something like below:
{
"properties": {
"FOO": {
"description": "Message type",
"type": "string",
"myType": "FOO"
}
},
"description": "",
"title": "Something",
"$defs": {},
"$id": "https://example.org/another-schema",
"$schema": "https://example.org/meta/schema",
"type": "object"
}
With the above I see the warning message in the log when this line executes jsonSchemaFactory.getSchema(jsonSchema, config)
from json-schema-validator.
You are attempting to load a schema that uses your custom meta schema.
If you want to validate inputs using such a schema you would need to implement the keywords / vocabularies in Java to implement the desired validation behavior.
If you don't do anything then obviously the library can't do validation on unknown keywords and would by default pass validation. The warning is there to tell you that that is probably not what you want.
See https://github.com/networknt/json-schema-validator/blob/master/doc/custom-meta-schema.md
from json-schema-validator.
Right, ok. Is there a way to generate the meta schema as json from the Java API? I need the meta schema in a file also and ideally would then like to have the Java API as the source of truth, and generate from there. Or will I need to manually create the meta schema json file alongside the java api?
from json-schema-validator.
No there is no way to generate the meta schema using the Java API. They are not the same thing so there's no single source of truth. The meta schema simply describes what a valid schema looks like and lists the relevant vocabularies. The Java API is for how to locate the vocabulary, keyword and format implementations.
This is much the same as the standard dialect meta schemas so you can just refer to how the standard ones look like, eg. https://json-schema.org/draft/2020-12/schema.
It is preferable to define a custom vocabulary to hold the custom keywords that you want to create.
from json-schema-validator.
Ah I see. Question on the vocabularies, I see in this example it is creating a vocabulary "https://www.example.com/vocab/equals" https://github.com/networknt/json-schema-validator/blob/master/doc/custom-meta-schema.md#associating-vocabularies-to-a-dialect. What would be the way someone would find out what's in that vocabulary?
from json-schema-validator.
The reason I ask is because my custom keyword is an enum and I want to share with the API client what the possible enum values are.
from json-schema-validator.
See https://github.com/json-schema-org/json-schema-vocabularies
In the same way the standard spec describes what the standard keywords does you would use documentation to describe it.
from json-schema-validator.
Ok, thank you @justin-tay, I will try this out.
from json-schema-validator.
Really appreciate your time @justin-tay . I hope there's a buy coffee link somewhere!
I now have this as my meta schema (example-meta-schema.json):
{
"$schema": "http://json-schema.org/draft/2019-09/schema",
"$id": "https://example.org/schema/meta/example",
"$vocabulary": {
"https://json-schema.org/draft/2019-09/vocab/core": true,
"https://json-schema.org/draft/2019-09/vocab/applicator": true,
"https://json-schema.org/draft/2019-09/vocab/unevaluated": true,
"https://json-schema.org/draft/2019-09/vocab/validation": true,
"https://json-schema.org/draft/2019-09/vocab/meta-data": true,
"https://json-schema.org/draft/2019-09/vocab/format-annotation": true,
"https://json-schema.org/draft/2019-09/vocab/content": true,
"https://example.org/meta/vocab/example": true
},
"$dynamicAnchor": "meta",
"title": "Example JSON meta schema",
"allOf": [
{
"$ref": "https://json-schema.org/draft/2019-09/schema"
},
{
"$ref": "https://example.org/schema/meta/vocab/example"
}
]
}
and code:
val vocabularyFactory = VocabularyFactory { iri ->
if ("https://example.org/meta/vocab/example" == iri) {
Vocabulary("https://example.org/meta/vocab/example", keyword)
} else {
null
}
}
val jsonSchemaFactory: JsonSchemaFactory =
JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909) { builder ->
builder
.metaSchema(
JsonMetaSchema.builder("https://example.org/schema/meta/example", JsonMetaSchema.getV201909())
.vocabularyFactory(vocabularyFactory)
.unknownKeywordFactory(DisallowUnknownKeywordFactory())
.build()
)
.schemaMappers {
it.mappings(mapOf(
"https://example.org/schema/meta/example" to "classpath:schemas/example-meta-schema.json",
))
}
.build()
}
Input is something like:
{
"properties": {
"FOO": {
"description": "Message type",
"type": "string",
"myType": "FOO"
}
},
"description": "",
"title": "Something",
"$defs": {},
"$id": "https://example.org/another-schema",
"$schema": "https://example.org/schema/meta/example",
"type": "object"
}
I am still getting unknown keyword error (I added DisallowUnknownKeywordFactory to help with debugging). Is the above how I am supposed to be doing this?
from json-schema-validator.
You cannot convert a 2020-12 meta schema into a 2019-09 meta schema just by changing 2020-12 to 2019-09. The keywords aren't the same, for instance $dynamicAnchor
is not supported in 2019-09. In 2019-09 they used $recursiveAnchor
. Also the vocabularies are different. 2019 doesn't use format-annotation
. See https://json-schema.org/draft/2019-09/schema.
The vocabularies are typically associated with the dialect not the actual custom meta schema so that it can load other custom meta schemas that use the vocabulary without explicit configuration. I.e. as per the docs you should configure it on 2019-09 and not https://example.org/schema/meta/example
.
That said I don't see anything other thing obviously wrong so you're going to have to set breakpoints and debug.
from json-schema-validator.
json-schema-validator/src/test/java/com/networknt/schema/VocabularyTest.java
Lines 171 to 217 in 4045e7e
from json-schema-validator.
Hi @justin-tay , I believe I have it working now. Thank you so much for your help!
from json-schema-validator.
Related Issues (20)
- Do the values of allOf need to be validated as arrays?
- Add an option to toggle debug message HOT 1
- Impossible to construct a SchemaLocation containing a json pointer with an escaped fragment
- Impossible to construct a SchemaLocation containing a json pointer with an escaped fragment HOT 1
- EmailValidator limits the user length to be <= 64 HOT 2
- JsonSchemaFactory.builder().defaultMetaSchemaIri() and new Version4().getInstance().getIri()
- [Bug] IRI References with URL encoded '[' and ']' incorrectly fail validation HOT 1
- How to handle loading of relative refs HOT 2
- Could not find OpenApi30 in release 1.4.0 HOT 9
- "http://" should be considered as a valid iri-reference HOT 1
- 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 1
- Exponential algorithm in BaseJsonValidator.hasAdjacentKeywordInEvaluationPath HOT 11
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.