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 enum #34

Merged
merged 1 commit into from
Oct 10, 2023
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
2 changes: 0 additions & 2 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@
-->
<inculde>**/tests/draft2020-12/additionalProperties.json</inculde>
<inculde>**/tests/draft2020-12/boolean_schema.json</inculde>
<!-- needs required
<inculde>**/tests/draft2020-12/enum.json</inculde>
-->
<inculde>**/tests/draft2020-12/exclusiveMaximum.json</inculde>
<inculde>**/tests/draft2020-12/exclusiveMinimum.json</inculde>
<!-- more than items keyword needed :(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.validation;

import static java.util.function.Predicate.isEqual;

import io.github.sebastiantoepfer.jsonschema.InstanceType;
import io.github.sebastiantoepfer.jsonschema.JsonSchema;
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.JsonNumber;
import jakarta.json.JsonValue;
import java.math.BigDecimal;
import java.util.Objects;

final class EnumKeywordType implements KeywordType {

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

@Override
public Keyword createKeyword(final JsonSchema schema, final JsonValue value) {
return new EnumKeyword(value.asJsonArray());
}

private class EnumKeyword implements Assertion {

private final JsonArray allowedValues;

public EnumKeyword(final JsonArray allowedValues) {
this.allowedValues = allowedValues;
}

@Override
public boolean isValidFor(final JsonValue instance) {
final boolean result;
if (InstanceType.NUMBER.isInstance(instance)) {
result =
allowedValues
.stream()
.filter(InstanceType.NUMBER::isInstance)
.map(JsonNumber.class::cast)
.map(JsonNumber::bigDecimalValue)
.map(BigDecimal::stripTrailingZeros)
.anyMatch(isEqual(((JsonNumber) instance).bigDecimalValue().stripTrailingZeros()));
} else {
result = allowedValues.stream().anyMatch(isEqual(instance));
}
return result;
}

@Override
public boolean hasName(final String name) {
return Objects.equals(name(), name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public ValidationVocabulary() {
URI.create("https://json-schema.org/draft/2020-12/vocab/validation"),
new RequiredKeywordType(),
new TypeKeywordType(),
new EnumKeywordType(),
new MinLengthKeywordType(),
new MaxLengthKeywordType(),
new PatternKeywordType(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* 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.validation;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

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

class EnumKeywordTypeTest {

@Test
void should_know_his_name() {
final Keyword enumKeyword = new EnumKeywordType()
.createKeyword(new DefaultJsonSchemaFactory().create(JsonValue.TRUE), JsonValue.EMPTY_JSON_ARRAY);

assertThat(enumKeyword.hasName("enum"), is(true));
assertThat(enumKeyword.hasName("test"), is(false));
}

@Test
void should_valid_for_string_value_which_is_in_array() {
assertThat(
new EnumKeywordType()
.createKeyword(
new DefaultJsonSchemaFactory().create(JsonValue.TRUE),
Json.createArrayBuilder().add("TEST").add("VALID").build()
)
.asAssertion()
.isValidFor(Json.createValue("TEST")),
is(true)
);
}

@Test
void should_be_invalid_for_number_which_is_not_in_array() {
assertThat(
new EnumKeywordType()
.createKeyword(
new DefaultJsonSchemaFactory().create(JsonValue.TRUE),
Json.createArrayBuilder().add("TEST").add("VALID").build()
)
.asAssertion()
.isValidFor(Json.createValue(2)),
is(false)
);
}

@Test
void should_be_valid_for_decimal_without_scale_if_number_is_valid() {
assertThat(
new EnumKeywordType()
.createKeyword(
new DefaultJsonSchemaFactory().create(JsonValue.TRUE),
Json.createArrayBuilder().add(1).build()
)
.asAssertion()
.isValidFor(Json.createValue(1.0)),
is(true)
);
}
}