Automatically convert data in a Flatfile Sheet to match the data types defined in the corresponding Blueprint
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.
autocast
function accepts the following parameters:
listener.on('commit:created')
event.
recordHook
and bulkRecordHook
. The order in which you .use()
the plugins in your listener matters, as they will execute sequentially.
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.
number
, boolean
, and date
as defined in the Sheet’s Blueprint. It does not attempt to cast string
fields by default.
'1'
, 'yes'
, 'true'
, 'on'
, 't'
, 'y'
, and 1
'-1'
, '0'
, 'no'
, 'false'
, 'off'
, 'f'
, 'n'
, 0
, and -1
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.