Skip to content

Commit

Permalink
add support for additionalProperties keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-toepfer committed Oct 9, 2023
1 parent a59f9b5 commit 95171fa
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 4 deletions.
3 changes: 1 addition & 2 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,8 @@
<inculde>**/tests/draft2020-12/minItems.json</inculde>
<inculde>**/tests/draft2020-12/maxItems.json</inculde>
<inculde>**/tests/draft2020-12/patternProperties.json</inculde>
<!-- needs additionalProperty
<inculde>**/tests/draft2020-12/properties.json</inculde>
-->
<inculde>**/tests/draft2020-12/additionalProperties.json</inculde>
</includes>
</resource>
</resources>
Expand Down
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public ApplicatorVocabulary() {
new DefaultVocabulary(
URI.create("https://json-schema.org/draft/2020-12/vocab/applicator"),
new PropertiesKeywordType(),
new AdditionalPropertiesKeywordType(),
new PatternPropertiesKeywordType(),
new ItemsKeywordType(),
new PrefixItemsKeywordType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ private boolean propertyMatches(final Map.Entry<String, JsonValue> property) {
}

@Override
public JsonValue valueFor(final JsonValue value) {
return value
public JsonValue valueFor(final JsonValue instance) {
return instance
.asJsonObject()
.keySet()
.stream()
Expand Down
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"))
);
}
}

0 comments on commit 95171fa

Please sign in to comment.