-
-
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.
Merge pull request #120 from sebastian-toepfer/keyword_maxProperties
add support for maxProperties keyword
- Loading branch information
Showing
4 changed files
with
183 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
74 changes: 74 additions & 0 deletions
74
...ava/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaxPropertiesKeyword.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,74 @@ | ||
/* | ||
* 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.jsonschema.InstanceType; | ||
import io.github.sebastiantoepfer.jsonschema.keyword.Assertion; | ||
import jakarta.json.JsonObject; | ||
import jakarta.json.JsonValue; | ||
import java.math.BigInteger; | ||
import java.util.Objects; | ||
|
||
/** | ||
* /** | ||
* <b>maxProperties</b> : <i>Integer</i> | ||
* An object instance is valid if its number of properties is less than, or equal to, the value of this keyword.<br/> | ||
* keyword.<br/> | ||
* <br/> | ||
* <ul> | ||
* <li>assertion</li> | ||
* </ul> | ||
* | ||
* source: https://www.learnjsonschema.com/2020-12/validation/maxproperties/ | ||
* spec: https://json-schema.org/draft/2020-12/json-schema-validation.html#section-6.5.1 | ||
*/ | ||
final class MaxPropertiesKeyword implements Assertion { | ||
|
||
static final String NAME = "maxProperties"; | ||
private final BigInteger maxProperties; | ||
|
||
public MaxPropertiesKeyword(final BigInteger maxProperties) { | ||
this.maxProperties = maxProperties; | ||
} | ||
|
||
@Override | ||
public boolean hasName(final String name) { | ||
return Objects.equals(NAME, name); | ||
} | ||
|
||
@Override | ||
public <T extends Media<T>> T printOn(final T media) { | ||
return media.withValue(NAME, maxProperties); | ||
} | ||
|
||
@Override | ||
public boolean isValidFor(final JsonValue instance) { | ||
return !InstanceType.OBJECT.isInstance(instance) || hasMaxProperties(instance.asJsonObject()); | ||
} | ||
|
||
private boolean hasMaxProperties(final JsonObject object) { | ||
return object.keySet().size() <= maxProperties.intValue(); | ||
} | ||
} |
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
107 changes: 107 additions & 0 deletions
107
...io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaxPropertiesKeywordTest.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,107 @@ | ||
/* | ||
* 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.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.IntegerKeywordType; | ||
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; | ||
import jakarta.json.Json; | ||
import jakarta.json.JsonObject; | ||
import jakarta.json.JsonValue; | ||
import jakarta.json.spi.JsonProvider; | ||
import java.math.BigInteger; | ||
import org.hamcrest.Matcher; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class MaxPropertiesKeywordTest { | ||
|
||
@Test | ||
void should_know_his_name() { | ||
final Keyword enumKeyword = createKeywordFrom(Json.createObjectBuilder().add("maxProperties", 2).build()); | ||
|
||
assertThat(enumKeyword.hasName("maxProperties"), is(true)); | ||
assertThat(enumKeyword.hasName("test"), is(false)); | ||
} | ||
|
||
@Test | ||
void should_be_printable() { | ||
assertThat( | ||
createKeywordFrom(Json.createObjectBuilder().add("maxProperties", 2).build()).printOn(new HashMapMedia()), | ||
(Matcher) hasEntry(is("maxProperties"), is(BigInteger.valueOf(2))) | ||
); | ||
} | ||
|
||
@Test | ||
void should_be_valid_for_non_objects() { | ||
assertThat( | ||
createKeywordFrom(Json.createObjectBuilder().add("maxProperties", 2).build()) | ||
.asAssertion() | ||
.isValidFor(JsonValue.EMPTY_JSON_ARRAY), | ||
is(true) | ||
); | ||
} | ||
|
||
@Test | ||
void should_be_valid_for_less_properties() { | ||
assertThat( | ||
createKeywordFrom(Json.createObjectBuilder().add("maxProperties", 2).build()) | ||
.asAssertion() | ||
.isValidFor(Json.createObjectBuilder().add("foo", 3).build()), | ||
is(true) | ||
); | ||
} | ||
|
||
@Test | ||
void should_be_valid_exact_properties_count() { | ||
assertThat( | ||
createKeywordFrom(Json.createObjectBuilder().add("maxProperties", 2).build()) | ||
.asAssertion() | ||
.isValidFor(Json.createObjectBuilder().add("foo", 3).add("bar", "hi").build()), | ||
is(true) | ||
); | ||
} | ||
|
||
@Test | ||
void should_be_invalid_for_more_properties() { | ||
assertThat( | ||
createKeywordFrom(Json.createObjectBuilder().add("maxProperties", 2).build()) | ||
.asAssertion() | ||
.isValidFor(Json.createObjectBuilder().add("foo", 3).add("bar", "hi").add("baz", true).build()), | ||
is(false) | ||
); | ||
} | ||
|
||
private static Keyword createKeywordFrom(final JsonObject json) { | ||
return new IntegerKeywordType( | ||
JsonProvider.provider(), | ||
"maxProperties", | ||
MaxPropertiesKeyword::new | ||
).createKeyword(new DefaultJsonSchemaFactory().create(json)); | ||
} | ||
} |