Skip to content

Commit

Permalink
add support for prefixItems keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-toepfer committed Oct 5, 2023
1 parent d535ff0 commit b4ada20
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
<inculde>**/tests/draft2020-12/maximum.json</inculde>
<inculde>**/tests/draft2020-12/exclusiveMaximum.json</inculde>
<inculde>**/tests/draft2020-12/multipleOf.json</inculde>
<inculde>**/tests/draft2020-12/prefixItems.json</inculde>
<!-- more than items keyword needed :(
<inculde>**/tests/draft2020-12/items.json</inculde>
-->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* The MIT License
*
* Copyright 2023 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.jsonschema.InstanceType;
import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.JsonSchemas;
import io.github.sebastiantoepfer.jsonschema.keyword.Annotation;
import io.github.sebastiantoepfer.jsonschema.keyword.Applicator;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonValue;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
* https://json-schema.org/draft/2020-12/json-schema-core#section-10.3.1.1
* https://www.learnjsonschema.com/2020-12/applicator/prefixitems/
*/
final class PrefixItemsKeywordType implements KeywordType {

@Override
public String name() {
return "prefixItems";
}

@Override
public Keyword createKeyword(final JsonSchema schema, final JsonValue value) {
if (InstanceType.ARRAY.isInstance(value)) {
return new PrefixItemsKeyword(value.asJsonArray());
} else {
throw new IllegalArgumentException("must be a non empty array!");
}
}

private class PrefixItemsKeyword implements Annotation, Applicator {

private final List<JsonSchema> schemas;

public PrefixItemsKeyword(final JsonArray schemas) {
this.schemas = schemas.stream().map(JsonSchemas::load).toList();
}

@Override
public Collection<KeywordCategory> categories() {
return List.of(KeywordCategory.ANNOTATION, KeywordCategory.APPLICATOR);
}

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

@Override
public JsonValue value() {
return Json.createValue(schemas.size() - 1);
}

@Override
public boolean applyTo(final JsonValue instance) {
return !InstanceType.ARRAY.isInstance(instance) || matchesSchemas(instance.asJsonArray());
}

private boolean matchesSchemas(final JsonArray instance) {
boolean result = true;
for (int i = 0; i < Math.min(schemas.size(), instance.size()); i++) {
result &= schemas.get(i).validator().validate(instance.get(i)).isEmpty();
if (!result) {
break;
}
}
return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* The MIT License
*
* Copyright 2023 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.containsInAnyOrder;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;

import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
import jakarta.json.Json;
import jakarta.json.JsonValue;
import org.junit.jupiter.api.Test;

class PrefixItemsKeywordTypeTest {

@Test
void should_know_his_name() {
final Keyword items = new PrefixItemsKeywordType()
.createKeyword(
new DefaultJsonSchemaFactory().create(JsonValue.TRUE),
Json.createArrayBuilder().add(JsonValue.TRUE).build()
);

assertThat(items.hasName("prefixItems"), is(true));
assertThat(items.hasName("test"), is(false));
}

@Test
void should_not_be_createbale_from_non_array() {
final KeywordType keywordType = new PrefixItemsKeywordType();
final JsonSchema schema = new DefaultJsonSchemaFactory().create(JsonValue.TRUE);

assertThrows(
IllegalArgumentException.class,
() -> keywordType.createKeyword(schema, JsonValue.EMPTY_JSON_OBJECT)
);
}

@Test
void should_return_one_as_value() {
assertThat(
new PrefixItemsKeywordType()
.createKeyword(
new DefaultJsonSchemaFactory().create(JsonValue.TRUE),
Json.createArrayBuilder().add(JsonValue.TRUE).build()
)
.asAnnotation()
.value(),
is(Json.createValue(0))
);
}

@Test
void should_be_valid_for_non_arrays() {
assertThat(
new PrefixItemsKeywordType()
.createKeyword(
new DefaultJsonSchemaFactory().create(JsonValue.TRUE),
Json.createArrayBuilder().add(JsonValue.FALSE).build()
)
.asApplicator()
.applyTo(Json.createValue(1)),
is(true)
);
}

@Test
void should_be_valid_if_first_item_match_schema() {
assertThat(
new PrefixItemsKeywordType()
.createKeyword(
new DefaultJsonSchemaFactory().create(JsonValue.TRUE),
Json.createArrayBuilder().add(JsonValue.TRUE).build()
)
.asApplicator()
.applyTo(Json.createArrayBuilder().add(1).build()),
is(true)
);
}

@Test
void should_be_invalid_if_second_item_does_not_match_schema() {
assertThat(
new PrefixItemsKeywordType()
.createKeyword(
new DefaultJsonSchemaFactory().create(JsonValue.TRUE),
Json.createArrayBuilder().add(JsonValue.TRUE).add(JsonValue.FALSE).build()
)
.asApplicator()
.applyTo(Json.createArrayBuilder().add(1).add(3).build()),
is(false)
);
}

@Test
void should_be_applicator_and_annotation() {
assertThat(
new PrefixItemsKeywordType()
.createKeyword(new DefaultJsonSchemaFactory().create(JsonValue.TRUE), JsonValue.EMPTY_JSON_ARRAY)
.categories(),
containsInAnyOrder(Keyword.KeywordCategory.APPLICATOR, Keyword.KeywordCategory.ANNOTATION)
);
}
}

0 comments on commit b4ada20

Please sign in to comment.