IP Validator

Level: Beginner 15–30 min

Concepts: Validation

Solutions: C# | TypeScript | Python


Create a class with one method called ValidateIpv4Address. The method takes a string and return true if it is valid host assignable IP address and false if not.

IPv4 addresses are 32 bits long and grouped into 4 one byte blocks separated by dotted-decimal notation. E.g. 192.168.1.1.

Most IP addresses ending in 0 represent the entire network segment and cannot be used as host addresses. And most IP addresses ending in 255 represent a broadcast address and cannot be used as a host address. There are exceptions, when using subnets, for the sake of this Kata any address ending in 0 or 255 is not a valid host address.

NOTE

DO NOT USE REGULAR EXPRESSIONS TO SOLVE THIS KATA.

Examples

IP AddressResult
1.1.1.1true
192.168.1.1true
10.0.0.1true
127.0.0.1true
192.168.1.0true
0.0.0.0false
255.255.255.255false
10.0.1false
192.168.01.1false
192.168.1.00false

Solutions

You can find all language solutions here IP Validator Solutions

Or you can select a specific language below.

Reference Walkthrough

Reference implementations in C#, TypeScript, and Python live at tddbuddy-reference-katas/ip-validator. Eighteen shared scenarios cover the full rule cascade — valid host addresses (1.1.1.1, 192.168.1.1, 10.0.0.1, 127.0.0.1), the two non-host endings (0 and 255), wrong octet counts, leading zeros, out-of-range octets, non-digit characters, empty octets, and the empty string — satisfied identically across all three languages.

This kata ships in Agent Full-Bake mode at high gear (F1 tier): the algorithm is small enough to land as one commit per language, with a brief walkthrough noting there are no builders because the algorithm’s inputs and outputs are the domain. IPv6 is intentionally out of scope — the TDD Buddy spec is IPv4-only. No regular expressions, per the prompt: each language hand-rolls the digit scan, which makes the leading-zero and negative-sign rejections fall out of the character loop rather than a regex dialect quirk.

Spec conflict note. The Examples table above lists 192.168.1.0 → true, which contradicts both the prose rule (“any address ending in 0 or 255 is not a valid host address”) and the 0.0.0.0 → false / 255.255.255.255 → false rows. The reference implementations follow the prose rule — any address whose final octet is 0 or 255 is rejected. See the repo’s Gears section for when high gear is the right call.