Skip to content

Commit

Permalink
Merge pull request #910 from StackStorm/int_yaql
Browse files Browse the repository at this point in the history
Allow integer fields with YAQL expressions to be saved
  • Loading branch information
amanda11 committed Sep 20, 2021
2 parents 065f5c6 + d3f5b1d commit 3236dd2
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 7 deletions.
22 changes: 18 additions & 4 deletions apps/st2-actions/actions-details.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,18 @@ export default class ActionsDetails extends React.Component {
return false;
}

isExpression (value) {
if (value.startsWith('{{') && value.endsWith('}}')) {
return true;
}
else if (value.startsWith('<%') && value.endsWith('%>')) {
return true;
}
else {
return false;
}
}

isValidInt (value) {
for ( let n = 0; n < value.length; n += 1) {
const digit = (value.charCodeAt(n) >= 48 && value.charCodeAt(n) <= 57) || value.charCodeAt(n) === 45 || value.charCodeAt(n) === 8;
Expand Down Expand Up @@ -280,10 +292,12 @@ export default class ActionsDetails extends React.Component {
<DetailsToolbar key="toolbar">
<Button
disabled={
(this.state.runValue && this.state.runValue.timeout && this.minMax(this.state.runValue.timeout)) ||
(this.state.runValue && this.state.runValue.limit && this.minMax(this.state.runValue.limit)) ||
(this.state.runValue && this.state.runValue.timeout && this.isValidInt(this.state.runValue.timeout)) ||
(this.state.runValue && this.state.runValue.limit && this.isValidInt(this.state.runValue.limit))
(this.state.runValue && this.state.runValue.timeout && !this.isExpression(this.state.runValue.timeout) &&
(this.isValidInt(this.state.runValue.timeout) ||
this.minMax(this.state.runValue.timeout))) ||
(this.state.runValue && this.state.runValue.limit && !this.isExpression(this.state.runValue.limit) &&
(this.isValidInt(this.state.runValue.limit) ||
this.minMax(this.state.runValue.limit)))
}
value="Run" data-test="run_submit" onClick={(e) => this.handleRun(e, action.ref, this.state.runValue, this.state.runTrace || undefined)}
/>
Expand Down
5 changes: 5 additions & 0 deletions modules/st2-auto-form/fields/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ export class BaseTextField extends React.Component {
return false;
}

if (isYaql(v)) {
return false;
}

return undefined;

}

handleChange(e, value) {
Expand Down
10 changes: 9 additions & 1 deletion modules/st2-auto-form/fields/integer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import validator from 'validator';

import { BaseTextField, isJinja } from './base';
import { BaseTextField, isJinja, isYaql } from './base';

export default class IntegerField extends BaseTextField {
static icon = '12'
Expand All @@ -24,6 +24,10 @@ export default class IntegerField extends BaseTextField {
return v;
}

if (isYaql(v)) {
return v;
}

if (this.props.name === 'timeout' || this.props.name === 'limit') {
return v ;
}
Expand All @@ -37,6 +41,10 @@ export default class IntegerField extends BaseTextField {
return v;
}

if (isYaql(v)) {
return v;
}

return v ? v.toString(10) : '';
}

Expand Down
10 changes: 9 additions & 1 deletion modules/st2-auto-form/fields/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import validator from 'validator';

import { BaseTextField, isJinja } from './base';
import { BaseTextField, isJinja, isYaql } from './base';

export default class NumberField extends BaseTextField {
static icon = '.5'
Expand All @@ -24,6 +24,10 @@ export default class NumberField extends BaseTextField {
return v;
}

if (isYaql(v)) {
return v;
}

return v !== '' ? validator.toFloat(v) : void 0;
}

Expand All @@ -32,6 +36,10 @@ export default class NumberField extends BaseTextField {
return v;
}

if (isYaql(v)) {
return v;
}

return v ? v.toString(10) : '';
}

Expand Down
10 changes: 9 additions & 1 deletion modules/st2-auto-form/fields/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

import _ from 'lodash';
import { BaseTextareaField, isJinja } from './base';
import { BaseTextareaField, isJinja, isYaql } from './base';

export default class ObjectField extends BaseTextareaField {
static icon = '{ }'
Expand All @@ -23,6 +23,10 @@ export default class ObjectField extends BaseTextareaField {
return v;
}

if (isYaql(v)) {
return v;
}

return v !== '' && v !== undefined ? JSON.parse(v) : void 0;
}

Expand All @@ -31,6 +35,10 @@ export default class ObjectField extends BaseTextareaField {
return v;
}

if (isYaql(v)) {
return v;
}

return JSON.stringify(v || {});
}

Expand Down

0 comments on commit 3236dd2

Please sign in to comment.