-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #465 from vert-x3/json-schema
Json schema
- Loading branch information
Showing
7 changed files
with
165 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
90 changes: 90 additions & 0 deletions
90
json-schema-examples/src/main/java/io/vertx/example/jsonschema/ValidateJson.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
json-schema-examples/src/main/resources/io/vertx/example/jsonschema/basic_json.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"id": "123", | ||
"date": "2025-12-31", | ||
"tags": [ | ||
"vertx", | ||
"jsonschema" | ||
] | ||
} |
22 changes: 22 additions & 0 deletions
22
json-schema-examples/src/main/resources/io/vertx/example/jsonschema/basic_json_schema.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters