Add support for ipv4/6 predicates

This commit is contained in:
bom 2024-09-27 11:49:43 +02:00
parent 6a6e51d694
commit f24b7418e6
2 changed files with 22 additions and 0 deletions

View file

@ -14,6 +14,16 @@
(and (string? input) (and (string? input)
(some? (re-matches #"(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)" input)))) (some? (re-matches #"(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)" input))))
(defn ipv4-string?
[input]
(and (string? input)
(some? (re-matches #"^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$" input))))
(defn ipv6-string?
[input]
(and (string? input)
(some? (re-matches #"(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))" input))))
(defn string-of-separated-by? (defn string-of-separated-by?
[spec-function separator input] [spec-function separator input]
(every? true? (map spec-function (str/split input separator)))) (every? true? (map spec-function (str/split input separator))))

View file

@ -16,6 +16,18 @@
(is (false? (cut/fqdn-string? "123.456.789"))) (is (false? (cut/fqdn-string? "123.456.789")))
(is (false? (cut/fqdn-string? "test&test.de")))) (is (false? (cut/fqdn-string? "test&test.de"))))
(deftest test-ipv4-string?
(is (true? (cut/ipv4-string? "127.0.0.1")))
(is (true? (cut/ipv4-string? "192.168.192.168")))
(is (true? (cut/ipv4-string? "1.2.3.4")))
(is (false? (cut/ipv4-string? "1.a.2.b")))
(is (false? (cut/ipv4-string? "f::f::f::f"))))
(deftest test-ipv6-string?
(is (true? (cut/ipv6-string? "2a01:4f8:c012:cb41::1")))
(is (false? (cut/ipv6-string? "1.a.2.b")))
(is (false? (cut/ipv6-string? "f::f::f::f"))))
(deftest test-string-of-separated-by? (deftest test-string-of-separated-by?
(is (true? (cut/string-of-separated-by? cut/bash-env-string? #":" "abcd"))) (is (true? (cut/string-of-separated-by? cut/bash-env-string? #":" "abcd")))
(is (true? (cut/string-of-separated-by? cut/bash-env-string? #":" "abcd:efgh"))) (is (true? (cut/string-of-separated-by? cut/bash-env-string? #":" "abcd:efgh")))