Comprehensive validation for numeric data during the Flatfile import process with support for ranges, formats, and special number types.
commit:created
listener event to analyze and validate data from specified fields.
Its main purpose is to ensure that numeric data conforms to a wide range of rules before being accepted into the system. Use cases include:
validateNumber
function accepts a single configuration object with the following properties:
Parameter | Type | Description |
---|---|---|
fields | string[] | An array of field keys (column names) to which the validation rules should be applied |
Parameter | Type | Default | Description |
---|---|---|---|
sheetSlug | string | '**' | The slug of the sheet to apply the validation to. Defaults to all sheets |
min | number | undefined | The minimum allowed value for the number |
max | number | undefined | The maximum allowed value for the number |
inclusive | boolean | false | Determines if the min and max values are inclusive |
integerOnly | boolean | false | If true, the value must be an integer (no decimal part) |
precision | number | undefined | The total maximum number of digits allowed (requires scale to be set) |
scale | number | undefined | The maximum number of digits allowed after the decimal point (requires precision to be set) |
currency | boolean | false | If true, validates that the number is a valid currency format, allowing up to two decimal places |
step | number | undefined | The value must be a multiple of this number |
thousandsSeparator | string | ',' | The character used as a thousands separator in the input string |
decimalPoint | string | '.' | The character used as the decimal point in the input string |
specialTypes | string[] | undefined | An array of special number types to validate against. Supported values: 'prime' , 'even' , 'odd' |
round | boolean | false | If true, the number is rounded to the nearest integer before validation |
truncate | boolean | false | If true, the decimal part of the number is removed before validation |
listener.use()
.
Parameters:
config
(object): Configuration object containing validation rules and target fields(listener: FlatfileListener) => void
that registers the validation hook.
Example:
value
(string | number): The input value to validateconfig
(NumberValidationConfig): Configuration object with validation rulesNumberValidationResult
object with:
value
(number | null): The parsed numeric value or null if parsing failederrors
(string[]): Array of error messages for fundamental parsing failureswarnings
(string[]): Array of warning messages for validation rule violationsspecialTypes: ['prime']
is configured.
Parameters:
num
(number): The number to checkboolean
- True if the number is prime, false otherwise
fields
and sheetSlug
configuration to ensure the plugin is targeting the correct datathousandsSeparator
and decimalPoint
settings match your data formatsrc/validate.number.plugin.spec.ts
provides clear examples of expected outcomes for every configuration optionmin
/max
validation is exclusive by default. To include boundary values, set inclusive: true
thousandsSeparator
is ','
and decimalPoint
is '.'
round
and truncate
are true, rounding occurs first, then truncationcommit:created
event, running after user submission but before data finalizationFlatfileRecord
in place, overwriting original values with parsed numeric values