🐛 (number) Fix number input validation with variables
This commit is contained in:
@ -1,10 +1,29 @@
|
||||
import { isNotDefined } from '@typebot.io/lib'
|
||||
import { NumberInputBlock } from '@typebot.io/schemas'
|
||||
import { NumberInputBlock, Variable } from '@typebot.io/schemas'
|
||||
import { parseVariables } from '../../../variables/parseVariables'
|
||||
|
||||
export const validateNumber = (
|
||||
inputValue: string,
|
||||
{
|
||||
options,
|
||||
variables,
|
||||
}: {
|
||||
options: NumberInputBlock['options']
|
||||
) =>
|
||||
variables: Variable[]
|
||||
}
|
||||
) => {
|
||||
const min =
|
||||
options?.min && typeof options.min === 'string'
|
||||
? Number(parseVariables(variables)(options.min))
|
||||
: undefined
|
||||
const max =
|
||||
options?.min && typeof options.min === 'string'
|
||||
? Number(parseVariables(variables)(options.min))
|
||||
: undefined
|
||||
|
||||
return (
|
||||
inputValue !== '' &&
|
||||
(isNotDefined(options?.min) || Number(inputValue) >= Number(options.min)) &&
|
||||
(isNotDefined(options?.max) || Number(inputValue) <= Number(options.max))
|
||||
(isNotDefined(min) || Number(inputValue) >= min) &&
|
||||
(isNotDefined(max) || Number(inputValue) <= max)
|
||||
)
|
||||
}
|
||||
|
@ -345,7 +345,10 @@ const parseReply =
|
||||
}
|
||||
case InputBlockType.NUMBER: {
|
||||
if (!inputValue) return { status: 'fail' }
|
||||
const isValid = validateNumber(inputValue, block.options)
|
||||
const isValid = validateNumber(inputValue, {
|
||||
options: block.options,
|
||||
variables: state.typebotsQueue[0].typebot.variables,
|
||||
})
|
||||
if (!isValid) return { status: 'fail' }
|
||||
return { status: 'success', reply: parseNumber(inputValue) }
|
||||
}
|
||||
|
Reference in New Issue
Block a user