-
-
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 #123 from sebastian-toepfer/keyword_maxContains
Keyword max contains
- Loading branch information
Showing
7 changed files
with
624 additions
and
1 deletion.
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
105 changes: 105 additions & 0 deletions
105
...ain/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ContainsKeyword.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,105 @@ | ||
/* | ||
* 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 jakarta.json.stream.JsonCollectors.toJsonArray; | ||
|
||
import io.github.sebastiantoepfer.ddd.common.Media; | ||
import io.github.sebastiantoepfer.jsonschema.InstanceType; | ||
import io.github.sebastiantoepfer.jsonschema.JsonSubSchema; | ||
import io.github.sebastiantoepfer.jsonschema.keyword.Annotation; | ||
import io.github.sebastiantoepfer.jsonschema.keyword.Applicator; | ||
import jakarta.json.JsonArray; | ||
import jakarta.json.JsonValue; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
/** | ||
* <b>contains</b> : <i>Schema</i> | ||
* Validation succeeds if the instance contains an element that validates against this schema.<br/> | ||
* <br/> | ||
* <ul> | ||
* <li>applicator</li> | ||
* <li>annotation</li> | ||
* </ul> | ||
* | ||
* source: https://www.learnjsonschema.com/2020-12/applicator/contains/ | ||
* spec: https://json-schema.org/draft/2020-12/json-schema-core.html#section-10.3.1.3 | ||
*/ | ||
final class ContainsKeyword implements Applicator, Annotation { | ||
|
||
static final String NAME = "contains"; | ||
private final JsonSubSchema contains; | ||
|
||
public ContainsKeyword(final JsonSubSchema contains) { | ||
this.contains = Objects.requireNonNull(contains); | ||
} | ||
|
||
@Override | ||
public Collection<KeywordCategory> categories() { | ||
return List.of(KeywordCategory.APPLICATOR, KeywordCategory.ANNOTATION); | ||
} | ||
|
||
@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, contains); | ||
} | ||
|
||
@Override | ||
public boolean applyTo(final JsonValue instance) { | ||
return !InstanceType.ARRAY.isInstance(instance) || contains(instance.asJsonArray()); | ||
} | ||
|
||
private boolean contains(final JsonArray array) { | ||
return array.stream().anyMatch(contains.validator()::isValid); | ||
} | ||
|
||
@Override | ||
public JsonValue valueFor(final JsonValue value) { | ||
final JsonValue result; | ||
if (InstanceType.ARRAY.isInstance(value)) { | ||
result = valueFor(value.asJsonArray()); | ||
} else { | ||
result = JsonValue.FALSE; | ||
} | ||
return result; | ||
} | ||
|
||
private JsonValue valueFor(final JsonArray values) { | ||
final JsonValue result; | ||
final JsonArray matchingItems = values.stream().filter(contains.validator()::isValid).collect(toJsonArray()); | ||
if (matchingItems.size() == values.size()) { | ||
result = JsonValue.TRUE; | ||
} else { | ||
result = matchingItems; | ||
} | ||
return result; | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
...a/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaxContainsKeywordType.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,116 @@ | ||
/* | ||
* 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.validation; | ||
|
||
import io.github.sebastiantoepfer.ddd.common.Media; | ||
import io.github.sebastiantoepfer.jsonschema.InstanceType; | ||
import io.github.sebastiantoepfer.jsonschema.JsonSchema; | ||
import io.github.sebastiantoepfer.jsonschema.core.keyword.type.IntegerKeywordType; | ||
import io.github.sebastiantoepfer.jsonschema.keyword.Assertion; | ||
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; | ||
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType; | ||
import jakarta.json.JsonArray; | ||
import jakarta.json.JsonValue; | ||
import jakarta.json.spi.JsonProvider; | ||
import java.math.BigInteger; | ||
import java.util.Objects; | ||
|
||
/** | ||
* <b>minProperties</b> : <i>Integer</i> | ||
* An object instance is valid if its number of properties is less than, or equal to, the value of this keyword.<br/> | ||
* keyword.<br/> | ||
* <br/> | ||
* <ul> | ||
* <li>assertion</li> | ||
* </ul> | ||
* | ||
* source: https://www.learnjsonschema.com/2020-12/validation/maxcontains/ | ||
* spec: https://json-schema.org/draft/2020-12/json-schema-validation.html#section-6.4.4 | ||
*/ | ||
final class MaxContainsKeywordType implements KeywordType { | ||
|
||
private final JsonProvider jsonContext; | ||
|
||
public MaxContainsKeywordType(final JsonProvider jsonContext) { | ||
this.jsonContext = jsonContext; | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return "maxContains"; | ||
} | ||
|
||
@Override | ||
public Keyword createKeyword(final JsonSchema schema) { | ||
return new IntegerKeywordType( | ||
jsonContext, | ||
name(), | ||
value -> new MaxContainsKeyword(schema, value) | ||
).createKeyword(schema); | ||
} | ||
|
||
private final class MaxContainsKeyword implements Assertion { | ||
|
||
private final JsonSchema owner; | ||
private final BigInteger maxContains; | ||
|
||
public MaxContainsKeyword(final JsonSchema owner, final BigInteger maxContains) { | ||
this.owner = Objects.requireNonNull(owner); | ||
this.maxContains = Objects.requireNonNull(maxContains); | ||
} | ||
|
||
@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(), maxContains); | ||
} | ||
|
||
@Override | ||
public boolean isValidFor(final JsonValue instance) { | ||
return ( | ||
!InstanceType.ARRAY.isInstance(instance) || | ||
owner | ||
.keywordByName("contains") | ||
.map(Keyword::asAnnotation) | ||
.map(annotation -> annotation.valueFor(instance)) | ||
.map(contains -> isValidFor(contains, instance.asJsonArray())) | ||
.orElse(true) | ||
); | ||
} | ||
|
||
private boolean isValidFor(final JsonValue containing, final JsonArray values) { | ||
final boolean result; | ||
if (JsonValue.TRUE.equals(containing)) { | ||
result = values.size() <= maxContains.intValue(); | ||
} else { | ||
result = containing.asJsonArray().size() <= maxContains.intValue(); | ||
} | ||
return result; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.