Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
tzarski0 opened this issue Oct 15, 2024 · 2 comments
Open

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

tzarski0 opened this issue Oct 15, 2024 · 2 comments

Comments

@tzarski0
Copy link

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:

  • in prefix-list we can validate the prefix (e.g. 10.0.0.0/24 has both address and / subnet mask)
  • in logging/tacacs server ip we can validate the address (e.g. 10.0.0.0 has IP, but doesn't have subnet mask).
@kuba-mazurkiewicz
Copy link
Contributor

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants