Skip to content

Commit

Permalink
- 2927: IDE - Validate user input data string/array/object and only t…
Browse files Browse the repository at this point in the history
…ry to transform when needed
  • Loading branch information
Paulo Andre Azevedo Quirino committed Aug 29, 2024
1 parent 6a883ca commit 86f8942
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 38 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# v1.2.6
# TBD

- [FP-2739](https://movai.atlassian.net/browse/FP-2739): Port properties of the node templates are turning to strings after editing them
- [FP-2716](https://movai.atlassian.net/browse/FP-2716): Annotations imported cannot be edited
- [FP-2711](https://movai.atlassian.net/browse/FP-2711): Corrupted data in Annotation
- [FP-2927](https://movai.atlassian.net/browse/FP-2927): IDE - Validate user input data string/array/object and only try to transform when needed

# v1.2.6

- [FP-2739](https://movai.atlassian.net/browse/FP-2739): Port properties of the node templates are turning to strings after editing them

# v1.2.5

Expand Down
16 changes: 9 additions & 7 deletions src/editors/_shared/hooks/DataTypes/AbstractDataType.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,24 @@ class AbstractDataType {
* @param {*} parsedValue : Parsed value (can be a string, array, or object)
* @returns {ReactComponent} Text input for editing common strings
*/
stringEditComponent(props, placeholder, parsedValue) {
stringEditComponent(props, placeholder, parsedValue, options = {}) {
const { parse = a => a, unparse = a => a } = options;

const value = (parsedValue !== undefined ? parsedValue : props.rowData.value) ?? "";

return (
<TextField
inputProps={{ "data-testid": "input_value" }}
fullWidth
placeholder={placeholder}
defaultValue={
typeof value === "object" || Array.isArray(value)
? JSON.stringify(value) : value
}
defaultValue={unparse(value)}
onChange={evt => {
try {
props.onChange(JSON.parse(evt.target.value));
const parsedValue = parse(evt.target.value);
props.onChange(parsedValue);
} catch (e) {
return props.onChange(evt.target.value);
props.onChange(evt.target.value);
console.error("Error parsing value", e);
}
}}
></TextField>
Expand Down
20 changes: 6 additions & 14 deletions src/editors/_shared/hooks/DataTypes/types/ArrayType.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ class ArrayType extends DataType {
// Array type properties definition
key = DATA_TYPES.ARRAY;
label = "Array";
default = "[]";
default = [];

editComponent = (props, mode = "row") => {
const editor = {
row: _props => this.stringEditComponent(_props, this.default),
row: _props => this.stringEditComponent(_props, this.default, undefined, {
parse: a => JSON.parse(a),
unparse: a => JSON.stringify(a),
}),
dialog: this.codeEditComponent
};
return editor[mode](props);
Expand All @@ -22,18 +25,7 @@ class ArrayType extends DataType {
* @returns
*/
validate(value) {
value = typeof value === "string" ? value.replace(/'/g, '"'): value;
return new Promise(resolve => {
try {
if (checkIfDefaultOrDisabled(value)) {
return resolve({ success: true, value });
}
const parsed = this.getParsedValue(value);
resolve({ success: Array.isArray(parsed), value });
} catch (e) {
resolve({ success: false, value });
}
});
return Promise.resolve({ success: Array.isArray(value) });
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/editors/_shared/hooks/DataTypes/types/NumberType.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ class NumberType extends DataType {
// Number type properties definition
key = DATA_TYPES.NUMBER;
label = "Number";
default = "0";
default = 0;

editComponent = props => {
return (
<TextField
fullWidth
type={DATA_TYPES.NUMBER}
placeholder={"0"}
placeholder={this.default}
value={props.rowData.value || ""}
disabled={props.disabled}
onChange={evt => props.onChange(evt.target.value)}
Expand Down
19 changes: 6 additions & 13 deletions src/editors/_shared/hooks/DataTypes/types/ObjectType.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ class ObjectType extends DataType {
// Object type properties definition
key = DATA_TYPES.OBJECT;
label = "Object";
default = "{}";
default = {};

editComponent = (props, mode = "row") => {
const editor = {
row: _props => this.stringEditComponent(_props, this.default),
row: _props => this.stringEditComponent(_props, this.default, undefined, {
parse: a => JSON.parse(a),
unparse: a => JSON.stringify(a),
}),
dialog: this.codeEditComponent
};
return editor[mode](props);
Expand All @@ -22,17 +25,7 @@ class ObjectType extends DataType {
* @returns
*/
validate(value) {
return new Promise(resolve => {
try {
if (checkIfDefaultOrDisabled(value)) {
return resolve({ success: true, value });
}
const parsed = JSON.parse(value);
resolve({ success: parsed.constructor === Object });
} catch (e) {
resolve({ success: false });
}
});
return Promise.resolve({ success: typeof(value) === "object" });
}
}

Expand Down

0 comments on commit 86f8942

Please sign in to comment.