Comments (5)
That actually depends on what you define to be a valid schema.
You would typically perform validation of a schema by using it's meta-schema. This only validates it's structure though, for instance for a $ref
it would only just check if it is a valid uri_reference
format
and to figure out if the $ref
can resolve, you would need to actually load the schema.
Here however, from a meta-schema perspective, your example schema is valid, as it would allow unknown keywords. You may consider a custom meta-schema eg. using unevaluatedProperties: false
or something to that effect or you could add extra restrictions when loading the schema.
The following validates the schema according to the meta-schema, and it's valid due to how the meta-schema is defined.
package com.networknt.schema;
import org.junit.jupiter.api.Test;
import com.networknt.schema.SpecVersion.VersionFlag;
public class Issue996Test {
@Test
void metaSchemaValidation() {
String instanceData = "{\r\n"
+ " \"lol\": 420\r\n"
+ "}";
SchemaValidatorsConfig config = new SchemaValidatorsConfig();
config.setPathType(PathType.JSON_POINTER);
config.setFormatAssertionsEnabled(true);
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(SchemaLocation.of(SchemaId.V202012), config);
System.out.println(schema.validate(instanceData, InputFormat.JSON, OutputFormat.HIERARCHICAL, executionContext -> {
executionContext.getExecutionConfig().setAnnotationCollectionEnabled(true);
executionContext.getExecutionConfig().setAnnotationCollectionFilter(keyword -> true);
}));
}
}
You can also load the schema with additional restrictions after validating against the meta-schema. This for instance doesn't allow unknown keywords or unknown formats. You need to initializeValidators
to check if all the $ref
resolve.
package com.networknt.schema;
import org.junit.jupiter.api.Test;
import com.networknt.schema.SpecVersion.VersionFlag;
public class Issue996Test {
@Test
void test() {
String instanceData = "{\r\n"
+ " \"lol\": 420\r\n"
+ "}";
JsonMetaSchema metaSchema = JsonMetaSchema.builder(JsonMetaSchema.getV202012())
.unknownKeywordFactory(DisallowUnknownKeywordFactory.getInstance())
.build();
SchemaValidatorsConfig config = new SchemaValidatorsConfig();
config.setStrict("format", true);
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012, builder -> builder.metaSchema(metaSchema));
JsonSchema schema = factory.getSchema(instanceData, config);
schema.initializeValidators();
}
}
from json-schema-validator.
That's not really possible with the current code. It's not really designed as a linter. You can send a PR if you think you can add this without affecting the validation performance.
There are many reasons why a schema may be potentially invalid. According to the spec, unknown keywords are completely acceptable, they are just treated as annotation keywords, so you can see that what is valid depends on interpretation. You might also want to restrict the schemas to only use particular draft versions also for instance.
You would likely need to design a custom meta-schema implementation with custom validators if you want to do this. You would need custom validators because JSON Schema actually doesn't have built in validators for doing validation of data, for instance if you want to validate if minProperties
is always less than maxProperties
that can't be done with any standard keyword. Also validating whether a $ref
is resolvable is non-trivial without doing the load.
from json-schema-validator.
This is really helpful @justin-tay
Thank you very much. I will try it
from json-schema-validator.
Hello again,
Your second solution is really great. I can catch the exception and retrieve a message like
Keyword '$ids' is unknown and must be configured on the meta-schema or vocabulary
.
Now I do not want to get picky here, but in case of multiple typos like "$ids" and "$schemas" I would only get the first key which is not according to the meta schema. Could there be a way to "collect" all errors?
from json-schema-validator.
Okay, I think this is not really a big issue for us now. It is okay as it is. I just wanted to actively ask.
Thank you for all your help!
from json-schema-validator.
Related Issues (20)
- JSON Schema validation failed against custom JSON Meta Schema after update 1.2.0 -> 1.3.0 HOT 2
- 1.4.0: Is there a way to perform local verification? HOT 5
- Recursive calls leading to an OOM when parsing a valid schema HOT 6
- Bidi rule validation implementation does not match the specification HOT 3
- Additional i18n language(spanish) support related (#847) HOT 8
- We Need Additional Spanish Language support can you please add the spanish langauge our customers need it we already in PROD HOT 7
- Schema from String HOT 1
- change example.com connection error IOException
- improve outputUnits with all assertion fields HOT 3
- V1.4.0 ValidatorTypeCode.getKeywords(SpecVersion.VersionFlag.V201909) has missing keywords HOT 2
- Error occurs if OneOf and AllOf are nested HOT 10
- 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()
- Unknown keyword warning when using meta schema from file HOT 17
- [Bug] IRI References with URL encoded '[' and ']' incorrectly fail validation HOT 1
- How to handle loading of relative refs 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.