Class SqlIdentifierValidator
- Namespace
- Excalibur.Data.Validation
- Assembly
- Excalibur.Data.Abstractions.dll
Provides centralized SQL identifier validation to prevent SQL injection in dynamic SQL constructs such as savepoint names, CDC capture instances, table names, and schema names.
public static class SqlIdentifierValidator
- Inheritance
-
SqlIdentifierValidator
- Inherited Members
Remarks
All SQL identifiers that are interpolated into SQL strings (not parameterized) MUST be validated through this class before use.
The validation uses a strict allowlist pattern: only ASCII letters, digits, and underscores are permitted. This matches SQL Server's rules for regular identifiers without delimiters.
Uses [GeneratedRegex] for AOT compatibility (no RegexOptions.Compiled).
Methods
IsValid(string)
Returns whether the specified string is a valid SQL identifier (contains only ASCII letters, digits, and underscores).
public static bool IsValid(string identifier)
Parameters
identifierstringThe string to test.
Returns
ThrowIfInvalid(string, string)
Validates that a SQL identifier contains only safe characters (ASCII letters, digits, underscores). Throws ArgumentException if the identifier is invalid.
public static void ThrowIfInvalid(string identifier, string parameterName)
Parameters
identifierstringThe SQL identifier to validate.
parameterNamestringThe name of the parameter being validated, used in the exception message.
Examples
SqlIdentifierValidator.ThrowIfInvalid(tableName, nameof(tableName));
var sql = $"SELECT * FROM [{tableName}]";
Exceptions
- ArgumentException
Thrown when
identifiercontains characters other than ASCII letters, digits, or underscores.