From 729405102ff601391d2c3d1a9ac7b3fb8c6c1518 Mon Sep 17 00:00:00 2001 From: Sebastian Toepfer <61313468+sebastian-toepfer@users.noreply.github.com> Date: Tue, 30 Jan 2024 13:27:03 +0100 Subject: [PATCH] make instancetype a jsonvalue this allow to use instancetype to define a schema like: ...add("type", InstanceType.STRING) instead of ...add("type", "string") or ...add("type", InstanceType.STRING.toString()) --- .../jsonschema/InstanceType.java | 18 +++++++++++++++++- .../jsonschema/InstanceTypeTest.java | 11 +++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/io/github/sebastiantoepfer/jsonschema/InstanceType.java b/api/src/main/java/io/github/sebastiantoepfer/jsonschema/InstanceType.java index 8d72ec7a..fdb84a6c 100644 --- a/api/src/main/java/io/github/sebastiantoepfer/jsonschema/InstanceType.java +++ b/api/src/main/java/io/github/sebastiantoepfer/jsonschema/InstanceType.java @@ -24,6 +24,7 @@ package io.github.sebastiantoepfer.jsonschema; import jakarta.json.JsonNumber; +import jakarta.json.JsonString; import jakarta.json.JsonValue; import java.math.BigDecimal; import java.util.Arrays; @@ -33,7 +34,7 @@ /** * see: http://json-schema.org/draft/2020-12/json-schema-core.html#name-instance-data-model */ -public enum InstanceType { +public enum InstanceType implements JsonString { NULL(JsonValue.ValueType.NULL), BOOLEAN(JsonValue.ValueType.TRUE, JsonValue.ValueType.FALSE), OBJECT(JsonValue.ValueType.OBJECT), @@ -74,6 +75,21 @@ public boolean isInstance(final JsonValue value) { @Override public String toString() { + return getString(); + } + + @Override + public CharSequence getChars() { + return getString(); + } + + @Override + public String getString() { return name().toLowerCase(Locale.US); } + + @Override + public ValueType getValueType() { + return JsonValue.ValueType.STRING; + } } diff --git a/api/src/test/java/io/github/sebastiantoepfer/jsonschema/InstanceTypeTest.java b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/InstanceTypeTest.java index 8fb737e6..1acff83b 100644 --- a/api/src/test/java/io/github/sebastiantoepfer/jsonschema/InstanceTypeTest.java +++ b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/InstanceTypeTest.java @@ -24,6 +24,7 @@ package io.github.sebastiantoepfer.jsonschema; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasToString; import static org.hamcrest.Matchers.is; import static org.junit.jupiter.params.provider.Arguments.arguments; @@ -94,6 +95,16 @@ static Stream provideWithName() { ); } + @Test + void should_be_from_json_type_string() { + assertThat(InstanceType.INTEGER.getValueType(), is(JsonValue.ValueType.STRING)); + } + + @Test + void should_retrun_his_value_as_charsquence() { + assertThat(InstanceType.INTEGER.getChars(), hasToString("integer")); + } + @Test void should_be_createable_from_lowercase_value() { assertThat(InstanceType.fromString("object"), is(InstanceType.OBJECT));