-
-
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 branch 'main' into fix_refkeyword
- Loading branch information
Showing
7 changed files
with
180 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
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
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
79 changes: 79 additions & 0 deletions
79
.../main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PropertyNames.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,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); | ||
} | ||
} |
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
93 changes: 93 additions & 0 deletions
93
...t/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PropertyNamesTest.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,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) | ||
); | ||
} | ||
} |