-
-
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 #128 from sebastian-toepfer/keyword_oneOf
Keyword one of
- Loading branch information
Showing
4 changed files
with
269 additions
and
3 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
70 changes: 70 additions & 0 deletions
70
...c/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/OneOfKeyword.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,70 @@ | ||
/* | ||
* 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.JsonSchema; | ||
import io.github.sebastiantoepfer.jsonschema.keyword.Applicator; | ||
import jakarta.json.JsonValue; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
/** | ||
* <b>oneOf</b> : <i>Array<Schema></i><br/> | ||
* An instance validates successfully against this keyword if it validates successfully against exactly one schema | ||
* defined by this keyword’s value.<br/> | ||
* <br/> | ||
* <b>kind</b> | ||
* <ul> | ||
* <li>Applicator</li> | ||
* </ul> | ||
* | ||
* source: https://www.learnjsonschema.com/2020-12/applicator/oneof/ | ||
* spec: https://json-schema.org/draft/2020-12/json-schema-core.html#section-10.2.1.3 | ||
*/ | ||
final class OneOfKeyword implements Applicator { | ||
|
||
static final String NAME = "oneOf"; | ||
private final Collection<JsonSchema> schemas; | ||
|
||
public OneOfKeyword(final Collection<JsonSchema> schemas) { | ||
this.schemas = List.copyOf(schemas); | ||
} | ||
|
||
@Override | ||
public boolean applyTo(final JsonValue instance) { | ||
return schemas.stream().map(JsonSchema::validator).filter(v -> v.isValid(instance)).limit(2).count() == 1; | ||
} | ||
|
||
@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, schemas); | ||
} | ||
} |
197 changes: 197 additions & 0 deletions
197
...st/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/OneOfKeywordTest.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,197 @@ | ||
/* | ||
* 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.hasItem; | ||
import static org.hamcrest.Matchers.hasKey; | ||
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.SchemaArrayKeywordType; | ||
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; | ||
import jakarta.json.Json; | ||
import jakarta.json.JsonObject; | ||
import jakarta.json.JsonValue; | ||
import org.hamcrest.Matcher; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class OneOfKeywordTest { | ||
|
||
@Test | ||
void should_know_his_name() { | ||
final Keyword items = createKeywordFrom( | ||
Json.createObjectBuilder().add("oneOf", Json.createArrayBuilder().add(JsonValue.TRUE)).build() | ||
); | ||
|
||
assertThat(items.hasName("oneOf"), is(true)); | ||
assertThat(items.hasName("test"), is(false)); | ||
} | ||
|
||
@Test | ||
void should_be_printable() { | ||
assertThat( | ||
createKeywordFrom( | ||
Json.createObjectBuilder() | ||
.add( | ||
"oneOf", | ||
Json.createArrayBuilder() | ||
.add( | ||
Json.createObjectBuilder() | ||
.add( | ||
"properties", | ||
Json.createObjectBuilder() | ||
.add("foo", Json.createObjectBuilder().add("type", "string")) | ||
) | ||
.add("required", Json.createArrayBuilder().add("foo")) | ||
) | ||
.add( | ||
Json.createObjectBuilder() | ||
.add( | ||
"properties", | ||
Json.createObjectBuilder() | ||
.add("bar", Json.createObjectBuilder().add("type", "number")) | ||
) | ||
.add("required", Json.createArrayBuilder().add("bar")) | ||
) | ||
) | ||
.build() | ||
).printOn(new HashMapMedia()), | ||
(Matcher) hasEntry(is("oneOf"), hasItem((hasKey("properties")))) | ||
); | ||
} | ||
|
||
@Test | ||
void should_be_valid_if_exactly_one_schema_applies() { | ||
assertThat( | ||
createKeywordFrom( | ||
Json.createObjectBuilder() | ||
.add( | ||
"oneOf", | ||
Json.createArrayBuilder() | ||
.add( | ||
Json.createObjectBuilder() | ||
.add( | ||
"properties", | ||
Json.createObjectBuilder() | ||
.add("foo", Json.createObjectBuilder().add("type", "string")) | ||
) | ||
.add("required", Json.createArrayBuilder().add("foo")) | ||
) | ||
.add( | ||
Json.createObjectBuilder() | ||
.add( | ||
"properties", | ||
Json.createObjectBuilder() | ||
.add("bar", Json.createObjectBuilder().add("type", "number")) | ||
) | ||
.add("required", Json.createArrayBuilder().add("bar")) | ||
) | ||
) | ||
.build() | ||
) | ||
.asApplicator() | ||
.applyTo(Json.createObjectBuilder().add("foo", "foo").build()), | ||
is(true) | ||
); | ||
} | ||
|
||
@Test | ||
void should_be_invalid_if_none_schema_not_apply() { | ||
assertThat( | ||
createKeywordFrom( | ||
Json.createObjectBuilder() | ||
.add( | ||
"oneOf", | ||
Json.createArrayBuilder() | ||
.add( | ||
Json.createObjectBuilder() | ||
.add( | ||
"properties", | ||
Json.createObjectBuilder() | ||
.add("foo", Json.createObjectBuilder().add("type", "string")) | ||
) | ||
.add("required", Json.createArrayBuilder().add("foo")) | ||
) | ||
.add( | ||
Json.createObjectBuilder() | ||
.add( | ||
"properties", | ||
Json.createObjectBuilder() | ||
.add("bar", Json.createObjectBuilder().add("type", "number")) | ||
) | ||
.add("required", Json.createArrayBuilder().add("bar")) | ||
) | ||
) | ||
.build() | ||
) | ||
.asApplicator() | ||
.applyTo(Json.createObjectBuilder().add("foo", 3).add("bar", "bar").build()), | ||
is(false) | ||
); | ||
} | ||
|
||
@Test | ||
void should_be_invalid_if_more_than_one_schema_apply() { | ||
assertThat( | ||
createKeywordFrom( | ||
Json.createObjectBuilder() | ||
.add( | ||
"oneOf", | ||
Json.createArrayBuilder() | ||
.add( | ||
Json.createObjectBuilder() | ||
.add( | ||
"properties", | ||
Json.createObjectBuilder() | ||
.add("foo", Json.createObjectBuilder().add("type", "string")) | ||
) | ||
.add("required", Json.createArrayBuilder().add("foo")) | ||
) | ||
.add( | ||
Json.createObjectBuilder() | ||
.add( | ||
"properties", | ||
Json.createObjectBuilder() | ||
.add("bar", Json.createObjectBuilder().add("type", "number")) | ||
) | ||
.add("required", Json.createArrayBuilder().add("bar")) | ||
) | ||
) | ||
.build() | ||
) | ||
.asApplicator() | ||
.applyTo(Json.createObjectBuilder().add("foo", "foo").add("bar", 33).build()), | ||
is(false) | ||
); | ||
} | ||
|
||
private static Keyword createKeywordFrom(final JsonObject json) { | ||
return new SchemaArrayKeywordType("oneOf", OneOfKeyword::new).createKeyword( | ||
new DefaultJsonSchemaFactory().create(json) | ||
); | ||
} | ||
} |