Skip to content

Empty values ​

tinyval treats three values as empty: undefined, null and the empty string ''.

How it works ​

ValueRequired schema (string(), object()…)optional()nullable()
undefinedrequired errorskipped / default valuerequired error
''required errorskipped / default valuerequired error
nullrequired errorrequired errornull

Why ​

An empty field of an HTML form comes as an empty string, not undefined. If '' were a regular string, a required "Name" field with an empty input would pass, while an optional "Website" field would fail the URL format check.

Laravel does the same — the ConvertEmptyStringsToNull middleware turns empty strings into null before validation.

Common cases ​

ts
// Required: '' fails with Required
name: string()

// Optional: '' and undefined are skipped, the field is not added to data
website: optional(pipe(string(), pattern(/^https?:\/\//)))

// Optional with a default value
role: optional(oneOf(['admin', 'user']), 'user')

// May be null (e.g. from JSON) or missing
middleName: optional(nullable(string()))

// An empty string is a valid value
comment: optional(string(), '')

Whitespace-only strings ​

' ' is not empty, so string() accepts it. To reject such values, trim and check the length:

ts
name: pipe(string(), trim(), minLength(1))

See also ​