Class StringExtensions
- Namespace
- Trellis
- Assembly
- Trellis.Core.dll
String manipulation helpers for value object field name normalization.
public static class StringExtensions
- Inheritance
-
StringExtensions
- Inherited Members
Methods
NormalizeFieldName(string?, string)
Normalizes an optional field name to camelCase, falling back to a default name.
public static string NormalizeFieldName(this string? fieldName, string defaultName)
Parameters
fieldNamestringThe optional field name to normalize.
defaultNamestringThe default field name if
fieldNameis null.
Returns
- string
The camelCased field name, or
defaultNameiffieldNameis null. An empty string is preserved as-is (not substituted): it is the established sentinel meaning "target the current pointer itself, with no property leaf", used by the ASP.NET Core validation pipeline's ambient current-property-name tracking.
ParseScalarValue<T>(string?)
Parses a string value using the specified IScalarValue<TSelf, TPrimitive> factory. Throws TrellisValidationFormatException if the value is invalid.
public static T ParseScalarValue<T>(string? s) where T : class, IScalarValue<T, string>
Parameters
sstring
Returns
- T
Type Parameters
T
Remarks
The thrown exception derives from FormatException, so
IParsable<TSelf>'s contract and every existing catch (FormatException)
are unaffected, and the message is the same flattened sentence as before. What is new is
that the structured failure travels with it instead of being discarded here — this helper
backs nine of the twelve hand-authored parse sites, so it is the single largest place
structure was being thrown away.
ToCamelCase(string?)
Converts the first character of a string to lowercase while preserving the rest of the string. Used primarily for converting C# PascalCase property names to camelCase for JSON serialization and validation error field names.
public static string ToCamelCase(this string? str)
Parameters
strstringThe string to convert to camelCase.
Returns
- string
A new string with the first character in lowercase and the remaining characters unchanged. If the string is null or empty, returns Empty.
Remarks
This method is used internally to ensure consistent field naming in validation errors and JSON responses. For example, converting "Email" to "email" or "FirstName" to "firstName".
Examples:
- "Email" → "email"
- "FirstName" → "firstName"
- "A" → "a"
- "" → ""
- <c>(string)null</c> → ""
TryParseScalarValue<T>(string?, out T)
Tries to parse a string value using the specified IScalarValue<TSelf, TPrimitive> factory.
public static bool TryParseScalarValue<T>(string? s, out T result) where T : class, IScalarValue<T, string>
Parameters
sstringresultT
Returns
Type Parameters
T