I deal with a lot of bugs and production issues. A lot of them can ultimately be traced back to poor data validation. It's hard to get right, and it's an area that developers often overlook. Here are some things you need to think about when validating different data types. You can use this as a checklist.
All Data Types
- Is it required or optional?
- Is it nullable and is that the same as a not-supplied or optional field?
Integers
- What are the min/max values it can have?
- Can it ever be negative or zero?
- Is the data type size (16, 32, 64 bits) appropriate?
- Can it overflow during calculations?
- Can it underflow during calculations?
- How do you handle remainders during division or modulus?
- How do you handle rounding when combining with floating point numbers?
Floating Point
- How do you handle accuracy errors inherit to IEEE 754?
- (all the same issues as integers)
Date/Time
- What are the min/max values it can have?
- Is the value an absolute point in time or relative?
- Do leap years or leap seconds matter?
- Does daylight savings time matter?
- What time zone is the input (source)?
- What time zone is the output (destination)?
Strings
- What are the min/max lengths it can have?
- How are you counting the lengths; glyphs, code points, code units, or bytes?
- What is the character encoding of the input (source)?
- What is the character encoding of the output (destination)?
- Can it handle a BOM?
- What is the EOL character (CRLF vs LF)?
- What are the allowed or forbidden characters?
- What normalization should you do?
- Trim leading/trailing spaces
- Remove multiple spaces in the middle
- Change to upper/lower case
- Is an empty string, or a string with only whitespace, distinct from null?
Lists
- Should all values be unique? (Consider using a Set)
- Is an empty list distinct from null?
- Should the list be sorted? When is the sort performed?
- Do you append to the list repeatedly? (Consider a LinkedList)
- Do you need to read from arbitrary locations? (Consider using a Dictionary/Map)
- What is the maximum number of elements in the list?
- Do you have the ability to detect and handle a situation where repeated appends may cause an out-of-memory error? For example, when reading from a very large file.
- Can any of the elements be null?
Objects
- Are there any cross-field/conditional validations?