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 Nov 19, 2023
1 parent f4d6700 commit 33516df
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
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
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 33516df

Please sign in to comment.