Skip to content

Commit

Permalink
Merge branch 'main' into fix_refkeyword
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-toepfer committed Jun 12, 2024
2 parents 653df21 + d81242a commit 92b4514
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 4 deletions.
1 change: 1 addition & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
<include>**/tests/draft2020-12/patternProperties.json</include>
<include>**/tests/draft2020-12/prefixItems.json</include>
<include>**/tests/draft2020-12/properties.json</include>
<include>**/tests/draft2020-12/propertyNames.json</include>
<!--
needs allOf, $anchor and dynamicAnchor and some other stuff :(
<include>**/tests/draft2020-12/ref.json</include>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ final class Keywords {
.collect(toMap(Vocabulary::id, Function.identity()));

DEFAULT_VOCABS = List.of(
new ApplicatorVocabulary(),
new ApplicatorVocabulary(JsonProvider.provider()),
new ValidationVocabulary(JsonProvider.provider()),
new MetaDataVocabulary(),
new FormatAnnotationVocabulary(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public final class OfficialVocabularies implements LazyVocabularies {
static {
OFFICEAL_VOCABS = List.of(
new CoreVocabulary(JSONP),
new ApplicatorVocabulary(),
new ApplicatorVocabulary(JSONP),
new ValidationVocabulary(JSONP),
new MetaDataVocabulary(),
new FormatAnnotationVocabulary(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import io.github.sebastiantoepfer.jsonschema.vocabulary.spi.ListVocabulary;
import jakarta.json.Json;
import jakarta.json.JsonValue;
import jakarta.json.spi.JsonProvider;
import java.net.URI;
import java.util.List;
import java.util.Optional;
Expand All @@ -51,7 +52,7 @@ public final class ApplicatorVocabulary implements Vocabulary {

private final Vocabulary vocab;

public ApplicatorVocabulary() {
public ApplicatorVocabulary(final JsonProvider provider) {
this.vocab = new ListVocabulary(
URI.create("https://json-schema.org/draft/2020-12/vocab/applicator"),
new SchemaArrayKeywordType(AllOfKeyword.NAME, AllOfKeyword::new),
Expand All @@ -74,6 +75,7 @@ public ApplicatorVocabulary() {
),
new NamedJsonSchemaKeywordType(PatternPropertiesKeyword.NAME, PatternPropertiesKeyword::new),
new NamedJsonSchemaKeywordType(DependentSchemasKeyword.NAME, DependentSchemasKeyword::new),
new SubSchemaKeywordType(PropertyNames.NAME, s -> new PropertyNames(provider, s)),
//this example shows my missunderstanding from affects, affectedBy and keywordtypes :(
new AffectedByKeywordType(
ItemsKeyword.NAME,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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.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.Validator;
import io.github.sebastiantoepfer.jsonschema.keyword.Applicator;
import jakarta.json.JsonObject;
import jakarta.json.JsonValue;
import jakarta.json.spi.JsonProvider;
import java.util.Objects;

/**
* <b>propertyNames</b> : <i>Schema</i><br/>
* Validation succeeds if the schema validates against every property name in the instance.<br/>
* <br/>
* <b>Kind</b><br/>
* <ul>
* <li>Applicator</li>
* </li>
* </ul>
*
* source: https://www.learnjsonschema.com/2020-12/applicator/propertynames/
* spec: https://json-schema.org/draft/2020-12/json-schema-core.html#section-10.3.2.4
*/
final class PropertyNames implements Applicator {

static final String NAME = "propertyNames";
private final JsonProvider provider;
private final JsonSchema names;

public PropertyNames(final JsonProvider provider, final JsonSchema names) {
this.provider = Objects.requireNonNull(provider);
this.names = Objects.requireNonNull(names);
}

@Override
public <T extends Media<T>> T printOn(final T media) {
return media.withValue(NAME, names);
}

@Override
public boolean hasName(final String name) {
return Objects.equals(NAME, name);
}

@Override
public boolean applyTo(final JsonValue instance) {
return !InstanceType.OBJECT.isInstance(instance) || allProperyNamesMatchesSchema(instance.asJsonObject());
}

private boolean allProperyNamesMatchesSchema(final JsonObject obj) {
final Validator validator = names.validator();
return obj.keySet().stream().map(provider::createValue).allMatch(validator::isValid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@
import static org.hamcrest.MatcherAssert.assertThat;

import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
import jakarta.json.spi.JsonProvider;
import org.junit.jupiter.api.Test;

class ApplicatorVocabularyTest {

@Test
void should_find_items_keyword() {
assertThat(
new ApplicatorVocabulary().findKeywordTypeByName("items").map(KeywordType::name),
new ApplicatorVocabulary(JsonProvider.provider()).findKeywordTypeByName("items").map(KeywordType::name),
isPresentAndIs("items")
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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.applicator;

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.JsonSchemas;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.Json;
import jakarta.json.JsonValue;
import jakarta.json.spi.JsonProvider;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

class PropertyNamesTest {

@Test
void should_be_know_his_name() {
final Keyword keyword = new PropertyNames(JsonProvider.provider(), JsonSchemas.load(JsonValue.TRUE));

assertThat(keyword.hasName("propertyNames"), is(true));
assertThat(keyword.hasName("test"), is(false));
}

@Test
void should_be_printable() {
assertThat(
new PropertyNames(JsonProvider.provider(), JsonSchemas.load(JsonValue.TRUE)).printOn(new HashMapMedia()),
hasEntry(is("propertyNames"), (Matcher) Matchers.anEmptyMap())
);
}

@Test
void should_be_valid_for_non_objects() {
assertThat(
new PropertyNames(JsonProvider.provider(), JsonSchemas.load(JsonValue.TRUE))
.asApplicator()
.applyTo(JsonValue.EMPTY_JSON_ARRAY),
is(true)
);
}

@Test
void should_be_valid_if_schema_applies_to_all_propertyNames() {
assertThat(
new PropertyNames(
JsonProvider.provider(),
JsonSchemas.load(Json.createObjectBuilder().add("maxLength", 3).build())
)
.asApplicator()
.applyTo(Json.createObjectBuilder().add("foo", "foo").add("bar", 33).build()),
is(true)
);
}

@Test
void should_be_invalid_if_schema_does_not_applies_to_any_propertyName() {
assertThat(
new PropertyNames(
JsonProvider.provider(),
JsonSchemas.load(Json.createObjectBuilder().add("maxLength", 3).build())
)
.asApplicator()
.applyTo(Json.createObjectBuilder().add("name", "John Doe").add("age", 21).build()),
is(false)
);
}
}

0 comments on commit 92b4514

Please sign in to comment.