-
-
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.
add support for additionalProperties keyword
- Loading branch information
1 parent
a59f9b5
commit 95171fa
Showing
5 changed files
with
279 additions
and
4 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
118 changes: 118 additions & 0 deletions
118
...ub/sebastiantoepfer/jsonschema/core/vocab/applicator/AdditionalPropertiesKeywordType.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,118 @@ | ||
/* | ||
* 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.vocab.applicator; | ||
|
||
import static jakarta.json.stream.JsonCollectors.toJsonArray; | ||
import static java.util.function.Predicate.not; | ||
|
||
import io.github.sebastiantoepfer.jsonschema.InstanceType; | ||
import io.github.sebastiantoepfer.jsonschema.JsonSchema; | ||
import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; | ||
import io.github.sebastiantoepfer.jsonschema.keyword.Annotation; | ||
import io.github.sebastiantoepfer.jsonschema.keyword.Applicator; | ||
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; | ||
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType; | ||
import jakarta.json.Json; | ||
import jakarta.json.JsonObject; | ||
import jakarta.json.JsonString; | ||
import jakarta.json.JsonValue; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
|
||
class AdditionalPropertiesKeywordType implements KeywordType { | ||
|
||
@Override | ||
public String name() { | ||
return "additionalProperties"; | ||
} | ||
|
||
@Override | ||
public Keyword createKeyword(final JsonSchema schema, final JsonValue value) { | ||
return new AdditionalPropertiesKeyword(schema, value); | ||
} | ||
|
||
private class AdditionalPropertiesKeyword implements Applicator, Annotation { | ||
|
||
private final JsonSchema schema; | ||
private final JsonValue additionalProperties; | ||
|
||
public AdditionalPropertiesKeyword(final JsonSchema schema, final JsonValue additionalPropertiesSchema) { | ||
this.schema = schema; | ||
this.additionalProperties = additionalPropertiesSchema; | ||
} | ||
|
||
@Override | ||
public boolean applyTo(final JsonValue instance) { | ||
return !InstanceType.OBJECT.isInstance(instance) || additionalPropertiesMatches(instance.asJsonObject()); | ||
} | ||
|
||
private boolean additionalPropertiesMatches(final JsonObject instance) { | ||
final JsonSchema additionalPropertiesSchema = new DefaultJsonSchemaFactory().create(additionalProperties); | ||
return findPropertiesForValidation(instance) | ||
.map(Map.Entry::getValue) | ||
.allMatch(value -> additionalPropertiesSchema.validator().validate(value).isEmpty()); | ||
} | ||
|
||
@Override | ||
public JsonValue valueFor(final JsonValue instance) { | ||
return findPropertiesForValidation(instance.asJsonObject()) | ||
.map(Map.Entry::getKey) | ||
.map(Json::createValue) | ||
.collect(toJsonArray()); | ||
} | ||
|
||
private Stream<Map.Entry<String, JsonValue>> findPropertiesForValidation(final JsonObject instance) { | ||
final Collection<String> ignoredProperties = findePropertyNamesAlreadyConveredByOthersIn(instance); | ||
return instance.entrySet().stream().filter(not(e -> ignoredProperties.contains(e.getKey()))); | ||
} | ||
|
||
private Collection<String> findePropertyNamesAlreadyConveredByOthersIn(final JsonValue instance) { | ||
return Stream | ||
.of(schema.keywordByName("properties"), schema.keywordByName("patternProperties")) | ||
.flatMap(Optional::stream) | ||
.map(Keyword::asAnnotation) | ||
.map(anno -> anno.valueFor(instance)) | ||
.map(JsonValue::asJsonArray) | ||
.flatMap(Collection::stream) | ||
.filter(JsonString.class::isInstance) | ||
.map(JsonString.class::cast) | ||
.map(JsonString::getString) | ||
.toList(); | ||
} | ||
|
||
@Override | ||
public boolean hasName(final String name) { | ||
return Objects.equals(name(), name); | ||
} | ||
|
||
@Override | ||
public Collection<KeywordCategory> categories() { | ||
return List.of(Keyword.KeywordCategory.APPLICATOR, Keyword.KeywordCategory.ANNOTATION); | ||
} | ||
} | ||
} |
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
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
157 changes: 157 additions & 0 deletions
157
...ebastiantoepfer/jsonschema/core/vocab/applicator/AdditionalPropertiesKeywordTypeTest.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,157 @@ | ||
/* | ||
* 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.vocab.applicator; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsInAnyOrder; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; | ||
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; | ||
import jakarta.json.Json; | ||
import jakarta.json.JsonValue; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class AdditionalPropertiesKeywordTypeTest { | ||
|
||
@Test | ||
void should_know_his_name() { | ||
final Keyword keyword = new AdditionalPropertiesKeywordType() | ||
.createKeyword(new DefaultJsonSchemaFactory().create(JsonValue.TRUE), JsonValue.EMPTY_JSON_OBJECT); | ||
|
||
assertThat(keyword.hasName("additionalProperties"), is(true)); | ||
assertThat(keyword.hasName("test"), is(false)); | ||
} | ||
|
||
@Test | ||
void should_be_valid_for_non_objects() { | ||
assertThat( | ||
new AdditionalPropertiesKeywordType() | ||
.createKeyword( | ||
new DefaultJsonSchemaFactory() | ||
.create( | ||
Json | ||
.createObjectBuilder() | ||
.add("properties", Json.createObjectBuilder().add("test", JsonValue.TRUE)) | ||
.build() | ||
), | ||
JsonValue.FALSE | ||
) | ||
.asApplicator() | ||
.applyTo(JsonValue.EMPTY_JSON_ARRAY), | ||
is(true) | ||
); | ||
} | ||
|
||
@Test | ||
void should_not_valid_if_no_additionals_are_allow() { | ||
assertThat( | ||
new AdditionalPropertiesKeywordType() | ||
.createKeyword( | ||
new DefaultJsonSchemaFactory() | ||
.create( | ||
Json | ||
.createObjectBuilder() | ||
.add("properties", Json.createObjectBuilder().add("test", JsonValue.TRUE)) | ||
.build() | ||
), | ||
JsonValue.FALSE | ||
) | ||
.asApplicator() | ||
.applyTo(Json.createObjectBuilder().add("test", 1).add("foo", 1).build()), | ||
is(false) | ||
); | ||
} | ||
|
||
@Test | ||
void should_valid_if_additionals_are_allow() { | ||
assertThat( | ||
new AdditionalPropertiesKeywordType() | ||
.createKeyword( | ||
new DefaultJsonSchemaFactory() | ||
.create( | ||
Json | ||
.createObjectBuilder() | ||
.add("properties", Json.createObjectBuilder().add("test", JsonValue.TRUE)) | ||
.build() | ||
), | ||
JsonValue.TRUE | ||
) | ||
.asApplicator() | ||
.applyTo(Json.createObjectBuilder().add("test", 1).add("foo", 1).build()), | ||
is(true) | ||
); | ||
} | ||
|
||
@Test | ||
void should_valid_if_no_additionals_are_allow_and_no_additionals_their() { | ||
assertThat( | ||
new AdditionalPropertiesKeywordType() | ||
.createKeyword( | ||
new DefaultJsonSchemaFactory() | ||
.create( | ||
Json | ||
.createObjectBuilder() | ||
.add("properties", Json.createObjectBuilder().add("test", JsonValue.TRUE)) | ||
.build() | ||
), | ||
JsonValue.FALSE | ||
) | ||
.asApplicator() | ||
.applyTo(Json.createObjectBuilder().add("test", 1).build()), | ||
is(true) | ||
); | ||
} | ||
|
||
@Test | ||
void should_be_an_applicator_and_an_annotation() { | ||
assertThat( | ||
new AdditionalPropertiesKeywordType() | ||
.createKeyword(new DefaultJsonSchemaFactory().create(JsonValue.TRUE), JsonValue.TRUE) | ||
.categories(), | ||
containsInAnyOrder(Keyword.KeywordCategory.APPLICATOR, Keyword.KeywordCategory.ANNOTATION) | ||
); | ||
} | ||
|
||
@Test | ||
void should_return_propertynames_which_will_be_validated() { | ||
assertThat( | ||
new AdditionalPropertiesKeywordType() | ||
.createKeyword( | ||
new DefaultJsonSchemaFactory() | ||
.create( | ||
Json | ||
.createObjectBuilder() | ||
.add("properties", Json.createObjectBuilder().add("test", JsonValue.TRUE)) | ||
.build() | ||
), | ||
JsonValue.TRUE | ||
) | ||
.asAnnotation() | ||
.valueFor(Json.createObjectBuilder().add("test", 1).add("foo", 1).build()) | ||
.asJsonArray(), | ||
containsInAnyOrder(Json.createValue("foo")) | ||
); | ||
} | ||
} |