Skip to content
Logo

T.omittable

An absence primitive: a field that is sometimes missing from its object entirely. It only makes sense inside T.object(...), because it decides whether a key exists, not what a value is.

T.object({
  name: T.string.whereby({ length: { max: 20 } }),
  nickname: T.omittable(T.string.whereby({ length: { max: 20 } })),
});

By default it is 50/50: nickname is either missing from the fabricated object or holds a fabricated string. You can change the odds:

T.omittable(inner).weighted({ omitted: 1, value: 4 }); // present 80% of the time

To force the field to be left out, pass Omitted to .override() or .fabricate(overrides):

import { Omitted } from "@ghostry/fabricator";
 
WidgetFabricator.fabricate({ nickname: Omitted });

For a field that can be left out, present as undefined, or present with a value, use T.optional, not T.omittable(T.undefinable(inner)). Nesting the two gives 50% left out, 25% undefined, and 25% a value, instead of even odds. For a key that is always present but whose value may be undefined, use T.undefinable.