Skip to content

Commit

Permalink
improve $defs keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-toepfer committed Oct 4, 2023
1 parent 0090b7e commit 0feaa3b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
*/
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.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
import io.github.sebastiantoepfer.jsonschema.keyword.ReservedLocation;
import jakarta.json.JsonValue;
import java.util.Objects;

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

@Override
public Keyword createKeyword(final JsonSchema schema, final JsonValue value) {
return new DefaultAnnotation(name(), value);
if (InstanceType.OBJECT.isInstance(value)) {
return new DefsKeyword();
} else {
throw new IllegalArgumentException("must be an object!");
}
}

private class DefsKeyword implements ReservedLocation {

@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,28 @@

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 io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.JsonValue;
import org.junit.jupiter.api.Test;

class DefsKeywordTypeTest {

@Test
void should_create_keyword_with_name() {
assertThat(
new DefsKeywordType()
.createKeyword(new DefaultJsonSchemaFactory().create(JsonValue.TRUE), JsonValue.EMPTY_JSON_OBJECT)
.hasName("$defs"),
is(true)
);
void should_not_be_creatable_from_non_objects() {
final DefsKeywordType schema = new DefsKeywordType();

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

@Test
void should_know_his_name() {
final Keyword defs = new DefsKeywordType()
.createKeyword(new DefaultJsonSchemaFactory().create(JsonValue.TRUE), JsonValue.EMPTY_JSON_OBJECT);

assertThat(defs.hasName("$defs"), is(true));
assertThat(defs.hasName("test"), is(false));
}
}

0 comments on commit 0feaa3b

Please sign in to comment.