Skip to main content
The Autocast plugin is an opinionated transformer for Flatfile that automatically converts data in a Sheet to match the data types defined in the corresponding Blueprint (Schema). It operates on the commit:created event, meaning it processes records after they have been committed to the sheet. Its primary purpose is to clean and standardize data by ensuring that values intended to be numbers, booleans, or dates are correctly typed, even if they are imported as strings. For example, it can convert the string “1,000” to the number 1000, the string “yes” to the boolean true, and the string “08/16/2023” to a standardized UTC date string. This plugin is useful in any scenario where source data may have inconsistent or incorrect data types, saving developers from writing manual data-casting logic.

Installation

Install the plugin using npm:

Configuration & Parameters

Main Plugin Function

The autocast function accepts the following parameters:
string
required
The slug of the sheet that the plugin should monitor and apply autocasting to.
string[]
An optional array of field keys. If provided, the plugin will only attempt to cast values in the specified fields.Default Behavior: If not provided, the plugin will automatically attempt to cast all fields in the sheet that are not of type ‘string’ in the Blueprint (i.e., it will target number, boolean, and date fields by default).
object
Configuration options for performance and debugging:

Usage Examples

Basic Usage

Apply autocasting to all supported fields on a sheet:

Targeted Field Casting

Apply autocasting to only specific fields:

Advanced Configuration

Configure field filters and adjust performance settings:

Using Utility Functions

The plugin also exports individual casting utility functions:

Troubleshooting

Error Handling with Utility Functions

The individual casting utility functions throw errors when values cannot be converted:

Notes

Event Trigger

The plugin is designed to run on the listener.on('commit:created') event.

Plugin Order

This plugin runs on the same event as recordHook and bulkRecordHook. The order in which you .use() the plugins in your listener matters, as they will execute sequentially.

Error Handling Pattern

The main autocast plugin does not throw errors. Instead, if a value cannot be cast, it attaches an error message directly to the record’s cell using record.addError(). This makes the errors visible to the user in the Flatfile UI. The individual cast* utility functions, however, do throw an Error on failure.

Supported Types

The plugin automatically targets fields of type number, boolean, and date as defined in the Sheet’s Blueprint. It does not attempt to cast string fields by default.

Boolean Casting

  • Truthy values: '1', 'yes', 'true', 'on', 't', 'y', and 1
  • Falsy values: '-1', '0', 'no', 'false', 'off', 'f', 'n', 0, and -1

Date Casting

All parsed dates are converted to a standardized UTC string format. ISO 8601 formats like YYYY-MM-DD are treated as UTC, while other formats like MM/DD/YYYY are assumed to be local time and are converted to UTC.