parseString
Parses a value into a trimmed string. This parser combines the validateString primitive validator with the normalizeString normalizer to ensure that the input is a primitive string and to produce a trimmed, canonical representation.
The parser never throws and never mutates input. It returns a structured ParseResult<string> describing success or failure.
Signature
function parseString(
value: unknown,
field?: string
): ParseResult<string>
Behavior
- Accepts primitive strings only.
- Trims leading and trailing whitespace.
- Empty strings (after trimming) are valid.
- Rejects:
- Boxed String objects.
- Arrays and Objects.
- Functions.
- Primitives other than strings.
nullandundefined.- Never mutates input.
- Never throws.
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
parseString(" hello ")
// { ok: true, value: "hello", issues: [] }
parseString(123)
// { ok: false, value: null, issues: ["Value must be a string"] }
Notes
- For rejecting empty strings, use parseNonEmptyString.
- For more complex string semantics (email, UUID, hex, and so on), use the corresponding semantic parsers.