Field-level input validation returning error dictionary
C#
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
record SignupInput(string? Name, string? Email, int? Age);
static Dictionary<string, string> Validate(SignupInput input)
{
var errors = new Dictionary<string, string>();
if (string.IsNullOrWhiteSpace(input.Name))
errors["name"] = "Name is required.";
else if (input.Name.Length > 80)
errors["name"] = "Name must be at most 80 characters.";
if (input.Email is null || !Regex.IsMatch(input.Email, @"^[^@\s]+@[^@\s]+\.[^@\s]+$"))
errors["email"] = "Email address is not valid.";
if (input.Age is < 13 or > 120)
errors["age"] = "Age must be between 13 and 120.";
return errors;
}