public void Sanitize(Value value) { value.FieldName = Name ?? string.Empty; string sanitizedValue = TrimField ? value.OriginalValue.Trim() : value.OriginalValue; //If it wasn't in the original file, handle it in advance if (value.Missing || sanitizedValue == string.Empty) { if (!AllowedBlank) { value.ErrorMsg = ValueIfBlank; } return; } //Removes any disallowed characters and retains only allowed characters var chars = sanitizedValue.ToCharArray(); if (AllowedCharacters.Any()) { chars = chars.Where(c => AllowedCharacters.Any(a => a == c)).ToArray(); } if (DisallowedCharacters.Any()) { chars = chars.Where(c => !DisallowedCharacters.Any(d => d == c)).ToArray(); } //validate the length if (chars.Length > MaxLength || chars.Length < MinLength) { value.SanitizedValue = new string(chars); value.ErrorMsg = ValueIfWrongLength; return; } //run regex sanitizedValue = new string(chars); if (RegEx != string.Empty && !Regex.IsMatch(sanitizedValue, RegEx)) { value.ErrorMsg = "Value failed regex check on value."; return; } //run any custom checks value.SanitizedValue = RemoveDoubleSpaces(sanitizedValue); //remove any double spaces CustomChecks.ForEach(c => c.Execute(value)); //If this there are a fixed number of options, check for them if ((AllowedValues?.Count() ?? 0) != 0 && !AllowedValues.Any(v => v == value.SanitizedValue)) { value.ErrorMsg = ValueIfNotInAllowedOptions; } }