Installation
Install the plugin using npm:Configuration & Parameters
The plugin is configured with a singleStringValidationConfig
object with the following properties:
Required Parameters
An array of field keys (column names) to which the validation rules should be applied.
Optional Parameters
The slug of a specific sheet to apply the validations to. Defaults to ’**’ (applies to all sheets in the workbook).
A regular expression to validate the string against. You can use one of the predefined string keys for common patterns (‘email’, ‘phone’, ‘url’) or provide your own custom RegExp object.
The minimum allowed length for the string.
The maximum allowed length for the string.
The exact required length for the string.
Enforces a specific character case. If the input string does not match, it will be transformed, and a validation message will be added.
An object to control whitespace trimming. If
leading
is true, it trims whitespace from the start. If trailing
is true, it trims from the end. If the string is trimmed, the value is transformed, and a message is added.Controls whether an empty string is considered a valid value. By default, empty strings will generate a “Field cannot be empty” error.
An object to provide custom error messages for different validation types, overriding the default messages. The plugin provides default messages for each validation type (e.g., “Invalid format”, “Minimum length is X”).
Usage Examples
Basic Usage
Email Validation
Advanced Custom Pattern Validation
Using the Utility Function
Error Handling Example
API Reference
validateString(config)
The main entry point for the plugin. It configures and registers arecordHook
that listens for new commits and applies the specified string validations to each record.
Parameters:
config
(StringValidationConfig): An object that defines the validation and transformation rules.
FlatfileListener
instance and attaches the validation logic to it.
validateAndTransformString(value, config)
An exported utility function that runs the validation and transformation logic on a single string value. It is used internally by the plugin but can be used for custom validation logic if needed. Parameters:value
(string): The input string to validate and transform.config
(StringValidationConfig): The configuration object defining the rules to apply.
ValidationResult
with two properties:
value
(string): The original or transformed string value.error
(string | null): An error message string if any validation fails, otherwisenull
.
Notes
Default Behavior
- Empty strings: By default, empty strings are considered invalid. To allow them, you must explicitly set
emptyStringAllowed: true
in the configuration. - Sheet targeting: When no
sheetSlug
is specified, the plugin applies to all sheets in the workbook using the default value ’**’. - Error messages: The plugin provides default error messages for each validation type (e.g., “Invalid format”, “Minimum length is X”) which can be customized using the
errorMessages
configuration option.
Important Considerations
- The plugin operates using the
@flatfile/plugin-record-hook
, which is a dependency. It processes records individually during thecommit:created
event. - The plugin can modify data. When a transformation is applied (e.g., changing case or trimming whitespace), the record’s value is updated with
record.set()
. - By default, when a transformation is applied, the plugin also adds a validation message to the cell (e.g., “Field value must be in titlecase”). This informs the user that their original data was automatically corrected.
- The plugin handles
null
andundefined
values by simply skipping them. Validation is only applied to defined string values. - The plugin does not throw exceptions. Instead, it captures validation failures and adds them as errors to the corresponding record and field using
record.addError(field, message)
. These errors are then visible to the user in the Flatfile UI.