question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ipv4 validation not implemented

See original GitHub issue

General information

The ipv4 format specifies that the entered string should be in the format of an ipv4 address. I believe this was already specified in json schema v4 but doesn’t seem to be implemented in json-editor.

  • json-editor version: 1.4.0-beta.0

Expected behavior

Should create a validation error if a non-valid ip address is entered into a string field specified with format ipv4.

Actual behavior

Does not raise a validation error.

Steps to reproduce the behavior

Direct link to example

{
  "title": "json schema example",
  "type": "object",
  "properties": {
    "ipAddress": {
      "type": "string",
      "format": "ipv4",
      "description": "IP address."
    }
  },
  "required": [
    "ipAddress"
  ]
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
sirockincommented, Jul 20, 2019

I wrote a custom validator below that will provide this validation. Would you like me to attempt to create a PR to include make it part of the default validation?

  JSONEditor.defaults.custom_validators.push(function(schema, value, path) {
    var errors = [];
    if(schema.format==="ipv4") 
    {
      try
      {
        let parts=value.split('.');
        if( parts.length !==4)
        {
          throw(Error());
        }
        parts.forEach((part)=>{
          if( isNaN(+part) || +part < 0 || +part>255)
          {
            throw(Error());
          }
        })
      }
      catch(err)
      {
        // Errors must be an object with `path`, `property`, and `message`
        errors.push({
          path: path,
          property: 'format',
          message: 'ip addresses must be in the form of 4 numbers between 0 and 255, separated by dots'
        });
      }
    }
    return errors;

1reaction
pmk65commented, Jul 20, 2019

@sirockin Good idea, as both ipv4 and ipv6 is part of the 04 specs. https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-7.3.4 So the validation should support both versions. Also it might also be a good idea to create an “ip editor”. It just has to extend the “string” editor and set a validator pattern, similar to what I did in the UUID editor postBuild() function. https://github.com/json-editor/json-editor/blob/master/src/editors/uuid.js

IPv4 Regex pattern: ^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$ IPv6 Regex pattern: ^(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}$

You can also find a lot of “ip” regex patterns here: https://regex101.com/library?orderBy=RELEVANCE&search=ip

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do you validate that a string is a valid IPv4 address in C++?
WARNING. According to the docs, it returns -1 for invalid AF argument, 0 for invalid address, and +1 for valid IP address. – ......
Read more >
[Feature Request] validation IP address (at least IPv4) #628
or maybe even implement some (weird) extra code like what I've seen here on a project I'm working on: let ipAddressSchema = Yup.string()...
Read more >
Program to validate an IP address - GeeksforGeeks
Following are steps to check whether a given string is a valid IPv4 address or not: step 1) Parse string with “.
Read more >
Validate IP Address - LeetCode
Given a string queryIP , return "IPv4" if IP is a valid IPv4 address, "IPv6" if IP is a valid IPv6 address or...
Read more >
How To Validate an IP Address with Python (2022) | AbstractAPI
The ipaddress module is not actually validating our IP address. It's attempting to use the provided string to create a Python IP address ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found