Skip to content
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

format: ipv4 regular expression seems to be wrong #86

Open
sonufarid opened this issue Sep 5, 2019 · 0 comments
Open

format: ipv4 regular expression seems to be wrong #86

sonufarid opened this issue Sep 5, 2019 · 0 comments

Comments

@sonufarid
Copy link

sonufarid commented Sep 5, 2019

While testing the ipv4 format checker in my application, I noticed this problem.

Test case:

env = new djv()
schema = {type: "string",  format: "ipv4"}
env.addSchema('field', schema)
env.validate('field', "1.1.1111111.1") // = undefined
env.validate('field', "111111111111.1111111111111.11111111111111111111111111111111122222222222222.11") // = undefined

This is due to the regular expression used for the validation being slightly wrong:
lib/utils/formats.js line 24:

ipv4: expression`!/^(\\d?\\d?\\d){0,255}\\.(\\d?\\d?\\d){0,255}\\.(\\d?\\d?\\d){0,255}\\.(\\d?\\d?\\d){0,255}$/.test(${'data'}) || ${'data'}.split(".")[3] > 255`,

(\d?\d?\d){0,255} means 0 to 255 repetitions of (\d?\d?\d). I assume the actual intention was the integer value should be between 0 to 255.
We could do this instead:

ipv4: expression`!/^(\\d){1,3}\\.(\\d){1,3}\\.(\\d){1,3}\\.(\\d){1,3}$/.test(${'data'}) || ${'data'}.split(".").some(d => d > 255)`,
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

1 participant