Skip to content

Commit

Permalink
make instancetype a jsonvalue
Browse files Browse the repository at this point in the history
this allow to use instancetype to define a schema like:
...add("type", InstanceType.STRING)
instead of
...add("type", "string")
or
...add("type", InstanceType.STRING.toString())
  • Loading branch information
sebastian-toepfer committed Jan 30, 2024
1 parent e5d84a7 commit 7294051
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package io.github.sebastiantoepfer.jsonschema;

import jakarta.json.JsonNumber;
import jakarta.json.JsonString;
import jakarta.json.JsonValue;
import java.math.BigDecimal;
import java.util.Arrays;
Expand All @@ -33,7 +34,7 @@
/**
* see: http://json-schema.org/draft/2020-12/json-schema-core.html#name-instance-data-model
*/
public enum InstanceType {
public enum InstanceType implements JsonString {
NULL(JsonValue.ValueType.NULL),
BOOLEAN(JsonValue.ValueType.TRUE, JsonValue.ValueType.FALSE),
OBJECT(JsonValue.ValueType.OBJECT),
Expand Down Expand Up @@ -74,6 +75,21 @@ public boolean isInstance(final JsonValue value) {

@Override
public String toString() {
return getString();
}

@Override
public CharSequence getChars() {
return getString();
}

@Override
public String getString() {
return name().toLowerCase(Locale.US);
}

@Override
public ValueType getValueType() {
return JsonValue.ValueType.STRING;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package io.github.sebastiantoepfer.jsonschema;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasToString;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.params.provider.Arguments.arguments;

Expand Down Expand Up @@ -94,6 +95,16 @@ static Stream<? extends Arguments> provideWithName() {
);
}

@Test
void should_be_from_json_type_string() {
assertThat(InstanceType.INTEGER.getValueType(), is(JsonValue.ValueType.STRING));
}

@Test
void should_retrun_his_value_as_charsquence() {
assertThat(InstanceType.INTEGER.getChars(), hasToString("integer"));
}

@Test
void should_be_createable_from_lowercase_value() {
assertThat(InstanceType.fromString("object"), is(InstanceType.OBJECT));
Expand Down

0 comments on commit 7294051

Please sign in to comment.