Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyword max contains #123

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
<include>**/tests/draft2020-12/additionalProperties.json</include>
<include>**/tests/draft2020-12/boolean_schema.json</include>
<include>**/tests/draft2020-12/const.json</include>
<include>**/tests/draft2020-12/contains.json</include>
<include>**/tests/draft2020-12/dependentRequired.json</include>
<include>**/tests/draft2020-12/enum.json</include>
<include>**/tests/draft2020-12/exclusiveMaximum.json</include>
Expand All @@ -130,6 +131,9 @@
<!-- more than items keyword needed :(
<include>**/tests/draft2020-12/items.json</include>
-->
<!-- needs minContains too :(
<include>**/tests/draft2020-12/maxContains.json</include>
-->
<include>**/tests/draft2020-12/maxItems.json</include>
<include>**/tests/draft2020-12/maxLength.json</include>
<include>**/tests/draft2020-12/maximum.json</include>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public ApplicatorVocabulary() {
new SubSchemaKeywordType(AdditionalPropertiesKeyword.NAME, AdditionalPropertiesKeyword::new),
new NamedJsonSchemaKeywordType(PatternPropertiesKeyword.NAME, PatternPropertiesKeyword::new),
new SubSchemaKeywordType(ItemsKeyword.NAME, ItemsKeyword::new),
new ArraySubSchemaKeywordType(PrefixItemsKeyword.NAME, PrefixItemsKeyword::new)
new ArraySubSchemaKeywordType(PrefixItemsKeyword.NAME, PrefixItemsKeyword::new),
new SubSchemaKeywordType(ContainsKeyword.NAME, ContainsKeyword::new)
);
}

Expand Down
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;
}
}
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@
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/minproperties/
* spec: https://json-schema.org/draft/2020-12/json-schema-validation.html#section-6.5.2
*/
final class MinPropertiesKeyword implements Assertion {

static final String NAME = "minProperties";
Expand Down
Loading