Skip to content

Commit

Permalink
improve schema keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-toepfer committed Oct 3, 2023
1 parent 0e664b0 commit 15cb529
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@
*/
package io.github.sebastiantoepfer.jsonschema.core.vocab.core;

import io.github.sebastiantoepfer.jsonschema.InstanceType;
import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.keyword.DefaultAnnotation;
import io.github.sebastiantoepfer.jsonschema.keyword.Identifier;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
import jakarta.json.JsonString;
import jakarta.json.JsonValue;
import java.net.URI;
import java.util.Objects;

/**
*
Expand All @@ -42,6 +46,29 @@ public String name() {

@Override
public Keyword createKeyword(final JsonSchema schema, final JsonValue value) {
return new DefaultAnnotation(name(), value);
if (InstanceType.STRING.isInstance(value)) {
return new SchemaKeyword((JsonString) value);
} else {
throw new IllegalArgumentException("must be a string!");
}
}

private class SchemaKeyword implements Identifier {

private final URI uri;

private SchemaKeyword(final JsonString uri) {
this.uri = URI.create(uri.getString());
}

@Override
public URI asUri() {
return uri;
}

@Override
public boolean hasName(final String name) {
return Objects.equals(name(), name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,47 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;

import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory;
import jakarta.json.Json;
import jakarta.json.JsonValue;
import java.net.URI;
import org.junit.jupiter.api.Test;

class SchemaKeywordTypeTest {

@Test
void should_not_be_creatable_from_non_string() {
final SchemaKeywordType schema = new SchemaKeywordType();

assertThrows(IllegalArgumentException.class, () -> schema.createKeyword(null, JsonValue.FALSE));
}

@Test
void should_create_keyword_with_name() {
assertThat(
new SchemaKeywordType()
.createKeyword(new DefaultJsonSchemaFactory().create(JsonValue.TRUE), JsonValue.EMPTY_JSON_OBJECT)
.createKeyword(
new DefaultJsonSchemaFactory().create(JsonValue.TRUE),
Json.createValue("https://json-schema.org/draft/2020-12/schema")
)
.hasName("$schema"),
is(true)
);
}

@Test
void should_retun_his_uri() {
assertThat(
new SchemaKeywordType()
.createKeyword(
new DefaultJsonSchemaFactory().create(JsonValue.TRUE),
Json.createValue("https://json-schema.org/draft/2020-12/schema")
)
.asIdentifier()
.asUri(),
is(URI.create("https://json-schema.org/draft/2020-12/schema"))
);
}
}

0 comments on commit 15cb529

Please sign in to comment.