-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5cd9e00
commit f1375de
Showing
5 changed files
with
285 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/AnyKeywordType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2024 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.keyword.type; | ||
|
||
import io.github.sebastiantoepfer.jsonschema.JsonSchema; | ||
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; | ||
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType; | ||
import jakarta.json.JsonValue; | ||
import java.util.Objects; | ||
import java.util.function.Function; | ||
|
||
public class AnyKeywordType implements KeywordType { | ||
|
||
private final String name; | ||
private final Function<JsonValue, Keyword> keywordCreator; | ||
|
||
public AnyKeywordType(final String name, final Function<JsonValue, Keyword> keywordCreator) { | ||
this.name = Objects.requireNonNull(name); | ||
this.keywordCreator = Objects.requireNonNull(keywordCreator); | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public Keyword createKeyword(final JsonSchema schema) { | ||
return keywordCreator.apply(schema.asJsonObject().getOrDefault(name, JsonValue.NULL)); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
...c/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ConstKeyword.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2024 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.vocab.validation; | ||
|
||
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.keyword.Assertion; | ||
import jakarta.json.Json; | ||
import jakarta.json.JsonNumber; | ||
import jakarta.json.JsonValue; | ||
import java.util.Objects; | ||
|
||
/** | ||
* <b>const</b> : <i>Any</i><br/> | ||
* Validation succeeds if the instance is equal to this keyword’s value.<br/> | ||
* <br/> | ||
* <ul> | ||
* <li>assertion</li> | ||
* </ul> | ||
* | ||
* source: https://www.learnjsonschema.com/2020-12/validation/const/ | ||
* spec: https://json-schema.org/draft/2020-12/json-schema-validation.html#section-6.1.3 | ||
*/ | ||
class ConstKeyword implements Assertion { | ||
|
||
static final String NAME = "const"; | ||
private final JsonValue allowedValue; | ||
|
||
public ConstKeyword(final JsonValue allowedValue) { | ||
this.allowedValue = Objects.requireNonNull(allowedValue); | ||
} | ||
|
||
@Override | ||
public boolean isValidFor(final JsonValue instance) { | ||
return ( | ||
instance != null && allowedValue.getValueType() == instance.getValueType() && isEqualsToAllowed(instance) | ||
); | ||
} | ||
|
||
@SuppressWarnings("BigDecimalEquals") | ||
private boolean isEqualsToAllowed(final JsonValue instance) { | ||
final boolean result; | ||
if (InstanceType.NUMBER.isInstance(instance)) { | ||
result = ((JsonNumber) instance).bigDecimalValue() | ||
.stripTrailingZeros() | ||
.equals(((JsonNumber) allowedValue).bigDecimalValue().stripTrailingZeros()); | ||
} else { | ||
result = Objects.equals(allowedValue, instance); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean hasName(final String name) { | ||
return Objects.equals(NAME, name); | ||
} | ||
|
||
@Override | ||
public <T extends Media<T>> T printOn(final T media) { | ||
return new JsonObjectPrintable(Json.createObjectBuilder().add(NAME, allowedValue).build()).printOn(media); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
...st/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ConstKeywordTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2024 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.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.core.keyword.type.AnyKeywordType; | ||
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; | ||
import jakarta.json.Json; | ||
import jakarta.json.JsonObject; | ||
import jakarta.json.JsonValue; | ||
import org.hamcrest.Matcher; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ConstKeywordTest { | ||
|
||
@Test | ||
void should_know_his_name() { | ||
final Keyword enumKeyword = createKeywordFrom( | ||
Json.createObjectBuilder().add("const", JsonValue.EMPTY_JSON_ARRAY).build() | ||
); | ||
|
||
assertThat(enumKeyword.hasName("const"), is(true)); | ||
assertThat(enumKeyword.hasName("test"), is(false)); | ||
} | ||
|
||
@Test | ||
void should_be_valid_for_same_string_value() { | ||
assertThat( | ||
createKeywordFrom(Json.createObjectBuilder().add("const", Json.createValue("hello")).build()) | ||
.asAssertion() | ||
.isValidFor(Json.createValue("hello")), | ||
is(true) | ||
); | ||
} | ||
|
||
@Test | ||
void should_be_invalid_for_different_string_value() { | ||
assertThat( | ||
createKeywordFrom(Json.createObjectBuilder().add("const", Json.createValue("hello")).build()) | ||
.asAssertion() | ||
.isValidFor(Json.createValue("world")), | ||
is(false) | ||
); | ||
} | ||
|
||
@Test | ||
void should_be_valid_for_same_number_value() { | ||
assertThat( | ||
createKeywordFrom(Json.createObjectBuilder().add("const", Json.createValue(3.14159)).build()) | ||
.asAssertion() | ||
.isValidFor(Json.createValue(3.14159)), | ||
is(true) | ||
); | ||
} | ||
|
||
@Test | ||
void should_be_invalid_for_value_with_different_type() { | ||
assertThat( | ||
createKeywordFrom(Json.createObjectBuilder().add("const", Json.createValue(3.14159)).build()) | ||
.asAssertion() | ||
.isValidFor(Json.createValue("pi")), | ||
is(false) | ||
); | ||
} | ||
|
||
@Test | ||
void should_be_valid_for_exact_object_structure() { | ||
assertThat( | ||
createKeywordFrom( | ||
Json.createObjectBuilder() | ||
.add("const", Json.createObjectBuilder().add("name", "John Doe").add("age", 31)) | ||
.build() | ||
) | ||
.asAssertion() | ||
.isValidFor(Json.createObjectBuilder().add("name", "John Doe").add("age", 31).build()), | ||
is(true) | ||
); | ||
} | ||
|
||
@Test | ||
void should_be_invalid_for_not_exact_object_structure() { | ||
assertThat( | ||
createKeywordFrom( | ||
Json.createObjectBuilder() | ||
.add("const", Json.createObjectBuilder().add("name", "John Doe").add("age", 31)) | ||
.build() | ||
) | ||
.asAssertion() | ||
.isValidFor(Json.createObjectBuilder().add("name", "Robert").add("age", 31).build()), | ||
is(false) | ||
); | ||
} | ||
|
||
@Test | ||
void should_be_valid_for_decimal_without_scale_if_number_is_valid() { | ||
assertThat( | ||
createKeywordFrom(Json.createObjectBuilder().add("const", 1).build()) | ||
.asAssertion() | ||
.isValidFor(Json.createValue(1.0)), | ||
is(true) | ||
); | ||
} | ||
|
||
@Test | ||
void should_be_printable() { | ||
assertThat( | ||
createKeywordFrom( | ||
Json.createObjectBuilder().add("const", Json.createArrayBuilder().add("TEST").add("VALID")).build() | ||
).printOn(new HashMapMedia()), | ||
(Matcher) hasEntry(is("const"), containsInAnyOrder("TEST", "VALID")) | ||
); | ||
} | ||
|
||
private static Keyword createKeywordFrom(final JsonObject json) { | ||
return new AnyKeywordType("const", ConstKeyword::new).createKeyword( | ||
new DefaultJsonSchemaFactory().create(json) | ||
); | ||
} | ||
} |