netascode / iac-validate

A CLI tool to perform syntactic and semantic validation of YAML files.
https://github.com/netascode/iac-validate
Mozilla Public License 2.0
9 stars 3 forks source link

Idea to add custom validator for IP address vs prefix #280

Open tzarski0 opened 3 weeks ago

tzarski0 commented 3 weeks ago

We are using the IPv4/IPv6 addresses and prefixes a lot. The build-in YAMALE ip validator (for example, ip(version=4)) accepts both address (for example 10.0.0.1) and prefix (10.0.0.0/24). However in most cases we want to validate either address or prefix. The idea is to create custom validator for ip_address and ip_prefix so for example:

kuba-mazurkiewicz commented 1 week ago

Hey @tzarski0 ,

for validating IP address with or without prefix you can use regex instead of built in yamale ip validator

For Ipv4 without prefix you can use something like that:

^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$

for IPv4 with prefix:

^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}\/(3[0-2]|[1-2]?[0-9])$

And for ipv6 without prefix:

^([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4})$

Ipv6 with prefix:

^([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4})\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$

tzarski0 commented 1 week ago

True, it's just that then the error that iac-validate shows is "... is not a valid regex: ^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d).?\b){4}$", which is not very readable for end user. We could probably make it more clear if we implemented a simple ip address and prefix validator.