Giter VIP home page Giter VIP logo

json-schema-validator's People

Contributors

dependabot[bot] avatar github-actions[bot] avatar optimumcode avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

json-schema-validator's Issues

Validation issue

Is there an existing issue for this?

  • I have searched the existing issues

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)

Library version

0.0.9

Bug: uri-reference in root `$id` results in incorrect reference resolution

Is there an existing issue for this?

  • I have searched the existing issues

Question

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.

foo.json
request.json

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 ?

Library version

0.0.10

Anything else?

No response

Support remote schema loading

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:

  1. The json-schema-validator project should have an interface (e.g. SchemaProvider) that allows to resolve such schemas
  2. The json-schema-validator project should have a simple implementation that:
    1. Configured with pre-loaded schemas
    2. Provides those schemas by the URI
  3. Another project should be created (e.g. json-schema-remote-provider) and should contain an implementation that can load those schemas from the remote URI

Bug: multipleOf round error

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

ValidationError: 19.99 is not a multiple of 0.01

Expected Behavior

Validation shall pass. Note: values 9.99, 29.99 are passing. It's only 19.99 has an issue.

JSON 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
            }
          }
        }

Library version

0.0.8

Anything else?

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()

Too much validation errors

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:

  • missing required properties: [host]
  • missing required properties: [payment_type]
  • missing required properties: [type]

I think it is supposed to return :

  • missing required properties: [type]

Add format keyword support

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:

  • Provide API
    • Provide an interface to implement a format validator and API to register custom format validators
    • Provide API for switching between assertion and annotation mode for format keyword
  • Implement formats:
    • date
    • time
    • date-time
    • duration
    • email
    • hostname
    • idn-email
    • idn-hostname
    • ipv4
    • ipv6
    • iri
    • iri-reference
    • json-pointer
    • relative-json-pointer
    • regex
    • uri
    • uri-reference
    • uri-template
    • uuid

Each assertion should be loaded independently instead of grouping in one factory

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)

Enhancement: Optimize generated classes with character data

Is there an existing issue for this?

  • I have searched the existing issues

Enhancement description

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
  }
}

Add module-info.java to JVM artifact

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;
}

Register custom Keywords

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!

Correct circled references detection logic

Right now there are some problems in circled references detection logic:

  1. If property name matches one of the applicator keywords that always executes the reference might be reported as circled.
  2. If there is an applicator that might not be always executed the reference still can be reported as circled.

This should be addressed and corrected

The $id should be treated as a URI

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

Add publication to Sonatype repository

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.