From fb0df0b683d0f45972c3274629be24c6439defa4 Mon Sep 17 00:00:00 2001 From: Sebastian Toepfer <61313468+sebastian-toepfer@users.noreply.github.com> Date: Sat, 13 Jan 2024 13:03:13 +0100 Subject: [PATCH 1/2] improve instancetype --- .../jsonschema/InstanceType.java | 18 +++-- .../jsonschema/InstanceTypeTest.java | 69 ++++++++++++++----- 2 files changed, 63 insertions(+), 24 deletions(-) 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 aa7f4608..8d72ec7a 100644 --- a/api/src/main/java/io/github/sebastiantoepfer/jsonschema/InstanceType.java +++ b/api/src/main/java/io/github/sebastiantoepfer/jsonschema/InstanceType.java @@ -28,6 +28,7 @@ import java.math.BigDecimal; import java.util.Arrays; import java.util.Collection; +import java.util.Locale; /** * see: http://json-schema.org/draft/2020-12/json-schema-core.html#name-instance-data-model @@ -41,13 +42,7 @@ public enum InstanceType { INTEGER() { @Override public boolean isInstance(final JsonValue value) { - final boolean result; - if (value instanceof JsonNumber nr) { - result = isIntegral(nr.bigDecimalValue()); - } else { - result = false; - } - return result; + return value instanceof JsonNumber nr && isIntegral(nr.bigDecimalValue()); } @SuppressWarnings("BigDecimalEquals") @@ -62,6 +57,10 @@ private boolean isIntegral(final BigDecimal decimal) { }, STRING(JsonValue.ValueType.STRING); + public static InstanceType fromString(final String name) { + return valueOf(name.toUpperCase(Locale.US)); + } + @SuppressWarnings("ImmutableEnumChecker") private final Collection validTypes; @@ -72,4 +71,9 @@ private boolean isIntegral(final BigDecimal decimal) { public boolean isInstance(final JsonValue value) { return validTypes.contains(value.getValueType()); } + + @Override + public String toString() { + return name().toLowerCase(Locale.US); + } } 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 3cf70391..8fb737e6 100644 --- a/api/src/test/java/io/github/sebastiantoepfer/jsonschema/InstanceTypeTest.java +++ b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/InstanceTypeTest.java @@ -25,10 +25,13 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.params.provider.Arguments.arguments; import jakarta.json.Json; import jakarta.json.JsonValue; import java.util.stream.Stream; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -43,15 +46,15 @@ void should_be_true_if_instancetype_is_correct(final InstanceType instanceType, static Stream provideWithValidCombinations() { return Stream.of( - Arguments.of(InstanceType.NULL, JsonValue.NULL), - Arguments.of(InstanceType.BOOLEAN, JsonValue.FALSE), - Arguments.of(InstanceType.BOOLEAN, JsonValue.TRUE), - Arguments.of(InstanceType.OBJECT, JsonValue.EMPTY_JSON_OBJECT), - Arguments.of(InstanceType.ARRAY, JsonValue.EMPTY_JSON_ARRAY), - Arguments.of(InstanceType.NUMBER, Json.createValue(23L)), - Arguments.of(InstanceType.INTEGER, Json.createValue(23L)), - Arguments.of(InstanceType.INTEGER, Json.createValue(10L)), - Arguments.of(InstanceType.INTEGER, Json.createValue(0.0)) + arguments(InstanceType.NULL, JsonValue.NULL), + arguments(InstanceType.BOOLEAN, JsonValue.FALSE), + arguments(InstanceType.BOOLEAN, JsonValue.TRUE), + arguments(InstanceType.OBJECT, JsonValue.EMPTY_JSON_OBJECT), + arguments(InstanceType.ARRAY, JsonValue.EMPTY_JSON_ARRAY), + arguments(InstanceType.NUMBER, Json.createValue(23L)), + arguments(InstanceType.INTEGER, Json.createValue(23L)), + arguments(InstanceType.INTEGER, Json.createValue(10L)), + arguments(InstanceType.INTEGER, Json.createValue(0.0)) ); } @@ -63,14 +66,46 @@ void should_be_false_if_instancetype_is_correct(final InstanceType instanceType, static Stream provideWithInvalidCombinations() { return Stream.of( - Arguments.of(InstanceType.NULL, JsonValue.TRUE), - Arguments.of(InstanceType.BOOLEAN, JsonValue.EMPTY_JSON_OBJECT), - Arguments.of(InstanceType.BOOLEAN, JsonValue.EMPTY_JSON_ARRAY), - Arguments.of(InstanceType.OBJECT, JsonValue.EMPTY_JSON_ARRAY), - Arguments.of(InstanceType.ARRAY, JsonValue.TRUE), - Arguments.of(InstanceType.NUMBER, Json.createValue("string")), - Arguments.of(InstanceType.INTEGER, Json.createValue(23.2)), - Arguments.of(InstanceType.INTEGER, Json.createValue("test")) + arguments(InstanceType.NULL, JsonValue.TRUE), + arguments(InstanceType.BOOLEAN, JsonValue.EMPTY_JSON_OBJECT), + arguments(InstanceType.BOOLEAN, JsonValue.EMPTY_JSON_ARRAY), + arguments(InstanceType.OBJECT, JsonValue.EMPTY_JSON_ARRAY), + arguments(InstanceType.ARRAY, JsonValue.TRUE), + arguments(InstanceType.NUMBER, Json.createValue("string")), + arguments(InstanceType.INTEGER, Json.createValue(23.2)), + arguments(InstanceType.INTEGER, Json.createValue("test")) ); } + + @ParameterizedTest + @MethodSource("provideWithName") + void should_return_his_name(final InstanceType instanceType, final String value) { + assertThat(instanceType, Matchers.hasToString(value)); + } + + static Stream provideWithName() { + return Stream.of( + arguments(InstanceType.NULL, "null"), + arguments(InstanceType.BOOLEAN, "boolean"), + arguments(InstanceType.OBJECT, "object"), + arguments(InstanceType.ARRAY, "array"), + arguments(InstanceType.NUMBER, "number"), + arguments(InstanceType.INTEGER, "integer") + ); + } + + @Test + void should_be_createable_from_lowercase_value() { + assertThat(InstanceType.fromString("object"), is(InstanceType.OBJECT)); + } + + @Test + void should_be_createable_from_uppercase_value() { + assertThat(InstanceType.fromString("NUMBER"), is(InstanceType.NUMBER)); + } + + @Test + void should_be_createable_from_mixcase_value() { + assertThat(InstanceType.fromString("Null"), is(InstanceType.NULL)); + } } From 4d59f73a06afb5cea372bcc720dc52861935df0b Mon Sep 17 00:00:00 2001 From: Sebastian Toepfer <61313468+sebastian-toepfer@users.noreply.github.com> Date: Sat, 13 Jan 2024 13:18:54 +0100 Subject: [PATCH 2/2] make it printable a json schema can now be created from a json object and converted back into it in two ways. 1. JsonSchemas.load(jsonObject).asJsonObject(); 2. JsonSchemas.load(jsonObject).printOn(new JsonObjectMedia()); the second way also enables conversion to a hash map or another media. --- api/pom.xml | 14 ++++++ .../jsonschema/JsonSchema.java | 3 +- .../jsonschema/keyword/Keyword.java | 3 +- .../jsonschema/keyword/StaticAnnotation.java | 8 ++++ api/src/main/java/module-info.java | 2 + .../jsonschema/FakeJsonSchemaFactory.java | 6 +++ .../jsonschema/JsonSubSchemaTest.java | 10 +++- .../jsonschema/keyword/AnnotationTest.java | 10 +++- .../jsonschema/keyword/ApplicatorTest.java | 10 +++- .../jsonschema/keyword/AssertionTest.java | 10 +++- .../jsonschema/keyword/IdentifierTest.java | 8 +++- .../keyword/ReservedLocationTest.java | 8 +++- .../keyword/StaticAnnotationTest.java | 45 ++++++++++++++++++ api/src/test/java/module-info.java | 3 ++ core/pom.xml | 19 ++++++++ .../core/DefaultJsonObjectSchema.java | 6 +++ .../jsonschema/core/DefaultJsonSubSchema.java | 6 +++ .../jsonschema/core/EmptyJsonSchema.java | 6 +++ .../jsonschema/core/FalseJsonSchema.java | 6 +++ .../jsonschema/core/TrueJsonSchema.java | 6 +++ .../core/codition/AnyOfCondition.java | 42 ----------------- .../core/codition/OfTypeCondition.java | 41 ----------------- .../AdditionalPropertiesKeywordType.java | 18 ++++++-- .../vocab/applicator/ItemsKeywordType.java | 6 +++ .../PatternPropertiesKeywordType.java | 26 +++++++---- .../applicator/PrefixItemsKeywordType.java | 6 +++ .../applicator/PropertiesKeywordType.java | 18 ++++++++ .../core/vocab/core/DefsKeywordType.java | 6 +++ .../vocab/core/DynamicRefKeywordType.java | 7 +++ .../core/vocab/core/IdKeywordType.java | 6 +++ .../core/vocab/core/RefKeywordType.java | 6 +++ .../core/vocab/core/SchemaKeywordType.java | 6 +++ .../vocab/core/VocabularyKeywordType.java | 7 +++ .../vocab/validation/EnumKeywordType.java | 9 ++++ .../ExclusiveMaximumKeywordType.java | 6 +++ .../ExclusiveMinimumKeywordType.java | 6 +++ .../vocab/validation/MaxItemsKeywordType.java | 18 +++++--- .../validation/MaxLengthKeywordType.java | 13 ++++-- .../vocab/validation/MaximumKeywordType.java | 6 +++ .../vocab/validation/MinItemsKeywordType.java | 18 +++++--- .../validation/MinLengthKeywordType.java | 6 +++ .../vocab/validation/MinimumKeywordType.java | 6 +++ .../validation/MultipleOfKeywordType.java | 6 +++ .../vocab/validation/PatternKeywordType.java | 6 +++ .../vocab/validation/RequiredKeywordType.java | 20 ++++++-- .../vocab/validation/TypeKeywordType.java | 46 +++++++++++-------- .../validation/UniqueItemsKeywordType.java | 6 +++ core/src/main/java/module-info.java | 2 + .../core/AbstractJsonValueSchemaTest.java | 6 +++ .../core/DefaultJsonObjectSchemaTest.java | 9 ++++ .../core/DefaultJsonSubSchemaTest.java | 14 ++++++ .../jsonschema/core/EmptyJsonSchemaTest.java | 8 ++++ .../jsonschema/core/TrueJsonSchemaTest.java | 8 ++++ .../AdditionalPropertiesKeywordTypeTest.java | 19 ++++++++ .../PatternPropertiesKeywordTypeTest.java | 19 ++++++++ .../PrefixItemsKeywordTypeTest.java | 23 ++++++++++ .../applicator/PropertiesKeywordTypeTest.java | 23 ++++++++++ .../core/vocab/core/DefsKeywordTypeTest.java | 17 +++++++ .../vocab/core/DynamicRefKeywordTypeTest.java | 17 +++++++ .../core/vocab/core/IdKeywordTypeTest.java | 21 +++++++++ .../core/vocab/core/RefKeywordTypeTest.java | 16 +++++++ .../vocab/core/SchemaKeywordTypeTest.java | 21 +++++++++ .../vocab/core/VocabularyKeywordTypeTest.java | 34 ++++++++++++++ .../vocab/validation/EnumKeywordTypeTest.java | 22 +++++++++ .../ExclusiveMaximumKeywordTypeTest.java | 17 +++++++ .../ExclusiveMinimumKeywordTypeTest.java | 17 +++++++ .../validation/MaxItemsKeywordTypeTest.java | 17 +++++++ .../validation/MaxLengthKeywordTypeTest.java | 17 +++++++ .../validation/MaximumKeywordTypeTest.java | 17 +++++++ .../validation/MinItemsKeywordTypeTest.java | 17 +++++++ .../validation/MinLengthKeywordTypeTest.java | 17 +++++++ .../validation/MinimumKeywordTypeTest.java | 17 +++++++ .../validation/MultipleOfKeywordTypeTest.java | 16 +++++++ .../validation/PatternKeywordTypeTest.java | 21 +++++++++ .../validation/RequiredKeywordTypeTest.java | 22 +++++++++ .../UniqueItemsKeywordTypeTest.java | 16 +++++++ core/src/test/java/module-info.java | 3 ++ pom.xml | 18 ++++++++ 78 files changed, 917 insertions(+), 150 deletions(-) delete mode 100644 core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/codition/AnyOfCondition.java delete mode 100644 core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/codition/OfTypeCondition.java diff --git a/api/pom.xml b/api/pom.xml index d9d19f5c..559f2ae0 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -12,6 +12,15 @@ Json Schema :: api + + io.github.sebastian-toepfer.ddd + common + + + io.github.sebastian-toepfer.ddd + media-json-api + + org.junit.jupiter junit-jupiter-api @@ -33,6 +42,11 @@ hamcrest test + + io.github.sebastian-toepfer.ddd + media-core + test + jakarta.json diff --git a/api/src/main/java/io/github/sebastiantoepfer/jsonschema/JsonSchema.java b/api/src/main/java/io/github/sebastiantoepfer/jsonschema/JsonSchema.java index 2240d3fa..16b71aee 100644 --- a/api/src/main/java/io/github/sebastiantoepfer/jsonschema/JsonSchema.java +++ b/api/src/main/java/io/github/sebastiantoepfer/jsonschema/JsonSchema.java @@ -23,11 +23,12 @@ */ package io.github.sebastiantoepfer.jsonschema; +import io.github.sebastiantoepfer.ddd.common.Printable; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.JsonValue; import java.util.Optional; -public interface JsonSchema extends JsonValue { +public interface JsonSchema extends JsonValue, Printable { Validator validator(); Optional keywordByName(String name); diff --git a/api/src/main/java/io/github/sebastiantoepfer/jsonschema/keyword/Keyword.java b/api/src/main/java/io/github/sebastiantoepfer/jsonschema/keyword/Keyword.java index 40e55913..58a978d6 100644 --- a/api/src/main/java/io/github/sebastiantoepfer/jsonschema/keyword/Keyword.java +++ b/api/src/main/java/io/github/sebastiantoepfer/jsonschema/keyword/Keyword.java @@ -23,12 +23,13 @@ */ package io.github.sebastiantoepfer.jsonschema.keyword; +import io.github.sebastiantoepfer.ddd.common.Printable; import java.util.Collection; /** * see: https://json-schema.org/draft/2020-12/json-schema-core.html#name-json-schema-objects-and-key **/ -public interface Keyword { +public interface Keyword extends Printable { default Identifier asIdentifier() { return (Identifier) this; } diff --git a/api/src/main/java/io/github/sebastiantoepfer/jsonschema/keyword/StaticAnnotation.java b/api/src/main/java/io/github/sebastiantoepfer/jsonschema/keyword/StaticAnnotation.java index fa8b7559..88eb9a31 100644 --- a/api/src/main/java/io/github/sebastiantoepfer/jsonschema/keyword/StaticAnnotation.java +++ b/api/src/main/java/io/github/sebastiantoepfer/jsonschema/keyword/StaticAnnotation.java @@ -23,6 +23,9 @@ */ package io.github.sebastiantoepfer.jsonschema.keyword; +import io.github.sebastiantoepfer.ddd.common.Media; +import io.github.sebastiantoepfer.ddd.media.json.JsonObjectPrintable; +import jakarta.json.Json; import jakarta.json.JsonValue; import java.util.Objects; @@ -51,4 +54,9 @@ public boolean hasName(final String name) { public JsonValue valueFor(final JsonValue instance) { return value; } + + @Override + public > T printOn(final T media) { + return new JsonObjectPrintable(Json.createObjectBuilder().add(name, value).build()).printOn(media); + } } diff --git a/api/src/main/java/module-info.java b/api/src/main/java/module-info.java index 9ead3edd..d66a3570 100644 --- a/api/src/main/java/module-info.java +++ b/api/src/main/java/module-info.java @@ -27,5 +27,7 @@ exports io.github.sebastiantoepfer.jsonschema.keyword; exports io.github.sebastiantoepfer.jsonschema.spi; + requires io.github.sebastiantoepfer.ddd.common; + requires io.github.sebastiantoepfer.ddd.media.json; requires jakarta.json; } diff --git a/api/src/test/java/io/github/sebastiantoepfer/jsonschema/FakeJsonSchemaFactory.java b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/FakeJsonSchemaFactory.java index 42f0c41a..a05855f3 100644 --- a/api/src/test/java/io/github/sebastiantoepfer/jsonschema/FakeJsonSchemaFactory.java +++ b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/FakeJsonSchemaFactory.java @@ -23,6 +23,7 @@ */ package io.github.sebastiantoepfer.jsonschema; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import io.github.sebastiantoepfer.jsonschema.spi.JsonSchemaFactory; import jakarta.json.JsonValue; @@ -56,5 +57,10 @@ public JsonValue.ValueType getValueType() { public Optional asSubSchema(String name) { throw new UnsupportedOperationException("Not supported yet."); } + + @Override + public > T printOn(final T media) { + throw new UnsupportedOperationException("Not supported yet."); + } } } diff --git a/api/src/test/java/io/github/sebastiantoepfer/jsonschema/JsonSubSchemaTest.java b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/JsonSubSchemaTest.java index f058b4d7..b0ca587f 100644 --- a/api/src/test/java/io/github/sebastiantoepfer/jsonschema/JsonSubSchemaTest.java +++ b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/JsonSubSchemaTest.java @@ -27,6 +27,7 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.sameInstance; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.JsonValue; import java.util.Optional; @@ -50,7 +51,7 @@ public Validator validator() { } @Override - public Optional keywordByName(String name) { + public Optional keywordByName(final String name) { throw new UnsupportedOperationException("Not supported yet."); } @@ -60,7 +61,12 @@ public JsonValue.ValueType getValueType() { } @Override - public Optional asSubSchema(String name) { + public Optional asSubSchema(final String name) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public > T printOn(final T media) { throw new UnsupportedOperationException("Not supported yet."); } } diff --git a/api/src/test/java/io/github/sebastiantoepfer/jsonschema/keyword/AnnotationTest.java b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/keyword/AnnotationTest.java index c9479d12..3f5793dc 100644 --- a/api/src/test/java/io/github/sebastiantoepfer/jsonschema/keyword/AnnotationTest.java +++ b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/keyword/AnnotationTest.java @@ -26,6 +26,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; +import io.github.sebastiantoepfer.ddd.common.Media; import jakarta.json.JsonValue; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; @@ -52,12 +53,17 @@ void should_return_this_as_annotation() { private static class TestAnnotation implements Annotation { @Override - public JsonValue valueFor(JsonValue value) { + public JsonValue valueFor(final JsonValue value) { throw new UnsupportedOperationException("Not supported yet."); } @Override - public boolean hasName(String name) { + public boolean hasName(final String name) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public > T printOn(final T media) { throw new UnsupportedOperationException("Not supported yet."); } } diff --git a/api/src/test/java/io/github/sebastiantoepfer/jsonschema/keyword/ApplicatorTest.java b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/keyword/ApplicatorTest.java index 85a2aabd..552270f4 100644 --- a/api/src/test/java/io/github/sebastiantoepfer/jsonschema/keyword/ApplicatorTest.java +++ b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/keyword/ApplicatorTest.java @@ -26,6 +26,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; +import io.github.sebastiantoepfer.ddd.common.Media; import jakarta.json.JsonValue; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; @@ -52,12 +53,17 @@ void should_return_this_as_applicator() { private static class TestApplicator implements Applicator { @Override - public boolean applyTo(JsonValue instance) { + public boolean applyTo(final JsonValue instance) { throw new UnsupportedOperationException("Not supported yet."); } @Override - public boolean hasName(String name) { + public boolean hasName(final String name) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public > T printOn(final T media) { throw new UnsupportedOperationException("Not supported yet."); } } diff --git a/api/src/test/java/io/github/sebastiantoepfer/jsonschema/keyword/AssertionTest.java b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/keyword/AssertionTest.java index 63322eb5..6363f46d 100644 --- a/api/src/test/java/io/github/sebastiantoepfer/jsonschema/keyword/AssertionTest.java +++ b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/keyword/AssertionTest.java @@ -26,6 +26,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; +import io.github.sebastiantoepfer.ddd.common.Media; import jakarta.json.JsonValue; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; @@ -52,12 +53,17 @@ void should_return_this_as_assertion() { private static class TestAssertion implements Assertion { @Override - public boolean isValidFor(JsonValue instance) { + public boolean isValidFor(final JsonValue instance) { throw new UnsupportedOperationException("Not supported yet."); } @Override - public boolean hasName(String name) { + public boolean hasName(final String name) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public > T printOn(final T media) { throw new UnsupportedOperationException("Not supported yet."); } } diff --git a/api/src/test/java/io/github/sebastiantoepfer/jsonschema/keyword/IdentifierTest.java b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/keyword/IdentifierTest.java index 36085e82..144b6eb4 100644 --- a/api/src/test/java/io/github/sebastiantoepfer/jsonschema/keyword/IdentifierTest.java +++ b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/keyword/IdentifierTest.java @@ -26,6 +26,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword.KeywordCategory; import java.net.URI; import org.hamcrest.Matchers; @@ -58,7 +59,12 @@ public URI asUri() { } @Override - public boolean hasName(String name) { + public boolean hasName(final String name) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public > T printOn(final T media) { throw new UnsupportedOperationException("Not supported yet."); } } diff --git a/api/src/test/java/io/github/sebastiantoepfer/jsonschema/keyword/ReservedLocationTest.java b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/keyword/ReservedLocationTest.java index 22cca4aa..71b42b33 100644 --- a/api/src/test/java/io/github/sebastiantoepfer/jsonschema/keyword/ReservedLocationTest.java +++ b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/keyword/ReservedLocationTest.java @@ -26,6 +26,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; +import io.github.sebastiantoepfer.ddd.common.Media; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; @@ -54,7 +55,12 @@ void should_return_this_as_reservedLocation() { private static class TestReservedLocation implements ReservedLocation { @Override - public boolean hasName(String name) { + public boolean hasName(final String name) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public > T printOn(final T media) { throw new UnsupportedOperationException("Not supported yet."); } } diff --git a/api/src/test/java/io/github/sebastiantoepfer/jsonschema/keyword/StaticAnnotationTest.java b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/keyword/StaticAnnotationTest.java index 89206c70..843055f9 100644 --- a/api/src/test/java/io/github/sebastiantoepfer/jsonschema/keyword/StaticAnnotationTest.java +++ b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/keyword/StaticAnnotationTest.java @@ -24,8 +24,11 @@ package io.github.sebastiantoepfer.jsonschema.keyword; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import jakarta.json.Json; import jakarta.json.JsonValue; import org.junit.jupiter.api.Test; @@ -49,4 +52,46 @@ void should_return_his_value() { is(Json.createValue("string")) ); } + + @Test + void should_be_printable_with_null_as_value() { + assertThat(new StaticAnnotation("value", null).printOn(new HashMapMedia()).entrySet(), is(empty())); + } + + @Test + void should_be_printable_with_decimalnumber_as_value() { + assertThat( + new StaticAnnotation("default", Json.createValue(32)).printOn(new HashMapMedia()), + hasEntry("default", 32L) + ); + } + + @Test + void should_be_printable_with_decimal_as_value() { + assertThat( + new StaticAnnotation("default", Json.createValue(32.1)).printOn(new HashMapMedia()), + hasEntry("default", 32.1) + ); + } + + @Test + void should_be_printable_with_string_as_value() { + assertThat( + new StaticAnnotation("format", Json.createValue("datetime")).printOn(new HashMapMedia()), + hasEntry("format", "datetime") + ); + } + + @Test + void should_be_printable_with_true_as_value() { + assertThat(new StaticAnnotation("value", JsonValue.TRUE).printOn(new HashMapMedia()), hasEntry("value", true)); + } + + @Test + void should_be_printable_with_false_as_value() { + assertThat( + new StaticAnnotation("value", JsonValue.FALSE).printOn(new HashMapMedia()), + hasEntry("value", false) + ); + } } diff --git a/api/src/test/java/module-info.java b/api/src/test/java/module-info.java index 359118ed..3d799ba8 100644 --- a/api/src/test/java/module-info.java +++ b/api/src/test/java/module-info.java @@ -23,6 +23,9 @@ */ open module io.github.sebastiantoepfer.jsonschema { + requires io.github.sebastiantoepfer.ddd.common; + requires io.github.sebastiantoepfer.ddd.media.core; + requires io.github.sebastiantoepfer.ddd.media.json; requires jakarta.json; requires org.junit.jupiter.api; diff --git a/core/pom.xml b/core/pom.xml index 60bd3be5..dc8eda7a 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -23,6 +23,20 @@ json-schema-vocabulary-spi ${project.version} + + io.github.sebastian-toepfer.ddd + common + + + io.github.sebastian-toepfer.ddd + media-json-api + + + com.github.spotbugs + spotbugs-annotations + + + org.junit.jupiter @@ -67,6 +81,11 @@ parsson test + + io.github.sebastian-toepfer.ddd + media-core + test + diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/DefaultJsonObjectSchema.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/DefaultJsonObjectSchema.java index 87813013..433e3f4e 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/DefaultJsonObjectSchema.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/DefaultJsonObjectSchema.java @@ -26,6 +26,7 @@ import static java.util.stream.Collectors.collectingAndThen; import static java.util.stream.Collectors.toList; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.JsonSubSchema; import io.github.sebastiantoepfer.jsonschema.Validator; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; @@ -39,6 +40,11 @@ public DefaultJsonObjectSchema(final JsonObject value) { super(value); } + @Override + public > T printOn(final T media) { + return keywords().reduce(media, (m, k) -> k.printOn(m), (l, r) -> null); + } + @Override public Validator validator() { return keywords().collect(collectingAndThen(toList(), KeywordBasedValidator::new)); diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/DefaultJsonSubSchema.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/DefaultJsonSubSchema.java index c217eb9d..753e6929 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/DefaultJsonSubSchema.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/DefaultJsonSubSchema.java @@ -26,6 +26,7 @@ import static java.util.stream.Collectors.collectingAndThen; import static java.util.stream.Collectors.toList; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.JsonSubSchema; import io.github.sebastiantoepfer.jsonschema.Validator; @@ -46,6 +47,11 @@ public DefaultJsonSubSchema(final JsonSchema owner, final JsonSchema schema) { this.schema = Objects.requireNonNull(schema); } + @Override + public > T printOn(final T media) { + return schema.printOn(media); + } + @Override public JsonSchema owner() { return owner; diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/EmptyJsonSchema.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/EmptyJsonSchema.java index 47242c62..8e21cbcc 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/EmptyJsonSchema.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/EmptyJsonSchema.java @@ -23,6 +23,7 @@ */ package io.github.sebastiantoepfer.jsonschema.core; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.JsonSubSchema; import io.github.sebastiantoepfer.jsonschema.Validator; import io.github.sebastiantoepfer.jsonschema.core.codition.NoCondition; @@ -50,4 +51,9 @@ public Optional keywordByName(final String name) { public Optional asSubSchema(final String name) { return Optional.empty(); } + + @Override + public > T printOn(final T media) { + return media; + } } diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/FalseJsonSchema.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/FalseJsonSchema.java index 26086cb2..0297576b 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/FalseJsonSchema.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/FalseJsonSchema.java @@ -23,6 +23,7 @@ */ package io.github.sebastiantoepfer.jsonschema.core; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.JsonSubSchema; import io.github.sebastiantoepfer.jsonschema.Validator; import io.github.sebastiantoepfer.jsonschema.core.codition.UnfulfillableCondition; @@ -36,6 +37,11 @@ public FalseJsonSchema() { super(JsonValue.FALSE); } + @Override + public > T printOn(final T media) { + throw new UnsupportedOperationException("false schema not supported yet!"); + } + @Override public Validator validator() { return new DefaultValidator(new UnfulfillableCondition<>()); diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/TrueJsonSchema.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/TrueJsonSchema.java index dec65232..e22f0e8e 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/TrueJsonSchema.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/TrueJsonSchema.java @@ -23,6 +23,7 @@ */ package io.github.sebastiantoepfer.jsonschema.core; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.JsonSubSchema; import io.github.sebastiantoepfer.jsonschema.Validator; import io.github.sebastiantoepfer.jsonschema.core.codition.NoCondition; @@ -50,4 +51,9 @@ public Optional keywordByName(final String name) { public Optional asSubSchema(final String name) { return Optional.empty(); } + + @Override + public > T printOn(final T media) { + return media; + } } diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/codition/AnyOfCondition.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/codition/AnyOfCondition.java deleted file mode 100644 index 194167f6..00000000 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/codition/AnyOfCondition.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * The MIT License - * - * Copyright 2023 sebastian. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package io.github.sebastiantoepfer.jsonschema.core.codition; - -import jakarta.json.JsonValue; -import java.util.Collection; -import java.util.List; - -public class AnyOfCondition implements Condition { - - private final Collection> conditions; - - public AnyOfCondition(final Collection> conditions) { - this.conditions = List.copyOf(conditions); - } - - @Override - public boolean isFulfilledBy(final JsonValue value) { - return conditions.stream().anyMatch(c -> c.isFulfilledBy(value)); - } -} diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/codition/OfTypeCondition.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/codition/OfTypeCondition.java deleted file mode 100644 index 9c9573b7..00000000 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/codition/OfTypeCondition.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * The MIT License - * - * Copyright 2023 sebastian. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package io.github.sebastiantoepfer.jsonschema.core.codition; - -import io.github.sebastiantoepfer.jsonschema.InstanceType; -import jakarta.json.JsonValue; - -public final class OfTypeCondition implements Condition { - - private final InstanceType type; - - public OfTypeCondition(final InstanceType type) { - this.type = type; - } - - @Override - public boolean isFulfilledBy(final JsonValue value) { - return type.isInstance(value); - } -} diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/AdditionalPropertiesKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/AdditionalPropertiesKeywordType.java index 00ef8363..1f77eced 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/AdditionalPropertiesKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/AdditionalPropertiesKeywordType.java @@ -26,6 +26,7 @@ import static jakarta.json.stream.JsonCollectors.toJsonArray; import static java.util.function.Predicate.not; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; @@ -53,17 +54,25 @@ public String name() { @Override public Keyword createKeyword(final JsonSchema schema) { - return new AdditionalPropertiesKeyword(schema, schema.asJsonObject().get(name())); + return new AdditionalPropertiesKeyword( + schema, + new DefaultJsonSchemaFactory().create(schema.asJsonObject().get(name())) + ); } private class AdditionalPropertiesKeyword implements Applicator, Annotation { private final JsonSchema schema; - private final JsonValue additionalProperties; + private final JsonSchema additionalPropertiesSchema; - public AdditionalPropertiesKeyword(final JsonSchema schema, final JsonValue additionalPropertiesSchema) { + public AdditionalPropertiesKeyword(final JsonSchema schema, final JsonSchema additionalPropertiesSchema) { this.schema = schema; - this.additionalProperties = additionalPropertiesSchema; + this.additionalPropertiesSchema = additionalPropertiesSchema; + } + + @Override + public > T printOn(final T media) { + return media.withValue(name(), additionalPropertiesSchema); } @Override @@ -72,7 +81,6 @@ public boolean applyTo(final JsonValue instance) { } private boolean additionalPropertiesMatches(final JsonObject instance) { - final JsonSchema additionalPropertiesSchema = new DefaultJsonSchemaFactory().create(additionalProperties); return findPropertiesForValidation(instance) .map(Map.Entry::getValue) .allMatch(value -> additionalPropertiesSchema.validator().isValid(value)); diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ItemsKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ItemsKeywordType.java index 7fbebacd..935191f7 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ItemsKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ItemsKeywordType.java @@ -23,6 +23,7 @@ */ package io.github.sebastiantoepfer.jsonschema.core.vocab.applicator; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.JsonSubSchema; @@ -59,6 +60,11 @@ public ItemsKeyword(final JsonSubSchema schema) { this.schema = Objects.requireNonNull(schema); } + @Override + public > T printOn(final T media) { + return media.withValue(name(), schema); + } + @Override public JsonSchema owner() { return schema.owner(); diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PatternPropertiesKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PatternPropertiesKeywordType.java index a0085d88..2f360b7b 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PatternPropertiesKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PatternPropertiesKeywordType.java @@ -25,6 +25,8 @@ import static jakarta.json.stream.JsonCollectors.toJsonArray; +import io.github.sebastiantoepfer.ddd.common.Media; +import io.github.sebastiantoepfer.ddd.media.json.JsonObjectPrintable; import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.JsonSchemas; @@ -40,7 +42,6 @@ import java.util.Map; import java.util.Objects; import java.util.regex.Pattern; -import java.util.stream.Collectors; final class PatternPropertiesKeywordType implements KeywordType { @@ -56,15 +57,15 @@ public Keyword createKeyword(final JsonSchema schema) { private class PatternPropertiesKeyword implements Applicator, Annotation { - private final Map properties; + private final JsonObject properties; - public PatternPropertiesKeyword(final JsonObject schema) { - this.properties = - schema - .entrySet() - .stream() - .map(e -> Map.entry(Pattern.compile(e.getKey()), e.getValue())) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + public PatternPropertiesKeyword(final JsonObject properties) { + this.properties = properties; + } + + @Override + public > T printOn(final T media) { + return media.withValue(name(), new JsonObjectPrintable(properties)); } @Override @@ -90,6 +91,7 @@ private boolean propertyMatches(final Map.Entry property) { return properties .entrySet() .stream() + .map(e -> Map.entry(Pattern.compile(e.getKey()), e.getValue())) .filter(e -> e.getKey().matcher(property.getKey()).find()) .map(Map.Entry::getValue) .map(JsonSchemas::load) @@ -102,9 +104,13 @@ public JsonValue valueFor(final JsonValue value) { .asJsonObject() .keySet() .stream() - .filter(name -> properties.keySet().stream().anyMatch(p -> p.matcher(name).find())) + .filter(this::isValidName) .map(Json::createValue) .collect(toJsonArray()); } + + private boolean isValidName(final String name) { + return properties.keySet().stream().map(Pattern::compile).anyMatch(p -> p.matcher(name).find()); + } } } diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PrefixItemsKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PrefixItemsKeywordType.java index 02efb99f..63a45cd3 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PrefixItemsKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PrefixItemsKeywordType.java @@ -23,6 +23,7 @@ */ package io.github.sebastiantoepfer.jsonschema.core.vocab.applicator; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.JsonSchemas; @@ -66,6 +67,11 @@ public PrefixItemsKeyword(final JsonArray schemas) { this.schemas = schemas.stream().map(JsonSchemas::load).toList(); } + @Override + public > T printOn(final T media) { + return media.withValue(name(), schemas); + } + @Override public Collection categories() { return List.of(KeywordCategory.ANNOTATION, KeywordCategory.APPLICATOR); diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PropertiesKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PropertiesKeywordType.java index 9799450b..e81cd764 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PropertiesKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PropertiesKeywordType.java @@ -27,6 +27,8 @@ import static java.util.stream.Collectors.collectingAndThen; import static java.util.stream.Collectors.toMap; +import io.github.sebastiantoepfer.ddd.common.Media; +import io.github.sebastiantoepfer.ddd.common.Printable; import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; @@ -77,6 +79,11 @@ public PropertiesKeyword(final Map schemas) { this.schemas = Map.copyOf(schemas); } + @Override + public > T printOn(final T media) { + return media.withValue(name(), new SchemaMapPrintableAdapter()); + } + @Override public boolean hasName(final String name) { return Objects.equals(name(), name); @@ -114,5 +121,16 @@ public JsonValue valueFor(final JsonValue instance) { .map(Json::createValue) .collect(toJsonArray()); } + + private class SchemaMapPrintableAdapter implements Printable { + + @Override + public > T printOn(final T media) { + return schemas + .entrySet() + .stream() + .reduce(media, (m, e) -> m.withValue(e.getKey(), e.getValue()), (l, r) -> null); + } + } } } diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DefsKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DefsKeywordType.java index 9f3a7325..1b31f5a4 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DefsKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DefsKeywordType.java @@ -23,6 +23,7 @@ */ package io.github.sebastiantoepfer.jsonschema.core.vocab.core; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; @@ -52,6 +53,11 @@ public Keyword createKeyword(final JsonSchema schema) { private class DefsKeyword implements ReservedLocation { + @Override + public > T printOn(final T media) { + return media.withValue(name(), new Empty()); + } + @Override public boolean hasName(final String name) { return Objects.equals(name(), name); diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DynamicRefKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DynamicRefKeywordType.java index 998b052d..95c1dd9a 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DynamicRefKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DynamicRefKeywordType.java @@ -23,6 +23,8 @@ */ package io.github.sebastiantoepfer.jsonschema.core.vocab.core; +import io.github.sebastiantoepfer.ddd.common.Media; +import io.github.sebastiantoepfer.ddd.common.Printable; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.keyword.Applicator; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; @@ -44,6 +46,11 @@ public String name() { @Override public Keyword createKeyword(final JsonSchema schema) { return new Applicator() { + @Override + public > T printOn(final T media) { + return media.withValue(name(), new Printable.Empty()); + } + @Override public boolean applyTo(final JsonValue instance) { //something is wrong here diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/IdKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/IdKeywordType.java index 913840f4..2d4dbbac 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/IdKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/IdKeywordType.java @@ -23,6 +23,7 @@ */ package io.github.sebastiantoepfer.jsonschema.core.vocab.core; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.keyword.Identifier; @@ -62,6 +63,11 @@ private IdKeyword(final JsonString uri) { this.uri = URI.create(uri.getString()); } + @Override + public > T printOn(final T media) { + return media.withValue(name(), uri.toString()); + } + @Override public URI asUri() { return uri; diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/RefKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/RefKeywordType.java index cff82fb0..6c949d8f 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/RefKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/RefKeywordType.java @@ -23,6 +23,7 @@ */ package io.github.sebastiantoepfer.jsonschema.core.vocab.core; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.JsonSchemas; @@ -70,6 +71,11 @@ private RefKeyword(final JsonSchema schema, final JsonString uri) { this.uri = URI.create(uri.getString()); } + @Override + public > T printOn(final T media) { + return media.withValue(name(), uri.toString()); + } + @Override public boolean applyTo(final JsonValue instance) { return retrieveJsonSchema().validator().isValid(instance); diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/SchemaKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/SchemaKeywordType.java index 76645f4d..88579f1c 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/SchemaKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/SchemaKeywordType.java @@ -23,6 +23,7 @@ */ package io.github.sebastiantoepfer.jsonschema.core.vocab.core; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.keyword.Identifier; @@ -62,6 +63,11 @@ private SchemaKeyword(final JsonString uri) { this.uri = URI.create(uri.getString()); } + @Override + public > T printOn(final T media) { + return media.withValue(name(), uri.toString()); + } + @Override public URI asUri() { return uri; diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/VocabularyKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/VocabularyKeywordType.java index 6e648af9..9dbf2263 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/VocabularyKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/VocabularyKeywordType.java @@ -23,6 +23,8 @@ */ package io.github.sebastiantoepfer.jsonschema.core.vocab.core; +import io.github.sebastiantoepfer.ddd.common.Media; +import io.github.sebastiantoepfer.ddd.media.json.JsonObjectPrintable; import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; @@ -75,6 +77,11 @@ public final class VocabularyKeyword implements Keyword, VocabularyDefinitions { this.vocabularies = Objects.requireNonNull(vocabularies); } + @Override + public > T printOn(final T media) { + return media.withValue(name(), new JsonObjectPrintable(vocabularies)); + } + @Override public boolean hasName(final String name) { return Objects.equals(name(), name); diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/EnumKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/EnumKeywordType.java index 7f207587..af2d3f1c 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/EnumKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/EnumKeywordType.java @@ -25,11 +25,14 @@ import static java.util.function.Predicate.isEqual; +import io.github.sebastiantoepfer.ddd.common.Media; +import io.github.sebastiantoepfer.ddd.media.json.JsonObjectPrintable; import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.keyword.Assertion; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType; +import jakarta.json.Json; import jakarta.json.JsonArray; import jakarta.json.JsonNumber; import jakarta.json.JsonValue; @@ -56,6 +59,12 @@ public EnumKeyword(final JsonArray allowedValues) { this.allowedValues = allowedValues; } + @Override + public > T printOn(final T media) { + return new JsonObjectPrintable(Json.createObjectBuilder().add(name(), allowedValues).build()) + .printOn(media); + } + @Override public boolean isValidFor(final JsonValue instance) { final boolean result; diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ExclusiveMaximumKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ExclusiveMaximumKeywordType.java index 6132b617..f4bfcec9 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ExclusiveMaximumKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ExclusiveMaximumKeywordType.java @@ -23,6 +23,7 @@ */ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.keyword.Assertion; @@ -58,6 +59,11 @@ private ExclusiveMaximumKeyword(final JsonNumber max) { this.max = max.bigDecimalValue(); } + @Override + public > T printOn(final T media) { + return media.withValue(name(), max); + } + @Override public boolean hasName(final String name) { return Objects.equals(name(), name); diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ExclusiveMinimumKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ExclusiveMinimumKeywordType.java index f708b5ce..b0894727 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ExclusiveMinimumKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ExclusiveMinimumKeywordType.java @@ -23,6 +23,7 @@ */ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.keyword.Assertion; @@ -58,6 +59,11 @@ public ExclusiveMinimumKeyword(final JsonNumber min) { this.min = min.bigDecimalValue(); } + @Override + public > T printOn(final T media) { + return media.withValue(name(), min); + } + @Override public boolean hasName(final String name) { return Objects.equals(name(), name); diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaxItemsKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaxItemsKeywordType.java index 0f3d3c68..7b76d1eb 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaxItemsKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaxItemsKeywordType.java @@ -23,13 +23,14 @@ */ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.keyword.Assertion; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType; -import jakarta.json.JsonNumber; import jakarta.json.JsonValue; +import java.math.BigInteger; import java.util.Objects; final class MaxItemsKeywordType implements KeywordType { @@ -41,20 +42,25 @@ public String name() { @Override public Keyword createKeyword(final JsonSchema schema) { - return new MaxItemsKeyword(schema.asJsonObject().getJsonNumber(name())); + return new MaxItemsKeyword(schema.asJsonObject().getJsonNumber(name()).bigIntegerValueExact()); } private class MaxItemsKeyword implements Assertion { - private final int maxItems; + private final BigInteger maxItems; - public MaxItemsKeyword(final JsonNumber maxItems) { - this.maxItems = maxItems.intValue(); + public MaxItemsKeyword(final BigInteger maxItems) { + this.maxItems = maxItems; + } + + @Override + public > T printOn(final T media) { + return media.withValue(name(), maxItems); } @Override public boolean isValidFor(final JsonValue instance) { - return !InstanceType.ARRAY.isInstance(instance) || instance.asJsonArray().size() <= maxItems; + return !InstanceType.ARRAY.isInstance(instance) || instance.asJsonArray().size() <= maxItems.intValue(); } @Override diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaxLengthKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaxLengthKeywordType.java index dee8ce15..464ee676 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaxLengthKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaxLengthKeywordType.java @@ -23,6 +23,7 @@ */ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.keyword.Assertion; @@ -31,6 +32,7 @@ import jakarta.json.JsonNumber; import jakarta.json.JsonString; import jakarta.json.JsonValue; +import java.math.BigInteger; import java.util.Objects; class MaxLengthKeywordType implements KeywordType { @@ -44,7 +46,7 @@ public String name() { public Keyword createKeyword(final JsonSchema schema) { final JsonValue value = schema.asJsonObject().get(name()); if (InstanceType.INTEGER.isInstance(value)) { - return new MaxLengthKeyword((JsonNumber) value); + return new MaxLengthKeyword(((JsonNumber) value).bigIntegerValueExact()); } else { throw new IllegalArgumentException("value must be a positiv integer!"); } @@ -52,12 +54,17 @@ public Keyword createKeyword(final JsonSchema schema) { private class MaxLengthKeyword implements Assertion { - private final JsonNumber value; + private final BigInteger value; - public MaxLengthKeyword(final JsonNumber value) { + public MaxLengthKeyword(final BigInteger value) { this.value = value; } + @Override + public > T printOn(final T media) { + return media.withValue(name(), value); + } + @Override public boolean hasName(final String name) { return Objects.equals(name(), name); diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaximumKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaximumKeywordType.java index 7e95c9b8..7d6e4a36 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaximumKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaximumKeywordType.java @@ -23,6 +23,7 @@ */ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.keyword.Assertion; @@ -58,6 +59,11 @@ public MaximumKeyword(final JsonNumber max) { this.max = max.bigDecimalValue(); } + @Override + public > T printOn(final T media) { + return media.withValue(name(), max); + } + @Override public boolean hasName(final String name) { return Objects.equals(name(), name); diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinItemsKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinItemsKeywordType.java index 35f9a395..6743e1e5 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinItemsKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinItemsKeywordType.java @@ -23,13 +23,14 @@ */ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.keyword.Assertion; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType; -import jakarta.json.JsonNumber; import jakarta.json.JsonValue; +import java.math.BigInteger; import java.util.Objects; final class MinItemsKeywordType implements KeywordType { @@ -41,20 +42,25 @@ public String name() { @Override public Keyword createKeyword(final JsonSchema schema) { - return new MinItemsKeyword(schema.asJsonObject().getJsonNumber(name())); + return new MinItemsKeyword(schema.asJsonObject().getJsonNumber(name()).bigIntegerValueExact()); } private class MinItemsKeyword implements Assertion { - private final int minItems; + private final BigInteger minItems; - public MinItemsKeyword(final JsonNumber minItems) { - this.minItems = minItems.intValue(); + public MinItemsKeyword(final BigInteger minItems) { + this.minItems = minItems; + } + + @Override + public > T printOn(final T media) { + return media.withValue(name(), minItems); } @Override public boolean isValidFor(final JsonValue instance) { - return !InstanceType.ARRAY.isInstance(instance) || instance.asJsonArray().size() >= minItems; + return !InstanceType.ARRAY.isInstance(instance) || instance.asJsonArray().size() >= minItems.intValue(); } @Override diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinLengthKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinLengthKeywordType.java index b61164d4..ee0f8469 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinLengthKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinLengthKeywordType.java @@ -23,6 +23,7 @@ */ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.keyword.Assertion; @@ -58,6 +59,11 @@ public MinLengthKeyword(final JsonNumber value) { this.value = Objects.requireNonNull(value); } + @Override + public > T printOn(final T media) { + return media.withValue(name(), value.bigIntegerValueExact()); + } + @Override public boolean hasName(final String name) { return Objects.equals(name(), name); diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinimumKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinimumKeywordType.java index 73450168..307082a8 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinimumKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinimumKeywordType.java @@ -23,6 +23,7 @@ */ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.keyword.Assertion; @@ -58,6 +59,11 @@ public MinimumKeyword(final JsonNumber min) { this.min = min.bigDecimalValue(); } + @Override + public > T printOn(final T media) { + return media.withValue(name(), min); + } + @Override public boolean hasName(final String name) { return Objects.equals(name(), name); diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MultipleOfKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MultipleOfKeywordType.java index 3c46a711..c3b56790 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MultipleOfKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MultipleOfKeywordType.java @@ -23,6 +23,7 @@ */ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.keyword.Assertion; @@ -58,6 +59,11 @@ public MultipleOfKeyword(final JsonNumber multipleOf) { this.multipleOf = multipleOf.bigDecimalValue(); } + @Override + public > T printOn(final T media) { + return media.withValue(name(), multipleOf); + } + @Override public boolean hasName(final String name) { return Objects.equals(name(), name); diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/PatternKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/PatternKeywordType.java index f5021565..8781bbb0 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/PatternKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/PatternKeywordType.java @@ -23,6 +23,7 @@ */ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.keyword.Assertion; @@ -58,6 +59,11 @@ public PatternKeyword(final JsonString value) { this.pattern = Pattern.compile(value.getString()); } + @Override + public > T printOn(final T media) { + return media.withValue(name(), pattern.pattern()); + } + @Override public boolean hasName(final String name) { return Objects.equals(name(), name); diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/RequiredKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/RequiredKeywordType.java index 70c2c696..5e8f2fea 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/RequiredKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/RequiredKeywordType.java @@ -23,6 +23,10 @@ */ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; +import static java.util.stream.Collectors.collectingAndThen; +import static java.util.stream.Collectors.toList; + +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.keyword.Assertion; @@ -33,6 +37,7 @@ import jakarta.json.JsonString; import jakarta.json.JsonValue; import java.util.Objects; +import java.util.stream.Stream; class RequiredKeywordType implements KeywordType { @@ -54,17 +59,22 @@ public RequiredKeyword(final JsonArray required) { this.required = Objects.requireNonNull(required); } + @Override + public > T printOn(final T media) { + return asStrings().collect(collectingAndThen(toList(), values -> media.withValue(name(), values))); + } + @Override public boolean isValidFor(final JsonValue instance) { return !InstanceType.OBJECT.isInstance(instance) || hasAllRequiredProperties(instance.asJsonObject()); } private boolean hasAllRequiredProperties(final JsonObject instance) { - return required - .stream() - .map(JsonString.class::cast) - .map(JsonString::getString) - .allMatch(instance.keySet()::contains); + return asStrings().allMatch(instance.keySet()::contains); + } + + private Stream asStrings() { + return required.stream().map(JsonString.class::cast).map(JsonString::getString); } @Override diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/TypeKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/TypeKeywordType.java index ae5ffa6a..b5552e93 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/TypeKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/TypeKeywordType.java @@ -23,20 +23,16 @@ */ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; -import static java.util.stream.Collectors.collectingAndThen; -import static java.util.stream.Collectors.toList; - +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.JsonSchema; -import io.github.sebastiantoepfer.jsonschema.core.codition.AnyOfCondition; -import io.github.sebastiantoepfer.jsonschema.core.codition.Condition; -import io.github.sebastiantoepfer.jsonschema.core.codition.OfTypeCondition; import io.github.sebastiantoepfer.jsonschema.keyword.Assertion; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType; import jakarta.json.JsonString; import jakarta.json.JsonValue; -import java.util.Locale; +import java.util.Collection; +import java.util.List; import java.util.Objects; /** @@ -52,32 +48,38 @@ public String name() { @Override public Keyword createKeyword(final JsonSchema schema) { final JsonValue typeDefinition = schema.asJsonObject().get(name()); - final Condition typeContraint = + final Collection allowedTypes = switch (typeDefinition.getValueType()) { - case STRING -> new OfTypeCondition( - InstanceType.valueOf(((JsonString) typeDefinition).getString().toUpperCase(Locale.US)) - ); + case STRING -> List.of(((JsonString) typeDefinition).getString()); case ARRAY -> typeDefinition .asJsonArray() .stream() .map(JsonString.class::cast) .map(JsonString::getString) - .map(String::toUpperCase) - .map(InstanceType::valueOf) - .map(OfTypeCondition::new) - .collect(collectingAndThen(toList(), AnyOfCondition::new)); + .toList(); default -> throw new IllegalArgumentException(); }; - return new TypeKeyword(typeContraint); + return new TypeKeyword(allowedTypes); } private final class TypeKeyword implements Assertion { - private final Condition definition; + private final Collection allowedTypes; + + public TypeKeyword(final Collection allowedTypes) { + this.allowedTypes = Objects.requireNonNull(allowedTypes); + } - public TypeKeyword(final Condition definition) { - this.definition = Objects.requireNonNull(definition); + @Override + public > T printOn(final T media) { + final T result; + if (allowedTypes.size() == 1) { + result = media.withValue(name(), allowedTypes.iterator().next()); + } else { + result = media.withValue(name(), allowedTypes); + } + return result; } @Override @@ -87,7 +89,11 @@ public boolean hasName(final String name) { @Override public boolean isValidFor(final JsonValue instance) { - return definition.isFulfilledBy(instance); + return allowedTypes + .stream() + .map(String::toLowerCase) + .map(InstanceType::fromString) + .anyMatch(instanceType -> instanceType.isInstance(instance)); } } } diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/UniqueItemsKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/UniqueItemsKeywordType.java index a58760e4..aa88be8c 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/UniqueItemsKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/UniqueItemsKeywordType.java @@ -23,6 +23,7 @@ */ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.keyword.Assertion; @@ -62,6 +63,11 @@ private UniqueItemsKeyword(final boolean unique) { this.unique = unique; } + @Override + public > T printOn(final T media) { + return media.withValue(name(), unique); + } + @Override public boolean isValidFor(final JsonValue instance) { return ( diff --git a/core/src/main/java/module-info.java b/core/src/main/java/module-info.java index 51f87de4..44b0b3d0 100644 --- a/core/src/main/java/module-info.java +++ b/core/src/main/java/module-info.java @@ -24,6 +24,8 @@ module io.github.sebastiantoepfer.jsonschema.core { requires io.github.sebastiantoepfer.jsonschema; requires io.github.sebastiantoepfer.jsonschema.vocabulary.spi; + requires io.github.sebastiantoepfer.ddd.common; + requires io.github.sebastiantoepfer.ddd.media.json; requires jakarta.json; provides io.github.sebastiantoepfer.jsonschema.spi.JsonSchemaFactory diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/AbstractJsonValueSchemaTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/AbstractJsonValueSchemaTest.java index 83eb06fb..e1fee821 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/AbstractJsonValueSchemaTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/AbstractJsonValueSchemaTest.java @@ -27,6 +27,7 @@ import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; +import io.github.sebastiantoepfer.ddd.common.Media; import io.github.sebastiantoepfer.jsonschema.JsonSubSchema; import io.github.sebastiantoepfer.jsonschema.Validator; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; @@ -69,6 +70,11 @@ public Validator validator() { throw new UnsupportedOperationException("Not supported yet."); } + @Override + public > T printOn(final T media) { + throw new UnsupportedOperationException("Not supported yet."); + } + @Override public Optional keywordByName(final String name) { return Optional.empty(); diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/DefaultJsonObjectSchemaTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/DefaultJsonObjectSchemaTest.java index 274099f2..933ea289 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/DefaultJsonObjectSchemaTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/DefaultJsonObjectSchemaTest.java @@ -26,10 +26,14 @@ import static com.github.npathai.hamcrestopt.OptionalMatchers.isEmpty; import static com.github.npathai.hamcrestopt.OptionalMatchers.isPresent; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.hasEntry; +import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.junit.jupiter.api.Assertions.assertThrows; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import jakarta.json.Json; import jakarta.json.JsonValue; import org.junit.jupiter.api.Test; @@ -139,4 +143,9 @@ void should_return_empty_if_given_name_not_resolve_to_a_valid_schematype() { isEmpty() ); } + + @Test + void should_be_printable() { + assertThat(schema.printOn(new HashMapMedia()), allOf(hasEntry("type", "array"), hasKey("items"))); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/DefaultJsonSubSchemaTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/DefaultJsonSubSchemaTest.java index 45851da4..b018fbba 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/DefaultJsonSubSchemaTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/DefaultJsonSubSchemaTest.java @@ -25,8 +25,10 @@ import static com.github.npathai.hamcrestopt.OptionalMatchers.isPresent; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import jakarta.json.Json; import jakarta.json.JsonValue; @@ -75,4 +77,16 @@ void should_convertable_to_plain_jsonobject() { is(Json.createObjectBuilder().add("sub", JsonValue.TRUE).build()) ); } + + @Test + void should_be_printable() { + assertThat( + new DefaultJsonSubSchema( + new DefaultJsonSchemaFactory().create(Json.createObjectBuilder().add("test", JsonValue.TRUE).build()), + new DefaultJsonSchemaFactory().create(Json.createObjectBuilder().add("type", "array").build()) + ) + .printOn(new HashMapMedia()), + hasEntry("type", "array") + ); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/EmptyJsonSchemaTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/EmptyJsonSchemaTest.java index b3057564..93d6fd4a 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/EmptyJsonSchemaTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/EmptyJsonSchemaTest.java @@ -24,9 +24,12 @@ package io.github.sebastiantoepfer.jsonschema.core; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.anEmptyMap; import static org.hamcrest.Matchers.is; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import jakarta.json.JsonValue; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ArgumentsSource; @@ -37,4 +40,9 @@ class EmptyJsonSchemaTest { void should_be_valid_for_everything(final JsonValue value) { assertThat(new EmptyJsonSchema().validator().isValid(value), is(true)); } + + @Test + void should_be_printable() { + assertThat(new EmptyJsonSchema().printOn(new HashMapMedia()), anEmptyMap()); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/TrueJsonSchemaTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/TrueJsonSchemaTest.java index 0ca73b20..1b5c3bef 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/TrueJsonSchemaTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/TrueJsonSchemaTest.java @@ -24,9 +24,12 @@ package io.github.sebastiantoepfer.jsonschema.core; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.anEmptyMap; import static org.hamcrest.Matchers.is; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import jakarta.json.JsonValue; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ArgumentsSource; @@ -37,4 +40,9 @@ class TrueJsonSchemaTest { void should_be_valid_for_everything(final JsonValue value) { assertThat(new TrueJsonSchema().validator().isValid(value), is(true)); } + + @Test + void should_be_printable() { + assertThat(new TrueJsonSchema().printOn(new HashMapMedia()), anEmptyMap()); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/AdditionalPropertiesKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/AdditionalPropertiesKeywordTypeTest.java index 262d5bd2..9cfa5537 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/AdditionalPropertiesKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/AdditionalPropertiesKeywordTypeTest.java @@ -24,13 +24,17 @@ package io.github.sebastiantoepfer.jsonschema.core.vocab.applicator; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.anEmptyMap; import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.Json; import jakarta.json.JsonValue; +import org.hamcrest.Matcher; import org.junit.jupiter.api.Test; class AdditionalPropertiesKeywordTypeTest { @@ -160,4 +164,19 @@ void should_return_propertynames_which_will_be_validated() { containsInAnyOrder(Json.createValue("foo")) ); } + + @Test + void should_be_printable() { + assertThat( + new AdditionalPropertiesKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create( + Json.createObjectBuilder().add("additionalProperties", JsonValue.EMPTY_JSON_OBJECT).build() + ) + ) + .printOn(new HashMapMedia()), + (Matcher) hasEntry(is("additionalProperties"), anEmptyMap()) + ); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PatternPropertiesKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PatternPropertiesKeywordTypeTest.java index 6dece1d6..34ba81d0 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PatternPropertiesKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PatternPropertiesKeywordTypeTest.java @@ -24,15 +24,19 @@ package io.github.sebastiantoepfer.jsonschema.core.vocab.applicator; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.anEmptyMap; import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.Json; import jakarta.json.JsonString; import jakarta.json.JsonValue; import java.math.BigDecimal; +import org.hamcrest.Matcher; import org.junit.jupiter.api.Test; class PatternPropertiesKeywordTypeTest { @@ -210,4 +214,19 @@ void should_return_the_matching_property_names() { containsInAnyOrder("foo", "fao") ); } + + @Test + void should_be_printable() { + assertThat( + new PatternPropertiesKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create( + Json.createObjectBuilder().add("patternProperties", JsonValue.EMPTY_JSON_OBJECT).build() + ) + ) + .printOn(new HashMapMedia()), + (Matcher) hasEntry(is("patternProperties"), anEmptyMap()) + ); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PrefixItemsKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PrefixItemsKeywordTypeTest.java index 2417a197..d66a74fd 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PrefixItemsKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PrefixItemsKeywordTypeTest.java @@ -24,16 +24,21 @@ package io.github.sebastiantoepfer.jsonschema.core.vocab.applicator; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.anEmptyMap; +import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.assertThrows; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType; import jakarta.json.Json; import jakarta.json.JsonValue; +import org.hamcrest.Matcher; import org.junit.jupiter.api.Test; class PrefixItemsKeywordTypeTest { @@ -171,4 +176,22 @@ void should_be_applicator_and_annotation() { containsInAnyOrder(Keyword.KeywordCategory.APPLICATOR, Keyword.KeywordCategory.ANNOTATION) ); } + + @Test + void should_be_printable() { + assertThat( + new PrefixItemsKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create( + Json + .createObjectBuilder() + .add("prefixItems", Json.createArrayBuilder().add(JsonValue.TRUE)) + .build() + ) + ) + .printOn(new HashMapMedia()), + (Matcher) hasEntry(is("prefixItems"), contains(anEmptyMap())) + ); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PropertiesKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PropertiesKeywordTypeTest.java index aca5917c..4e73bac2 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PropertiesKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PropertiesKeywordTypeTest.java @@ -24,16 +24,20 @@ package io.github.sebastiantoepfer.jsonschema.core.vocab.applicator; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.anEmptyMap; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.assertThrows; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType; import jakarta.json.Json; import jakarta.json.JsonValue; +import java.util.Map; +import org.hamcrest.Matcher; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; @@ -207,4 +211,23 @@ void should_return_all_matched_propertynames() { contains(Json.createValue("foo")) ); } + + @Test + void should_be_printable() { + assertThat( + ((Map) new PropertiesKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create( + Json + .createObjectBuilder() + .add("properties", Json.createObjectBuilder().add("test", JsonValue.TRUE)) + .build() + ) + ) + .printOn(new HashMapMedia()) + .get("properties")).get("test"), + (Matcher) anEmptyMap() + ); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DefsKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DefsKeywordTypeTest.java index d59c12be..5abcfbc6 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DefsKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DefsKeywordTypeTest.java @@ -24,15 +24,19 @@ package io.github.sebastiantoepfer.jsonschema.core.vocab.core; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.assertThrows; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType; import jakarta.json.Json; import jakarta.json.JsonValue; +import org.hamcrest.Matcher; +import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; class DefsKeywordTypeTest { @@ -57,4 +61,17 @@ void should_know_his_name() { assertThat(defs.hasName("$defs"), is(true)); assertThat(defs.hasName("test"), is(false)); } + + @Test + void should_be_printable() { + assertThat( + new DefsKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create(Json.createObjectBuilder().add("$defs", JsonValue.EMPTY_JSON_OBJECT).build()) + ) + .printOn(new HashMapMedia()), + (Matcher) hasEntry(is("$defs"), Matchers.anEmptyMap()) + ); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DynamicRefKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DynamicRefKeywordTypeTest.java index e7c7ef52..12104c85 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DynamicRefKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DynamicRefKeywordTypeTest.java @@ -24,12 +24,16 @@ package io.github.sebastiantoepfer.jsonschema.core.vocab.core; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.Json; import jakarta.json.JsonValue; +import org.hamcrest.Matcher; +import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; class DynamicRefKeywordTypeTest { @@ -60,4 +64,17 @@ void notFinischedYet() { assertThat(keyword.asApplicator().applyTo(JsonValue.TRUE), is(true)); } + + @Test + void should_be_printable() { + assertThat( + new DynamicRefKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create(Json.createObjectBuilder().add("$dynamicRef", JsonValue.EMPTY_JSON_OBJECT).build()) + ) + .printOn(new HashMapMedia()), + (Matcher) hasEntry(is("$dynamicRef"), Matchers.anEmptyMap()) + ); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/IdKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/IdKeywordTypeTest.java index f61103ae..fae2f4dd 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/IdKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/IdKeywordTypeTest.java @@ -24,15 +24,18 @@ package io.github.sebastiantoepfer.jsonschema.core.vocab.core; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.assertThrows; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.Json; import jakarta.json.JsonValue; import java.net.URI; +import org.hamcrest.Matcher; import org.junit.jupiter.api.Test; class IdKeywordTypeTest { @@ -76,4 +79,22 @@ void should_retun_his_uri() { is(URI.create("https://json-schema.org/draft/2020-12/schema")) ); } + + @Test + void should_be_printable() { + assertThat( + new IdKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create( + Json + .createObjectBuilder() + .add("$id", Json.createValue("https://json-schema.org/draft/2020-12/schema")) + .build() + ) + ) + .printOn(new HashMapMedia()), + (Matcher) hasEntry(is("$id"), is("https://json-schema.org/draft/2020-12/schema")) + ); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/RefKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/RefKeywordTypeTest.java index 29157f3f..cdbfb0a0 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/RefKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/RefKeywordTypeTest.java @@ -24,14 +24,17 @@ package io.github.sebastiantoepfer.jsonschema.core.vocab.core; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.assertThrows; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.Json; import jakarta.json.JsonValue; +import org.hamcrest.Matcher; import org.junit.jupiter.api.Test; class RefKeywordTypeTest { @@ -100,4 +103,17 @@ void should_use_remote_referenced_schema_for_validation() { assertThat(keyword.asApplicator().applyTo(Json.createValue(1L)), is(true)); } + + @Test + void should_be_printable() { + assertThat( + new RefKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create(Json.createObjectBuilder().add("$ref", Json.createValue("#")).build()) + ) + .printOn(new HashMapMedia()), + (Matcher) hasEntry(is("$ref"), is("#")) + ); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/SchemaKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/SchemaKeywordTypeTest.java index deaf0590..e690a980 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/SchemaKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/SchemaKeywordTypeTest.java @@ -24,15 +24,18 @@ package io.github.sebastiantoepfer.jsonschema.core.vocab.core; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.assertThrows; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.Json; import jakarta.json.JsonValue; import java.net.URI; +import org.hamcrest.Matcher; import org.junit.jupiter.api.Test; class SchemaKeywordTypeTest { @@ -75,4 +78,22 @@ void should_retun_his_uri() { is(URI.create("https://json-schema.org/draft/2020-12/schema")) ); } + + @Test + void should_be_printable() { + assertThat( + new SchemaKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create( + Json + .createObjectBuilder() + .add("$schema", Json.createValue("https://json-schema.org/draft/2020-12/schema")) + .build() + ) + ) + .printOn(new HashMapMedia()), + (Matcher) hasEntry(is("$schema"), is("https://json-schema.org/draft/2020-12/schema")) + ); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/VocabularyKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/VocabularyKeywordTypeTest.java index 776c80ca..429645bf 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/VocabularyKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/VocabularyKeywordTypeTest.java @@ -24,9 +24,12 @@ package io.github.sebastiantoepfer.jsonschema.core.vocab.core; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import io.github.sebastiantoepfer.jsonschema.vocabulary.spi.VocabularyDefinition; @@ -34,6 +37,7 @@ import jakarta.json.Json; import jakarta.json.JsonValue; import java.net.URI; +import org.hamcrest.Matcher; import org.junit.jupiter.api.Test; class VocabularyKeywordTypeTest { @@ -76,4 +80,34 @@ void should_create_definitions() { ) ); } + + @Test + void should_be_printable() { + assertThat( + new VocabularyKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create( + Json + .createObjectBuilder() + .add( + "$vocabulary", + Json + .createObjectBuilder() + .add("https://json-schema.org/draft/2020-12/vocab/core", true) + .add("http://openapi.org/test", false) + ) + .build() + ) + ) + .printOn(new HashMapMedia()), + (Matcher) hasEntry( + is("$vocabulary"), + allOf( + hasEntry("https://json-schema.org/draft/2020-12/vocab/core", true), + hasEntry("http://openapi.org/test", false) + ) + ) + ); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/EnumKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/EnumKeywordTypeTest.java index 6e46307a..40a240af 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/EnumKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/EnumKeywordTypeTest.java @@ -24,12 +24,16 @@ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.Json; import jakarta.json.JsonValue; +import org.hamcrest.Matcher; import org.junit.jupiter.api.Test; class EnumKeywordTypeTest { @@ -97,4 +101,22 @@ void should_be_valid_for_decimal_without_scale_if_number_is_valid() { is(true) ); } + + @Test + void should_be_printable() { + assertThat( + new EnumKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create( + Json + .createObjectBuilder() + .add("enum", Json.createArrayBuilder().add("TEST").add("VALID")) + .build() + ) + ) + .printOn(new HashMapMedia()), + (Matcher) hasEntry(is("enum"), containsInAnyOrder("TEST", "VALID")) + ); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ExclusiveMaximumKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ExclusiveMaximumKeywordTypeTest.java index 9a3fcd2a..0de948cd 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ExclusiveMaximumKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ExclusiveMaximumKeywordTypeTest.java @@ -24,13 +24,17 @@ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.Json; import jakarta.json.JsonValue; +import java.math.BigDecimal; +import org.hamcrest.Matcher; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -111,4 +115,17 @@ void shhould_be_valid_for_smaller_numbers() { is(true) ); } + + @Test + void should_be_printable() { + assertThat( + new ExclusiveMaximumKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create(Json.createObjectBuilder().add("exclusiveMaximum", Json.createValue(10)).build()) + ) + .printOn(new HashMapMedia()), + (Matcher) hasEntry(is("exclusiveMaximum"), is(BigDecimal.valueOf(10))) + ); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ExclusiveMinimumKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ExclusiveMinimumKeywordTypeTest.java index c32da113..6ce0cc52 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ExclusiveMinimumKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ExclusiveMinimumKeywordTypeTest.java @@ -24,13 +24,17 @@ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.Json; import jakarta.json.JsonValue; +import java.math.BigDecimal; +import org.hamcrest.Matcher; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -111,4 +115,17 @@ void shhould_be_valid_for_greater_numbers() { is(true) ); } + + @Test + void should_be_printable() { + assertThat( + new ExclusiveMinimumKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create(Json.createObjectBuilder().add("exclusiveMinimum", Json.createValue(0)).build()) + ) + .printOn(new HashMapMedia()), + (Matcher) hasEntry(is("exclusiveMinimum"), is(BigDecimal.valueOf(0))) + ); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaxItemsKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaxItemsKeywordTypeTest.java index 4c8a6c97..079c45d0 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaxItemsKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaxItemsKeywordTypeTest.java @@ -24,12 +24,16 @@ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.Json; import jakarta.json.JsonValue; +import java.math.BigInteger; +import org.hamcrest.Matcher; import org.junit.jupiter.api.Test; class MaxItemsKeywordTypeTest { @@ -101,4 +105,17 @@ void should_be_invalid_for_array_with_greather_size() { is(false) ); } + + @Test + void should_be_printable() { + assertThat( + new MaxItemsKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create(Json.createObjectBuilder().add("maxItems", Json.createValue(2)).build()) + ) + .printOn(new HashMapMedia()), + (Matcher) hasEntry(is("maxItems"), is(BigInteger.valueOf(2))) + ); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaxLengthKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaxLengthKeywordTypeTest.java index ea4d7d92..ec1cc8d9 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaxLengthKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaxLengthKeywordTypeTest.java @@ -24,14 +24,18 @@ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.assertThrows; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.Json; import jakarta.json.JsonValue; +import java.math.BigInteger; +import org.hamcrest.Matcher; import org.junit.jupiter.api.Test; class MaxLengthKeywordTypeTest { @@ -110,4 +114,17 @@ void should_ne_valid_for_string_with_is_shorter() { is(true) ); } + + @Test + void should_be_printable() { + assertThat( + new MaxLengthKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create(Json.createObjectBuilder().add("maxLength", Json.createValue(2)).build()) + ) + .printOn(new HashMapMedia()), + (Matcher) hasEntry(is("maxLength"), is(BigInteger.valueOf(2L))) + ); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaximumKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaximumKeywordTypeTest.java index 438439db..19d3421e 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaximumKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaximumKeywordTypeTest.java @@ -24,13 +24,17 @@ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.Json; import jakarta.json.JsonValue; +import java.math.BigDecimal; +import org.hamcrest.Matcher; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -111,4 +115,17 @@ void shhould_be_valid_for_smaller_numbers() { is(true) ); } + + @Test + void should_be_printable() { + assertThat( + new MaximumKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create(Json.createObjectBuilder().add("maximum", Json.createValue(10)).build()) + ) + .printOn(new HashMapMedia()), + (Matcher) hasEntry(is("maximum"), is(BigDecimal.valueOf(10))) + ); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinItemsKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinItemsKeywordTypeTest.java index f9bb600b..5d365897 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinItemsKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinItemsKeywordTypeTest.java @@ -24,12 +24,16 @@ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.Json; import jakarta.json.JsonValue; +import java.math.BigInteger; +import org.hamcrest.Matcher; import org.junit.jupiter.api.Test; class MinItemsKeywordTypeTest { @@ -101,4 +105,17 @@ void should_be_invalid_for_arrays_with_smaller_size() { is(false) ); } + + @Test + void should_be_printable() { + assertThat( + new MinItemsKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create(Json.createObjectBuilder().add("minItems", Json.createValue(1)).build()) + ) + .printOn(new HashMapMedia()), + (Matcher) hasEntry(is("minItems"), is(BigInteger.valueOf(1))) + ); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinLengthKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinLengthKeywordTypeTest.java index fdb41375..b815cacc 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinLengthKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinLengthKeywordTypeTest.java @@ -24,14 +24,18 @@ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.assertThrows; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.Json; import jakarta.json.JsonValue; +import java.math.BigInteger; +import org.hamcrest.Matcher; import org.junit.jupiter.api.Test; class MinLengthKeywordTypeTest { @@ -111,4 +115,17 @@ void should_be_valid_for_non_string_values() { is(true) ); } + + @Test + void should_be_printable() { + assertThat( + new MinLengthKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create(Json.createObjectBuilder().add("minLength", Json.createValue(2)).build()) + ) + .printOn(new HashMapMedia()), + (Matcher) hasEntry(is("minLength"), is(BigInteger.valueOf(2L))) + ); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinimumKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinimumKeywordTypeTest.java index 66bae1f5..70b3c68c 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinimumKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinimumKeywordTypeTest.java @@ -24,12 +24,16 @@ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.Json; import jakarta.json.JsonValue; +import java.math.BigDecimal; +import org.hamcrest.Matcher; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -110,4 +114,17 @@ void shhould_be_valid_for_greater_numbers() { is(true) ); } + + @Test + void should_be_printable() { + assertThat( + new MinimumKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create(Json.createObjectBuilder().add("minimum", Json.createValue(0)).build()) + ) + .printOn(new HashMapMedia()), + (Matcher) hasEntry(is("minimum"), is(BigDecimal.valueOf(0))) + ); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MultipleOfKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MultipleOfKeywordTypeTest.java index 29eb19c2..58bce4ca 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MultipleOfKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MultipleOfKeywordTypeTest.java @@ -24,15 +24,18 @@ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.assertThrows; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.Json; import jakarta.json.JsonValue; import java.math.BigDecimal; +import org.hamcrest.Matcher; import org.junit.jupiter.api.Test; class MultipleOfKeywordTypeTest { @@ -117,4 +120,17 @@ void should_be_valid_for_any_int_if_multipleOf_is_1en8() { is(true) ); } + + @Test + void should_be_printable() { + assertThat( + new MultipleOfKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create(Json.createObjectBuilder().add("multipleOf", Json.createValue(2)).build()) + ) + .printOn(new HashMapMedia()), + (Matcher) hasEntry(is("multipleOf"), is(new BigDecimal(2))) + ); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/PatternKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/PatternKeywordTypeTest.java index a0e3740c..915e5fd3 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/PatternKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/PatternKeywordTypeTest.java @@ -24,14 +24,17 @@ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.assertThrows; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.Json; import jakarta.json.JsonValue; +import org.hamcrest.Matcher; import org.junit.jupiter.api.Test; class PatternKeywordTypeTest { @@ -107,4 +110,22 @@ void should_be_valid_matching_value() { is(true) ); } + + @Test + void should_be_printable() { + assertThat( + new PatternKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create( + Json + .createObjectBuilder() + .add("pattern", Json.createValue("^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$")) + .build() + ) + ) + .printOn(new HashMapMedia()), + (Matcher) hasEntry(is("pattern"), is("^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$")) + ); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/RequiredKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/RequiredKeywordTypeTest.java index f91fe85d..333831ba 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/RequiredKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/RequiredKeywordTypeTest.java @@ -24,13 +24,17 @@ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.Json; import jakarta.json.JsonValue; import java.math.BigDecimal; +import org.hamcrest.Matcher; import org.junit.jupiter.api.Test; class RequiredKeywordTypeTest { @@ -103,4 +107,22 @@ void should_valid_if_all_properties_are_in_the_instance() { is(true) ); } + + @Test + void should_be_printable() { + assertThat( + new RequiredKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create( + Json + .createObjectBuilder() + .add("required", Json.createArrayBuilder().add("foo").add("bar")) + .build() + ) + ) + .printOn(new HashMapMedia()), + (Matcher) hasEntry(is("required"), containsInAnyOrder("foo", "bar")) + ); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/UniqueItemsKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/UniqueItemsKeywordTypeTest.java index 746f135d..cd94c86e 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/UniqueItemsKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/UniqueItemsKeywordTypeTest.java @@ -24,9 +24,11 @@ package io.github.sebastiantoepfer.jsonschema.core.vocab.validation; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.assertThrows; +import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import io.github.sebastiantoepfer.jsonschema.JsonSchema; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; @@ -35,6 +37,7 @@ import jakarta.json.JsonValue; import java.io.StringReader; import java.math.BigDecimal; +import org.hamcrest.Matcher; import org.junit.jupiter.api.Test; class UniqueItemsKeywordTypeTest { @@ -147,4 +150,17 @@ void pitests_say_i_must_write_this_tests() { assertThat(obj.equals(number1), is(false)); } + + @Test + void should_be_printable() { + assertThat( + new UniqueItemsKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create(Json.createObjectBuilder().add("uniqueItems", JsonValue.TRUE).build()) + ) + .printOn(new HashMapMedia()), + (Matcher) hasEntry(is("uniqueItems"), is(true)) + ); + } } diff --git a/core/src/test/java/module-info.java b/core/src/test/java/module-info.java index d1e86850..a4d9ec46 100644 --- a/core/src/test/java/module-info.java +++ b/core/src/test/java/module-info.java @@ -24,6 +24,9 @@ open module io.github.sebastiantoepfer.jsonschema.core { requires io.github.sebastiantoepfer.jsonschema; requires io.github.sebastiantoepfer.jsonschema.vocabulary.spi; + requires io.github.sebastiantoepfer.ddd.common; + requires io.github.sebastiantoepfer.ddd.media.json; + requires io.github.sebastiantoepfer.ddd.media.core; requires jakarta.json; requires com.google.errorprone.annotations; diff --git a/pom.xml b/pom.xml index d9e34745..bdf7de8e 100644 --- a/pom.xml +++ b/pom.xml @@ -46,6 +46,8 @@ 2024-01-01T00:00:00Z 17 + + 0.5.0 @@ -57,6 +59,22 @@ + + io.github.sebastian-toepfer.ddd + common + ${ddd.version} + + + io.github.sebastian-toepfer.ddd + media-core + ${ddd.version} + + + io.github.sebastian-toepfer.ddd + media-json-api + ${ddd.version} + + org.eclipse.parsson parsson