Validation protects your application from malformed or unexpected input. Validate at the API boundary before business logic and database operations.
Check data types, ranges, lengths, formats, and allowed values. A validation library can make larger projects more consistent.
Deep dive
Validation is not sanitization
Validation checks whether data meets your rules. Sanitization transforms or removes unwanted content. They solve different problems and should not be treated as interchangeable.
Validate again at important boundaries
Even if the frontend validates input, the backend must validate it again because clients cannot be trusted.