locus

Nullable vs Optional Fields

This guide explains the distinction between optional and nullable field types in Locus.

Concepts

These are intentionally different: an optional but non-nullable field has three logical states during input (omitted, provided non-null, provided null) but only two are valid (omitted or non-null). To store an actual NULL you must mark the type as nullable.

Syntax Summary

name: String?             # optional, NOT nullable
middleName: String | Null # nullable (and implicitly required unless suffixed with ?)
comment: Text? | Null     # optional AND nullable (can be omitted or set explicitly to null)
flag: Integer nullable    # keyword form

Order is not significant; both Text? | Null and Text | Null? are normalized internally.

Validation Rules

Prisma Mapping (Current)

At present both optional and nullable compile to Prisma’s ? suffixed field for simplicity. Future differentiation will:

Migration from Legacy Behavior

Historically ? implied both optional and nullable. To migrate:

Legacy New Explicit Form
name: String? intending nullable name: String | Null
name: String? intending optional (not nullable) keep as-is

FAQs

  1. Why separate them? Clarity in API semantics (omit vs explicit null) and enabling future diff-based update optimization.
  2. How to allow explicit null updates? Add | Null.

Next Steps

The generator will evolve to produce different runtime handling once nullable semantics diverge. Track progress in overhaul-checklist.md under Nullable vs Optional divergence.