IP Address

Validating IP Address is rather simple but time consuming task, valid8r IP validators provides a handful of options to best suit your validation requirements

Valid8r covers all the basic as well as advanced IP validations that covers 99% edge cases

Start by importing valid8r into your file:

import valid8r from '@c4code/valid8r';

Configuration Options

Valid8r IP validator provides handful of options to best meet your requirements and validation needs, the options that can be configured in ip validator are:

import valid8r from '@c4code/valid8r';

const ip: string = "2001:db8:130F:0000:00x0:09C0:876A:130B";

// The configuration can be defaulted globally
valid8r.ip(ip, {
  safe: false, // boolean
  version: "*", // '*' | 'v4' | 'v6'
  allowPrivate: false, // boolean
  allowLoopback: false, // boolean
  throwErrorsAs: "throw-all", // "throw-first" | "throw-last" | "throw-all",
  /*
  "throw-first" => Throws or returns the first error encountered,
  "throw-last" => Throws or returns the last error encountered,
  "throw-all" => Throws or returns all errors in a Array of string
  */
}); // the second argument is optional

The version field, if set will be used to verify the ip address to that version, ex.

version: 'v4' // only IPV4 will be considered valid
// or
version: 'v6' // only IPV6 will be considered valid
// or
version: '*' // either of the two ip versions will be considered valid

The version is by default set to '*'.

The Configuration can be globally set for all the validators in your project, know more?

Basic Usage

Here's how we can use the IP validator in it's most basic manner:

Start by importing valid8r:

import valid8r from '@c4code/valid8r';
import valid8r from '@c4code/valid8r';

const ip: string = "2001:db8:130F:0000:00x0:09C0:876A:130B";

// The configuration can be defaulted globally
valid8r.ip(ip, {
  version: 'v4'
}); // throws IPValidationFailed error as given ip is not IPV4

Safe handling

By default, all the validations in valid8r works on unsafe flag, meaning if any of the validation fails it will throw an Error.

However, if you don't want it to throw any error but return them. set the safe flag to true.

import valid8r from '@c4code/valid8r';

const ip: string = "2001:db8:130F:0000:00x0:09C0:876A:130B";

// The configuration can be defaulted globally
const [isValid, errors] = valid8r.ip(ip, {
  safe: true,
  version: 'v4'
});

if(!isValid) {
  console.log(errors);
    // [{ version: "IP Address is not valid, please enter a valid IP!" }]
}

Error Messages

If you don't like the default error message and want to set a custom error message for any specific fields, pass an object as a third argument of the ip function:

import valid8r from '@c4code/valid8r';

const ip: string = "2001:db8:130F:0000:0000:09C0:876A:130B";

// The configuration can be defaulted globally
const [isValid, errors] = valid8r.ip(ip, {
  safe: true,
  version: 'v4'
}, {
  version: "Invalid IP Address format!"
});

if(!isValid) {
  console.log(errors);
    // [{ version: "Invalid IP Address format!" }]
}

Error Message Options

Valid8r IP validator provides handful of options to configure error messages to best fit your project requirements

import valid8r from '@bitbybit/valid8r';

const ip: string = "2001:db8:130F:0000:0000:09C0:876A:130B";

// The configuration can be defaulted globally
valid8r.ip(ip, { /* Field Configurations */ }, {
  format: 
    "Invalid IP Address.",
  version: 
    "Invalid IP address version. Allowed versions are IPv4 or IPv6.",
  allowPrivate: 
    "Private IP addresses are not allowed.",
  allowLoopback: 
    "Loopback IP addresses are not allowed.",
}); // the second and third argument is optional

All the options are optional, if passed will be used as the configuration for that specific validator

The Error messages can be globally set for all the validators in your project, know more?

Last updated