Skip to content

format: ipv4 regular expression seems to be wrong #86

Description

@sonufarid

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)`,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions