Skip to content

Commit

Permalink
fix: incorrect negative value conversion (#4316)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle-ssg authored Aug 21, 2024
1 parent 3f561ee commit 2931cdf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion frontend/common/utils/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ const Utils = Object.assign({}, require('./base/_utils'), {
}

const typedValue = testWithTrim ? str.trim() : str
const isNum = /^\d+$/.test(typedValue)
const isNum = /^-?\d+$/.test(typedValue)

if (isNum && parseInt(typedValue) > Number.MAX_SAFE_INTEGER) {
return `${str}`
Expand Down
40 changes: 39 additions & 1 deletion frontend/web/components/Feature.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,31 @@ import AddVariationButton from './mv/AddVariationButton'
import ErrorMessage from './ErrorMessage'
import Tooltip from './Tooltip'
import Icon from './Icon'
import InputGroup from './base/forms/InputGroup'
import WarningMessage from './WarningMessage'

function isNegativeNumberString(str) {
if (typeof Utils.getTypedValue(str) !== 'number') {
return false
}
if (typeof str !== 'string') {
return false
}
const num = parseFloat(str)
return !isNaN(num) && num < 0
}

export default class Feature extends PureComponent {
static displayName = 'Feature'

constructor(props) {
super(props)
this.state = {
isNegativeNumberString: isNegativeNumberString(
props.environmentFlag?.feature_state_value,
),
}
}
removeVariation = (i) => {
const idToRemove = this.props.multivariate_options[i].id

Expand Down Expand Up @@ -102,13 +123,30 @@ export default class Feature extends PureComponent {
}
tooltip={`${Constants.strings.REMOTE_CONFIG_DESCRIPTION}${
!isEdit
? '<br/>Setting this when creating a feature will set the value for all environments. You can edit the this individually for each environment once the feature is created.'
? '<br/>Setting this when creating a feature will set the value for all environments. You can edit this individually for each environment once the feature is created.'
: ''
}`}
title={`${valueString}`}
/>
</FormGroup>
)}
{this.state.isNegativeNumberString && (
<WarningMessage
warningMessage={
<div>
This feature currently has the value of{' '}
<strong>"{environmentFlag?.feature_state_value}"</strong>.
Saving this feature will convert its value from a string to a
number. If you wish to preserve this value as a string, please
save it using the{' '}
<a href='https://api.flagsmith.com/api/v1/docs/#/api/api_v1_environments_featurestates_update'>
API
</a>
.
</div>
}
/>
)}

{!!error && (
<div className='mx-2 mt-2'>
Expand Down

0 comments on commit 2931cdf

Please sign in to comment.