diff --git a/json-schema-examples/README.adoc b/json-schema-examples/README.adoc new file mode 100644 index 000000000..ca1f800e4 --- /dev/null +++ b/json-schema-examples/README.adoc @@ -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] diff --git a/json-schema-examples/pom.xml b/json-schema-examples/pom.xml new file mode 100644 index 000000000..d7f7e9e7c --- /dev/null +++ b/json-schema-examples/pom.xml @@ -0,0 +1,26 @@ + + + 4.0.0 + + io.vertx + vertx-examples + 5.0.0.CR1 + + + json-schema-examples + + + UTF-8 + + + + + io.vertx + vertx-json-schema + ${project.version} + + + + diff --git a/json-schema-examples/src/main/java/io/vertx/example/jsonschema/ValidateJson.java b/json-schema-examples/src/main/java/io/vertx/example/jsonschema/ValidateJson.java new file mode 100644 index 000000000..b18d69cb0 --- /dev/null +++ b/json-schema-examples/src/main/java/io/vertx/example/jsonschema/ValidateJson.java @@ -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 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 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); + }); + } + +} diff --git a/json-schema-examples/src/main/resources/io/vertx/example/jsonschema/basic_json.json b/json-schema-examples/src/main/resources/io/vertx/example/jsonschema/basic_json.json new file mode 100644 index 000000000..44d2947fa --- /dev/null +++ b/json-schema-examples/src/main/resources/io/vertx/example/jsonschema/basic_json.json @@ -0,0 +1,8 @@ +{ + "id": "123", + "date": "2025-12-31", + "tags": [ + "vertx", + "jsonschema" + ] +} diff --git a/json-schema-examples/src/main/resources/io/vertx/example/jsonschema/basic_json_schema.json b/json-schema-examples/src/main/resources/io/vertx/example/jsonschema/basic_json_schema.json new file mode 100644 index 000000000..64d5c09a7 --- /dev/null +++ b/json-schema-examples/src/main/resources/io/vertx/example/jsonschema/basic_json_schema.json @@ -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" + } + } + } +} diff --git a/openapi-examples/pom.xml b/openapi-examples/pom.xml index 5ea2c717f..5cf269c27 100644 --- a/openapi-examples/pom.xml +++ b/openapi-examples/pom.xml @@ -27,7 +27,6 @@ openapi-examples - 4.5.8 2.22.2 diff --git a/pom.xml b/pom.xml index f28973c72..a0912cbee 100644 --- a/pom.xml +++ b/pom.xml @@ -46,6 +46,7 @@ web-graphql-examples openapi-examples jpms-examples + json-schema-examples