optimumcode / json-schema-validator Goto Github PK
View Code? Open in Web Editor NEWThe JSON schema validation library that works with https://github.com/Kotlin/kotlinx.serialization
License: MIT License
The JSON schema validation library that works with https://github.com/Kotlin/kotlinx.serialization
License: MIT License
Schema and json here
Validation passes with online validator
import io.github.optimumcode.json.schema.JsonSchema
import io.github.optimumcode.json.schema.ValidationError
import io.github.optimumcode.json.schema.fromStream
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import java.io.File
fun main() {
val schema = JsonSchema.fromStream(File("/home/elect/Downloads/SCHEMA.json").inputStream())
val errors = mutableListOf<ValidationError>()
val elementToValidate: JsonElement = Json.parseToJsonElement(File("/home/elect/Downloads/DATASETS.json").readText())
println(schema.validate(elementToValidate, errors::add))
errors.forEach { println(it) }
false
ValidationError(schemaPath=/type, objectPath=, message=element is not a object, details={}, absoluteLocation=null)
0.0.9
Add corresponding configuration files to the repository so it can be used in tools like GitHub Codespaces or DevPod
Hello,
We are currently trying to use your library with KMM in order to validate multiple JSON schemas which use references ($ref), see the following example.
But we did not manage to use absolute path "/myproject/enums/foo" nor relative paths "enums/foo"
We ended up using myproject://enums/foo
for the ids and refs, which is working fine but does not look like the best / appropriate solution.
We wanted to check with you if it might be a bug in the library or if we are misunderstanding the JSON Schema references/id usage ?
0.0.10
No response
There is an official JSON schema repository with test suites for schema validations
https://github.com/json-schema-org/JSON-Schema-Test-Suite
Those test suites should be integrated into the build process
Currently, library supports $vocabulary
keyword only inside the current schema. It should support vocabulary specified in the custom meta-schema
The library should support $schema property in schema definition to identify the draft version it should use to load the schema.
If the $schema is missing the latest supported draft should be used
The main JSON schema might reference other schemas. The library should provide the ability to provide those schemas.
From my point of view, this should be done in the following way:
json-schema-validator
project should have an interface (e.g. SchemaProvider
) that allows to resolve such schemasjson-schema-validator
project should have a simple implementation that:
json-schema-remote-provider
) and should contain an implementation that can load those schemas from the remote URIProject must have automated checks and release actions
ValidationError: 19.99 is not a multiple of 0.01
Validation shall pass. Note: values 9.99
, 29.99
are passing. It's only 19.99 has an issue.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "multipleOf round error reproducer",
"required": ["price"],
"type": "object",
"properties": {
"price": {
"description": "Product price",
"type": "number",
"minimum": 0,
"multipleOf": 0.01
}
}
}
0.0.8
Full reproducer
val jsonStr = """
{
"price": 19.99
}
""".trimIndent()
val schema = """
{
"${"$"}schema": "http://json-schema.org/draft-07/schema#",
"description": "multipleOf round error reproducer",
"required": ["price"],
"type": "object",
"properties": {
"price": {
"description": "Product price",
"type": "number",
"minimum": 0,
"multipleOf": 0.01
}
}
}
""".trimIndent()
val jsonSchema = JsonSchema.fromDefinition(schema)
val jsonElement = Json.parseToJsonElement(jsonStr)
val errors = mutableListOf<ValidationError>()
jsonSchema.validate(jsonElement, errors::add)
assertThat(errors).isEmpty()
I faced an issue when using this library.
I have an object named "type" marked as required. When it didn't appear in the JSON object to be validated i am expecting only an error mentioning the missing object. But, in addition to this error, some other errors related to conditions from the block of "allOf" based on the missing object("type") are also thrown also. It is supposed to return only the error of the missing object "type".
My JSON schema:
"title": "resource",
"type": "object",
"required": [
"type"
],
...
"allOf": [
{
"if": {
"properties": {
"type": {
"enum": [
"enum1",
"enum2"
]
}
}
},
"then": {
"properties": {
"host": {
"properties": {
.
.
.
}
}
}
},
"required": [
"host"
]
}
},
{
"if": {
"properties": {
"type": {
"enum": [
"enum1",
"enum2",
"enum3",
"enum4",
]
}
}
},
"then": {
"required": [
"amount"
]
}
},
{
"if": {
"properties": {
"type": {
"enum": [
"enum5"
]
}
}
},
"then": {
"properties": {
.
.
.
},
"required": [
"payment_type"
]
}
}
]
json to be validated:
"resource": {}
Returned errors:
I think it is supposed to return :
Add support for format keyword annotations and assertions as described in JSON schema spec.
I have decided to change the approach: supporting all the formats defined in the spec is a huge amount of work. Because of that this task will be split into several small sub-tasks:
Currently, assertions for properties (properties, patternProperties, additionalProperties), arrays (items, additonalItems) and condition (if, then, else) are loaded by a single factory (for each group).
Because of that the factory must load all of them and conditionally create an assertion that choose which of those "sub-assertions" to use.
This makes its implementation less obvious (because each assertion must able to be referenced).
The idea is to split each assertion in its own factory and create a group assertion that will execute them in the required order (because the order does matter in this case)
Required templates:
Add support for JSON schema Draft 2020-12
PR must have at least one label to specify its category. This can be used to implement that
Update actions in workflows
When release is published:
It is a good practice to show how your library performs. To show that the benchmark in the repository that can be executed by anyone would be the best option.
This library can be used for benchmarking.
Libraries to compare:
Draft 8 set of specs: https://json-schema.org/specification-links.html#draft-2019-09-formerly-known-as-draft-8
Currently generated classes with character data check if the codepoint belongs to this class by using some kind of binary search between the ranges this class has. This causes O(log(n)) complexity to find if the class contains the codepoint in the worst case.
I think this can be optimized in the following way:
We can divide ranges into sub-groups by applying a shift-right bit operation (codepoint >> 16). This will group ranges based on their higher bits. As a result, we will have smaller groups of ranges to apply our binary search and we will be able to filter out codepoints that cannot belong to the class by comparing the result of a shift-right operation with known values for this class.
fun contains(codepoint: Int): Boolean {
if (codepoint < minCodepoint || codepoint > maxCodepoint) {
return false
}
return when (codepoint shr 8) {
in 0x0..0x2 -> // binary search in this group
0x10 -> // binary search in this group
else -> false
}
}
It is required to add the module-info.java file into the JVM artifact in order to correctly work with the Java projects that use Java Platform Module System (JPMS).
module io.github.optimumcode.json {
requires transitive kotlin.stdlib;
requires transitive kotlinx.serialization.core;
requires kotlinx.serialization.json;
exports io.github.optimumcode.json.pointer;
exports io.github.optimumcode.json.schema;
}
Update dependencies and build.gradle to support kotlin 1.9
GitHub provides an API to see the dependencies used in the project.
However, it does not work automatically with Gradle dependencies. A separate action must be added in order to export the dependencies
https://github.com/marketplace/actions/gradle-dependency-submission
Hello,
According to the official documentation of JSON Schema , the standardized place to put subschemas should be "$defs", it is not currently supported by the library (it seems to only support "definitions" section)
Update required to fix the vulnerability:
https://github.com/OptimumCode/json-schema-validator/security/dependabot/2
It would be great to be able to register custom keywords.
Playing around with this library I was able to easily create a custom keyword, but I have no way to register it.
Thanks!
Right now there are some problems in circled references detection logic:
This should be addressed and corrected
The current version treats the $id as a simple string. It is not right and won't allow to correctly use $ref later.
The behavior should be changed to treat $id as an URI and resolve $ref as URI as well
The project should be published to the Sonatype repository as io.github.optimumcode:json-schema-validator
. All artifacts for other platforms must be published as well
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.