Custom Fields Validation regex field is too small #4513

Closed
opened 2025-12-29 18:36:52 +01:00 by adam · 3 comments
Owner

Originally created by @ggiesen on GitHub (Jan 29, 2021).

Environment

  • Python version: 3.6.8
  • NetBox version: 2.10.2

Proposed Functionality

Increase the length of the Validation regex field in Custom Fields beyond the current 500 character limit (perhaps to something like 4096)

Use Case

In attempting to apply a validator to a custom field that defines static routes assigned to an IP on an interface, I use the following format:

<prefix>/<prefixlen> via gateway[,<prefix>/<prefixlen> via gateway]

Example:

192.0.2.0/24 via 203.0.113.1,198.51.100.0/24 via 203.0.113.1

To validate this accurately, use the following regular expression:

^\b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\b\/\b([0-9]|[1-2][0-9]|3[1-2])\b via \b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\b(,\b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\b\/\b([0-9]|[1-2][0-9]|3[1-2])\b via \b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\b)*$

Which is 838 characters long.

While the above expression could perhaps be shortened without sacrificing accuracy or increasing complexity, it serves to demonstrate how a regular expression to validate something even relatively simple can grow very long, and the current field length is inadequate

Database Changes

I expect minor changes to column sizes would be require

External Dependencies

None

Originally created by @ggiesen on GitHub (Jan 29, 2021). <!-- NOTE: IF YOUR ISSUE DOES NOT FOLLOW THIS TEMPLATE, IT WILL BE CLOSED. This form is only for proposing specific new features or enhancements. If you have a general idea or question, please start a discussion instead: https://github.com/netbox-community/netbox/discussions NOTE: Due to an excessive backlog of feature requests, we are not currently accepting any proposals which significantly extend NetBox's feature scope. Please describe the environment in which you are running NetBox. Be sure that you are running an unmodified instance of the latest stable release before submitting a bug report. --> ### Environment * Python version: 3.6.8 * NetBox version: 2.10.2 <!-- Describe in detail the new functionality you are proposing. Include any specific changes to work flows, data models, or the user interface. --> ### Proposed Functionality Increase the length of the Validation regex field in Custom Fields beyond the current 500 character limit (perhaps to something like 4096) <!-- Convey an example use case for your proposed feature. Write from the perspective of a NetBox user who would benefit from the proposed functionality and describe how. ---> ### Use Case In attempting to apply a validator to a custom field that defines static routes assigned to an IP on an interface, I use the following format: \<prefix\>/\<prefixlen\> via gateway[,\<prefix\>/\<prefixlen\> via gateway] Example: `192.0.2.0/24 via 203.0.113.1,198.51.100.0/24 via 203.0.113.1` To validate this accurately, use the following regular expression: `^\b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\b\/\b([0-9]|[1-2][0-9]|3[1-2])\b via \b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\b(,\b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\b\/\b([0-9]|[1-2][0-9]|3[1-2])\b via \b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\b)*$` Which is 838 characters long. While the above expression could perhaps be shortened without sacrificing accuracy or increasing complexity, it serves to demonstrate how a regular expression to validate something even relatively simple can grow very long, and the current field length is inadequate <!-- Note any changes to the database schema necessary to support the new feature. For example, does the proposal require adding a new model or field? (Not all new features require database changes.) ---> ### Database Changes I expect minor changes to column sizes would be require <!-- List any new dependencies on external libraries or services that this new feature would introduce. For example, does the proposal require the installation of a new Python package? (Not all new features introduce new dependencies.) --> ### External Dependencies None
adam closed this issue 2025-12-29 18:36:53 +01:00
Author
Owner

@jeremystretch commented on GitHub (Jan 29, 2021):

This is pretty clearly excessive use of regular expressions. You're trying to leverage a regex to enforce the validity of several data types here, which is just generally a bad idea. You might consider splitting the relevant data into multiple fields or, better yet, writing a plugin to model it independently.

@jeremystretch commented on GitHub (Jan 29, 2021): This is pretty clearly excessive use of regular expressions. You're trying to leverage a regex to enforce the validity of _several_ data types here, which is just generally a bad idea. You might consider splitting the relevant data into multiple fields or, better yet, writing a plugin to model it independently.
Author
Owner

@DanSheps commented on GitHub (Feb 1, 2021):

While the above expression could perhaps be shortened without sacrificing accuracy or increasing complexity

I think this sentence sums this up pretty well. You could validate this using a much shorter regex (repeating patterns and recursion) to accomplish the same validation and I don't see any normal circumstance where you need to exceed 500 characters for validation.

@DanSheps commented on GitHub (Feb 1, 2021): > While the above expression could perhaps be shortened without sacrificing accuracy or increasing complexity I think this sentence sums this up pretty well. You could validate this using a much shorter regex (repeating patterns and recursion) to accomplish the same validation and I don't see any normal circumstance where you need to exceed 500 characters for validation.
Author
Owner

@ggiesen commented on GitHub (Feb 1, 2021):

This is pretty clearly excessive use of regular expressions. You're trying to leverage a regex to enforce the validity of several data types here, which is just generally a bad idea. You might consider splitting the relevant data into multiple fields or, better yet, writing a plugin to model it independently.

I'm not sure how you'd split this into multiple fields and still make it usable. I grant writing a plugin may work but that's a much bigger step from writing a regex.

While the above expression could perhaps be shortened without sacrificing accuracy or increasing complexity

I think this sentence sums this up pretty well. You could validate this using a much shorter regex (repeating patterns and recursion) to accomplish the same validation and I don't see any normal circumstance where you need to exceed 500 characters for validation.

I've got the regex down to 482 chars:

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

It sacrifices a bit of readability, not that the original was terribly readable in the first place. Given that regex processing of things like IP addresses are very complicated, and the relatively simple example above still occupies 482 chars, I thought this was a no-brainer, but I guess I'm wrong. In the meantime I've managed to squeeze the regex down below the limit, and I'll wait for #5451 as the much better solution

@ggiesen commented on GitHub (Feb 1, 2021): > This is pretty clearly excessive use of regular expressions. You're trying to leverage a regex to enforce the validity of several data types here, which is just generally a bad idea. You might consider splitting the relevant data into multiple fields or, better yet, writing a plugin to model it independently. I'm not sure how you'd split this into multiple fields and still make it usable. I grant writing a plugin may work but that's a much bigger step from writing a regex. > > > > While the above expression could perhaps be shortened without sacrificing accuracy or increasing complexity > > I think this sentence sums this up pretty well. You could validate this using a much shorter regex (repeating patterns and recursion) to accomplish the same validation and I don't see any normal circumstance where you need to exceed 500 characters for validation. I've got the regex down to 482 chars: `^\b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}\b\/\b([0-9]|[1-2][0-9]|3[1-2])\b via \b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}\b(,\b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}\b\/\b([0-9]|[1-2][0-9]|3[1-2])\b via \b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}\b)*$` It sacrifices a bit of readability, not that the original was terribly readable in the first place. Given that regex processing of things like IP addresses are very complicated, and the relatively simple example above still occupies 482 chars, I thought this was a no-brainer, but I guess I'm wrong. In the meantime I've managed to squeeze the regex down below the limit, and I'll wait for #5451 as the much better solution
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#4513