Skip to content

Commit

Permalink
first poc to show the idea behind the rules
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-toepfer committed Jun 15, 2024
1 parent d55566f commit 45d988b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion;

import io.github.sebastiantoepfer.ddd.common.Media;
import io.github.sebastiantoepfer.jsonschema.InstanceType;
import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.keyword.Annotation;
Expand Down Expand Up @@ -50,9 +51,13 @@ public String name() {
}

@Override
public Keyword createKeyword(final JsonSchema schema, final JsonValue value) {
if (InstanceType.STRING.isInstance(value)) {
return createKeyword(((JsonString) value).getString());
public Keyword createKeyword(final JsonSchema js) {
if (
js.getValueType() == JsonValue.ValueType.OBJECT &&
js.asJsonObject().containsKey(name()) &&
js.asJsonObject().get(name()).getValueType() == JsonValue.ValueType.STRING
) {
return createKeyword(js.asJsonObject().getString(name()));
} else {
throw new IllegalArgumentException("Value must be a string!");
}
Expand Down Expand Up @@ -94,5 +99,10 @@ public JsonValue valueFor(final JsonValue value) {
public boolean isValidFor(final JsonValue instance) {
return !InstanceType.STRING.isInstance(instance) || format.isValidFor(((JsonString) instance).getString());
}

@Override
public <T extends Media<T>> T printOn(final T media) {
return media.withValue(name(), format.name());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
import io.github.sebastiantoepfer.ddd.common.Printable;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.Element;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.RuleName;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.ValidateableCodePoint;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.IntStream;

public final class Rule implements Printable {

Expand All @@ -47,6 +50,15 @@ public boolean hasRuleName(final RuleName name) {
return Objects.equals(this.name, name);
}

Predicate<String> asPredicate() {
return s ->
IntStream
.range(0, s.length())
.boxed()
.map(i -> ValidateableCodePoint.of(i, s.codePointAt(i)))
.allMatch(elements::isValidFor);
}

@Override
public <T extends Media<T>> T printOn(final T media) {
return media.withValue("name", name).withValue("type", "rule").withValue("elements", elements);
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class FormatAssertionKeywordTypeTest {
@Test
void should_know_his_name() {
final Keyword keyword = new FormatAssertionKeywordType(List.of(new TestFormat()))
.createKeyword(JsonSchemas.load(JsonValue.TRUE), Json.createValue("date"));
.createKeyword(JsonSchemas.load(Json.createObjectBuilder().add("format", "date").build()));

assertThat(keyword.hasName("format"), is(true));
assertThat(keyword.hasName("test"), is(false));
Expand All @@ -52,25 +52,21 @@ void should_know_his_name() {
void should_not_be_createable_with_non_string() {
final JsonSchema schema = JsonSchemas.load(JsonValue.TRUE);
final KeywordType keywordType = new FormatAssertionKeywordType(List.of(new TestFormat()));
assertThrows(
IllegalArgumentException.class,
() -> keywordType.createKeyword(schema, JsonValue.EMPTY_JSON_OBJECT)
);
assertThrows(IllegalArgumentException.class, () -> keywordType.createKeyword(schema));
}

@Test
void should_not_be_createable_with_unknown_formatname() {
final JsonSchema schema = JsonSchemas.load(JsonValue.TRUE);
final JsonSchema schema = JsonSchemas.load(Json.createObjectBuilder().add("format", "test").build());
final KeywordType keywordType = new FormatAssertionKeywordType(List.of(new TestFormat()));
final JsonValue unknownFormatName = Json.createValue("test");
assertThrows(IllegalArgumentException.class, () -> keywordType.createKeyword(schema, unknownFormatName));
assertThrows(IllegalArgumentException.class, () -> keywordType.createKeyword(schema));
}

@Test
void should_be_assertion_and_annotation() {
assertThat(
new FormatAssertionKeywordType(List.of(new TestFormat()))
.createKeyword(JsonSchemas.load(JsonValue.TRUE), Json.createValue("date"))
.createKeyword(JsonSchemas.load(Json.createObjectBuilder().add("format", "date").build()))
.categories(),
containsInAnyOrder(Keyword.KeywordCategory.ASSERTION, Keyword.KeywordCategory.ANNOTATION)
);
Expand All @@ -80,7 +76,7 @@ void should_be_assertion_and_annotation() {
void should_return_formatname_as_annotation() {
assertThat(
new FormatAssertionKeywordType(List.of(new TestFormat()))
.createKeyword(JsonSchemas.load(JsonValue.TRUE), Json.createValue("date"))
.createKeyword(JsonSchemas.load(Json.createObjectBuilder().add("format", "date").build()))
.asAnnotation()
.valueFor(JsonValue.EMPTY_JSON_OBJECT),
is(Json.createValue("date"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@

import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.Alternative;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.Concatenation;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.RuleName;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.RuleReference;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.SequenceGroup;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.StringElement;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.VariableRepetition;
import nl.jqno.equalsverifier.EqualsVerifier;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

class RuleTest {
Expand Down Expand Up @@ -78,4 +83,49 @@ void should_be_printable() {
)
);
}

@Nested
class Validation {

private Rule ruleName;

@BeforeEach
void initRule() {
ruleName =
Rule.of(
RuleName.of("rulename"),
Concatenation.of(
CoreRules.ALPHA,
VariableRepetition.of(
SequenceGroup.of(Alternative.of(CoreRules.ALPHA, CoreRules.DIGIT, StringElement.of("-")))
)
)
);
}

@Test
void should_be_valid_for_alphas_only() {
assertThat(ruleName.asPredicate().test("rulename"), is(true));
}

@Test
void should_be_valid_for_valid_alpha_and_digits() {
assertThat(ruleName.asPredicate().test("rul3nam3"), is(true));
}

@Test
void should_be_valid_for_valid_alpha_and_minus() {
assertThat(ruleName.asPredicate().test("rule-name"), is(true));
}

@Test
void should_be_invalid_for_value_with_digist_at_start() {
assertThat(ruleName.asPredicate().test("1rule"), is(false));
}

@Test
void should_be_invalid_for_value_with_solidus() {
assertThat(ruleName.asPredicate().test("rule/name"), is(false));
}
}
}

0 comments on commit 45d988b

Please sign in to comment.