Skip to content

Commit

Permalink
Merge pull request #185 from sebastian-toepfer/format_assertion_ipv4
Browse files Browse the repository at this point in the history
add support for ipv4 format
  • Loading branch information
sebastian-toepfer authored Dec 4, 2024
2 parents 602a44a + cb4a0fe commit bdec341
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Pattern> RULES = Map.of(
"dotted-quad",
Pattern.compile("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$")
);

@Override
public Optional<Rule> findRuleByName(final String ruleName) {
return Optional.ofNullable(RULES.get(ruleName)).map(RegExRule::new);
}
}
20 changes: 20 additions & 0 deletions vocabulary/format-assertion/src/main/resources/rfc/rfc2673
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit bdec341

Please sign in to comment.