0.3.1 • Published 10 months ago
@flatfile/plugin-validate-boolean v0.3.1
@flatfile/plugin-validate-boolean
The @flatfile/plugin-validate-boolean plugin provides comprehensive boolean validation capabilities, offering flexible configuration options to handle various boolean representations across different languages and use cases.
Event Type:
listener.on('commit:created')
Features
- Supports both strict and truthy boolean validation
- Multi-language support (English, Spanish, French, German)
- Custom mapping for boolean values
- Case-sensitive and case-insensitive options
- Configurable null value handling
- Option to convert non-boolean values
- Custom error messages
- Default value setting
Installation
To install the plugin, run the following command:
npm install @flatfile/plugin-validate-booleanExample Usage
import { validateBoolean } from '@flatfile/plugin-validate-boolean';
listener.use(
validateBoolean({
fields: ['isActive', 'hasSubscription'],
validationType: 'truthy',
language: 'en',
handleNull: 'false',
convertNonBoolean: true
}
));Configuration
The BooleanValidator function accepts a configuration object with the following properties:
fields: An array of field names to validatevalidationType: 'strict' or 'truthy'customMapping: A custom mapping of string values to booleancaseSensitive: Whether the validation should be case-sensitivehandleNull: How to handle null values ('error', 'false', 'true', or 'skip')convertNonBoolean: Whether to convert non-boolean values to booleanlanguage: The language for predefined mappings ('en', 'es', 'fr', 'de')customErrorMessages: Custom error messages for different scenariosdefaultValue: A default value to use for invalid inputs
Behavior
- Strict Validation: Only accepts 'true', 'false', true, or false as valid inputs.
- Truthy Validation: Accepts various representations of true/false, including language-specific terms.
- Null Handling: Configurable behavior for null or undefined values.
- Non-Boolean Conversion: Option to convert non-boolean values to boolean.
- Error Handling: Adds errors or info messages to the record for invalid inputs.
- Default Value: Option to set a default value for invalid inputs instead of raising an error.
The plugin can be used either as a RecordHook or as an external constraint, providing flexibility in integration with your Flatfile setup.