From cb4a0fe6206c5b270ed07a52232d90939be3c6ac Mon Sep 17 00:00:00 2001 From: Sebastian Toepfer <61313468+sebastian-toepfer@users.noreply.github.com> Date: Tue, 3 Dec 2024 17:03:53 +0100 Subject: [PATCH] add support for ipv4 format --- .../vocabulary/formatassertion/Formats.java | 6 ++- .../formatassertion/rfc/Rfc2673.java | 41 +++++++++++++++++++ .../src/main/resources/rfc/rfc2673 | 20 +++++++++ .../formatassertion/FormatsTest.java | 6 +++ 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 vocabulary/format-assertion/src/main/java/io/github/sebastiantoepfer/jsonschema/vocabulary/formatassertion/rfc/Rfc2673.java create mode 100644 vocabulary/format-assertion/src/main/resources/rfc/rfc2673 diff --git a/vocabulary/format-assertion/src/main/java/io/github/sebastiantoepfer/jsonschema/vocabulary/formatassertion/Formats.java b/vocabulary/format-assertion/src/main/java/io/github/sebastiantoepfer/jsonschema/vocabulary/formatassertion/Formats.java index 588b44b..7557fb2 100644 --- a/vocabulary/format-assertion/src/main/java/io/github/sebastiantoepfer/jsonschema/vocabulary/formatassertion/Formats.java +++ b/vocabulary/format-assertion/src/main/java/io/github/sebastiantoepfer/jsonschema/vocabulary/formatassertion/Formats.java @@ -24,6 +24,7 @@ package io.github.sebastiantoepfer.jsonschema.vocabulary.formatassertion; import io.github.sebastiantoepfer.jsonschema.vocabulary.formatassertion.rfc.Rfc; +import io.github.sebastiantoepfer.jsonschema.vocabulary.formatassertion.rfc.Rfc2673; import io.github.sebastiantoepfer.jsonschema.vocabulary.formatassertion.rfc.Rfc3339; import io.github.sebastiantoepfer.jsonschema.vocabulary.formatassertion.rfc.Rfc5321; import java.util.List; @@ -43,7 +44,10 @@ final class Formats { .entrySet() .stream() .map(entry -> new RfcBasedFormat(entry.getKey(), new Rfc3339(), entry.getValue())), - Stream.of(new RfcBasedFormat("email", new Rfc5321(), "mailbox")) + Stream.of( + new RfcBasedFormat("email", new Rfc5321(), "mailbox"), + new RfcBasedFormat("ipv4", new Rfc2673(), "dotted-quad") + ) ) .map(Format.class::cast) .toList(); diff --git a/vocabulary/format-assertion/src/main/java/io/github/sebastiantoepfer/jsonschema/vocabulary/formatassertion/rfc/Rfc2673.java b/vocabulary/format-assertion/src/main/java/io/github/sebastiantoepfer/jsonschema/vocabulary/formatassertion/rfc/Rfc2673.java new file mode 100644 index 0000000..6f01216 --- /dev/null +++ b/vocabulary/format-assertion/src/main/java/io/github/sebastiantoepfer/jsonschema/vocabulary/formatassertion/rfc/Rfc2673.java @@ -0,0 +1,41 @@ +/* + * The MIT License + * + * Copyright 2024 sebastian. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package io.github.sebastiantoepfer.jsonschema.vocabulary.formatassertion.rfc; + +import java.util.Map; +import java.util.Optional; +import java.util.regex.Pattern; + +public final class Rfc2673 implements Rfc { + + private static final Map RULES = Map.of( + "dotted-quad", + Pattern.compile("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$") + ); + + @Override + public Optional findRuleByName(final String ruleName) { + return Optional.ofNullable(RULES.get(ruleName)).map(RegExRule::new); + } +} diff --git a/vocabulary/format-assertion/src/main/resources/rfc/rfc2673 b/vocabulary/format-assertion/src/main/resources/rfc/rfc2673 new file mode 100644 index 0000000..6bdf1bc --- /dev/null +++ b/vocabulary/format-assertion/src/main/resources/rfc/rfc2673 @@ -0,0 +1,20 @@ +bit-string-label = "\[" bit-spec "]" + +bit-spec = bit-data [ "/" length ] + / dotted-quad [ "/" slength ] + +bit-data = "x" 1*64HEXDIG + / "o" 1*86OCTDIG + / "b" 1*256BIT + +dotted-quad = decbyte "." decbyte "." decbyte "." decbyte + +decbyte = 1*3DIGIT + +length = NZDIGIT *2DIGIT + +slength = NZDIGIT [ DIGIT ] + +OCTDIG = %x30-37 + +NZDIGIT = %x31-39 \ No newline at end of file diff --git a/vocabulary/format-assertion/src/test/java/io/github/sebastiantoepfer/jsonschema/vocabulary/formatassertion/FormatsTest.java b/vocabulary/format-assertion/src/test/java/io/github/sebastiantoepfer/jsonschema/vocabulary/formatassertion/FormatsTest.java index e23a873..77e62a2 100644 --- a/vocabulary/format-assertion/src/test/java/io/github/sebastiantoepfer/jsonschema/vocabulary/formatassertion/FormatsTest.java +++ b/vocabulary/format-assertion/src/test/java/io/github/sebastiantoepfer/jsonschema/vocabulary/formatassertion/FormatsTest.java @@ -68,4 +68,10 @@ void should_found_emailFormat() { ); assertThat(new Formats().findByName("email").applyTo("noEmail"), is(false)); } + + @Test + void should_found_ipv4Format() { + assertThat(new Formats().findByName("ipv4").applyTo("161.111.26.9"), is(true)); + assertThat(new Formats().findByName("ipv4").applyTo("125.158.4589.1"), is(false)); + } }