validateString
Validates that a value is a primitive string. This validator relies on isString internally, performing a strict typeof check without coercion. Only primitive strings pass; String objects, numbers, booleans, symbols, objects, arrays, null, and undefined all fail.
This helper never throws and never mutates input. Use it when you need to confirm that a value is a string before further normalization or validation.
Signature
function validateString(value: unknown, field: string): ValidationResult<string>
Parameters
| Name | Type | Description |
|---|---|---|
| value | unknown |
The value to validate. |
| field | string |
The name of the field being validated, used in error reporting. |
Returns
ValidationResult<string> — One of:
{ ok: true, value: string }: If value is a primitive string.{ ok: false, field: string, message: string }: if value is not a string.
Behavior
- Uses isString internally.
- Strings with a length greater than
0pass, including whitespace or zero-width characters. - Empty strings and non-string values fail.
- Never throws or mutates input.
Examples
validateNonEmptyString("hello", "name")
// { ok: true, value: "hello" }
validateNonEmptyString(" ", "space")
// { ok: true, value: " " }
validateNonEmptyString("\u200B", "zeroWidth")
// { ok: true, value: "\u200B" }
validateNonEmptyString("", "empty")
// { ok: false, field: "empty", message: "String cannot be empty" }
validateNonEmptyString(123, "age")
// { ok: false, field: "age", message: "String cannot be empty" }
Notes
- Combine with normalizeString if trimming or other normalization is needed.
- Strictly primitive strings only;
Stringobjects fail.