parseNonEmptyString
Parses a value into a trimmed, non‑empty string. This parser combines the validateNonEmptyString validator with the normalizeNonEmptyString normalizer to ensure that the input is a primitive string containing meaningful content.
The parser never throws and never mutates input. It returns a structured ParseResult<string> describing success or failure.
Signature
function parseNonEmptyString(
value: unknown,
field?: string
): ParseResult<string>
Behavior
- Accepts primitive strings only.
- Trims leading and trailing whitespace.
- Rejects:
- Empty strings.
- Whitespace-only strings.
- Boxed String objects.
- Objects and arrays.
- Functions.
- Primitives other than strings.
nullandundefined.- Never throws and never mutates input.
Returns
A ParseResult<string> object:
- ok: true and the trimmed string when parsing succeeds.
- ok: false, value: null, and a list of issues when parsing fails.
Examples
parseNonEmptyString(" hello ")
// { ok: true, value: "hello", issues: [] }
parseNonEmptyString(" ")
// { ok: false, value: null, issues: ["String cannot be empty or whitespace"] }
Notes
- Use parseString if empty strings should be allowed.
- This parser is ideal for user-facing fields like names, titles, and labels.