Skip to content

JSON Schema Validator for the JVM, implements draft2020-12 (under development)

License

Notifications You must be signed in to change notification settings

jakesmolka/json-sKema

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json-sKema

json-Skema is a Json Schema validator library for the Java Virtual Machine. It implements the draft 2020-12 specification.

Are you new to JSON Schema? Get started with Understanding JSON Schema!

Installation

Maven

Add the following dependency to the <dependencies> section of your project:

<dependency>
    <groupId>com.github.erosb</groupId>
    <artifactId>json-sKema</artifactId>
    <version>0.7.0</version>
</dependency>

Gradle

dependencies {
    implementation("com.github.erosb:json-sKema:0.7.0")
}

Usage

Hello-world

// parse the schema JSON as string
JsonValue  schemaJson = new JsonParser("""
        {
            "type": "object",
            "properties": {
                "age": {
                    "type": "number",
                    "minimum": 0
                },
                "name": {
                    "type": "string"
                }
            }
        }
        """).parse();
// map the raw json to a reusable Schema instance
Schema schema = new SchemaLoader(schemaJson).load();

// create a validator instance for each validation (one-time use object) 
Validator validator = Validator.forSchema(schema);

// parse the input instance to validate against the schema
JsonValue instance = new JsonParser("""
        {
            "age": -5,
            "name": null
        }
        """).parse();

// run the validation
ValidationFailure failure = validator.validate(instance);

// print the validation failures (if any)
System.out.println(failure);

Loading a schema file from URL

// HTTP protocol is also supported
Schema schema = SchemaLoader.forURL("classpath:///path/to/your/schema.json").load();

// create a validator instance for each validation (one-time use object) 
Validator validator = Validator.forSchema(schema);
// ...

Compatibility notes

The library implements the JSON Schema draft 2020-12 core and validation specifications, with the following notes:

  • $dynamicAnchor and $dynamicRef support is partially implemented

"format" support

The library currently has built-in support for the following "format" values defined in the specification:

"format" Supported? Notes
date Yes
date-time Yes Non-UTC values with leap seconds not supported
time Yes Leap seconds not supported
duration Partially
email Yes IPV6 domain parts not supported
uri Yes
ipv4 Yes
ipv6 Yes
uuid Yes

The following formats are NOT supported: hostname, idn-email, idn-hostname, iri, iri-reference, json-pointer, regex, relative-json-pointer, uri-reference, uri-template .

Support for older JSON Schema drafts

This project is the successor of everit-org/json-schema. If you want to use draft-04, draft-06 or draft-07 versions of JSON Schema, then you can use the everit library.

Contribution guideline

Local environment setup:

Prerequisite: JDK and Maven installed

git clone https://github.com/erosb/json-sKema.git
cd json-sKema
git submodule init
git submodule update

Building the project:

mvn clean package

Building the project and running the official test suite:

Test annotated with @Tag("acceptance") require the test suite to be pulled using:

git submodule update --init --recursive

Then run the tests:

mvn clean verify

Building the project without running the official test suite:

mvn clean package -Dgroups='!acceptance'

About

JSON Schema Validator for the JVM, implements draft2020-12 (under development)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Kotlin 100.0%