Skip to content

Commit

Permalink
Merge pull request #465 from vert-x3/json-schema
Browse files Browse the repository at this point in the history
Json schema
  • Loading branch information
vietj authored Nov 18, 2024
2 parents 8454a25 + e9602f8 commit f48370a
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 1 deletion.
18 changes: 18 additions & 0 deletions json-schema-examples/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
= Vert.x Json Schema examples

Here you will find examples demonstrating Vert.x Json Schema in action.

== Dependencies required

To use Vert.x gRPC in your own Maven or Gradle project add the following dependency

----
Group ID: io.vertx
Artifact ID: vertx-json-schema
----

== Validate Json Against a Schema

This example shows how to load and create a SchemaRepository and how to validate json objects using it.

- link:src/main/java/io/vertx/example/jsonschema/ValidateJson.java[ValidateJson.java]
26 changes: 26 additions & 0 deletions json-schema-examples/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.vertx</groupId>
<artifactId>vertx-examples</artifactId>
<version>5.0.0.CR1</version>
</parent>

<artifactId>json-schema-examples</artifactId>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-json-schema</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package io.vertx.example.jsonschema;

import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.json.schema.*;

public class ValidateJson {

public static String BASE_DIRECTORY = "io/vertx/example/jsonschema/";

/**
*
* @param schema, the JsonSchema that you want to use with your repository.
* @return a SchemaRepository which is used to validate json.
*/
public static SchemaRepository createSchemaRepository(JsonSchema schema) {
return SchemaRepository.create(new JsonSchemaOptions()
// Set the base URI for all schemas, incase the schema you load doesn't contain one by default.
.setBaseUri("https://vertx.io")
// Set the json schema draft that you want to use for validation.
.setDraft(Draft.DRAFT202012)
// Set the output format when validating json objects.
.setOutputFormat(OutputFormat.Basic))
//dereference the schema so that it is ready to be used later on and referenced by the Schema URI.
.dereference(schema);
}

/**
*
* @param vertx instance
* @param fileName that is loaded into a JsonSchema
*/
public static Future<JsonSchema> loadSchema(Vertx vertx, String fileName) {
return loadJsonFromFile(vertx, BASE_DIRECTORY + fileName)
.compose(json -> Future.succeededFuture(JsonSchema.of(json)));
}

/**
*
* @param vertx instance
* @param filePath that is loaded into a JsonObject
*/
public static Future<JsonObject> loadJsonFromFile(Vertx vertx, String filePath) {
return vertx.fileSystem().readFile(filePath)
.compose(buffer -> Future.succeededFuture(buffer.toJsonObject()));
}

/**
*
* @param repository The SchemaRepository that the json object will be validated against.
* @param schema The schema that is used for validation
* @param json to validate
* @return an OutputUnit on whether the validation succeeded or failed.
*/
public static OutputUnit validateJson(SchemaRepository repository, String schemaUri, Object json) {
return repository.validator(schemaUri).validate(json);
}


public static void main(String[] args) {
Vertx vertx = Vertx.vertx();

loadSchema(vertx, "basic_json_schema.json")
.onSuccess(jsonSchema -> {
System.out.println("Successfully loaded json schema.");
SchemaRepository repository = createSchemaRepository(jsonSchema);
System.out.println("Successfully loaded and dereferenced the json schema repository.");
loadJsonFromFile(vertx, BASE_DIRECTORY + "/basic_json.json")
.onSuccess(jsonObject -> {

//Since we previously dereferenced the basic json schema, we can just use the $id defined the
// schema to validate the json object provided.
OutputUnit outputUnit = validateJson(repository, "https://vertx.io/basic.json", jsonObject);
System.out.println("Json validity: " + outputUnit.getValid() + " errors: " + outputUnit.getErrors());
System.exit(0);

})
.onFailure(err -> {
err.printStackTrace();
System.exit(1);
});
})
.onFailure(err -> {
err.printStackTrace();
System.exit(1);
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": "123",
"date": "2025-12-31",
"tags": [
"vertx",
"jsonschema"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"$id": "https://vertx.io/basic.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "A basic json schema to do validations against.",
"type": "object",
"properties": {
"id": {
"type": "string",
"maxLength": 5
},
"date": {
"type": "string",
"format": "date"
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
1 change: 0 additions & 1 deletion openapi-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
</parent>

<artifactId>openapi-examples</artifactId>
<version>4.5.8</version>

<properties>
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<module>web-graphql-examples</module>
<module>openapi-examples</module>
<module>jpms-examples</module>
<module>json-schema-examples</module>
</modules>

<build>
Expand Down

0 comments on commit f48370a

Please sign in to comment.