Skip to content

Commit

Permalink
Linter run
Browse files Browse the repository at this point in the history
  • Loading branch information
meta-paul committed Mar 15, 2024
1 parent 17870fa commit d56de4e
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// NOTE: that `react-form-composer` library must be set in webpack config as an alias.
import { validateFieldValue } from "react-form-composer";


export function onChangeCountry(
formData, // React state for the entire form
updateFormData, // callback to set the React state
Expand All @@ -24,8 +23,8 @@ export function onChangeCountry(
const newMottoValueIsValid = validateFieldValue(formFields.motto, "", true);
if (!newMottoValueIsValid) {
console.log(
'Write additional log message or logic here ' +
'if logs with argument `writeConsoleLog` is not enough for you'
"Write additional log message or logic here " +
"if logs with argument `writeConsoleLog` is not enough for you"
);
}

Expand Down
2 changes: 1 addition & 1 deletion mephisto/abstractions/architects/router/node/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ app.post("/submit_task", upload.any(), function (req, res) {
// Group files by fieldname to link them with fields
const filesByFields = {};
req.files.forEach((file) => {
const _file = {...file};
const _file = { ...file };
delete _file.fieldname;
filesByFields[file.fieldname] = _file;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ function InReviewFileModal(props: InReviewFileModalProps) {
setFileExt(null);

if (data.filename) {
setFileUrl(urls.server.unitsOutputsFileByFieldname(data.unitId, data.fieldname));
setFileUrl(
urls.server.unitsOutputsFileByFieldname(data.unitId, data.fieldname)
);
setFileExt(data.filename.split(".").pop().toLowerCase());
}
}, [data]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function CheckboxField({
updateFormData,
field,
value,
formFields,
formFields
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function FileField({
updateFormData,
field,
value,
formFields,
formFields
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function InputField({
updateFormData,
field,
value,
formFields,
formFields
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function RadioField({
updateFormData,
field,
value,
formFields,
formFields
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function SelectField({
updateFormData,
field,
value,
formFields,
formFields
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function TextareaField({
updateFormData,
field,
value,
formFields,
formFields
);
}

Expand Down
73 changes: 39 additions & 34 deletions packages/react-form-composer/src/FormComposer/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ export function runCustomTrigger(
updateFormData, // callback to set the React state
element, // "field", "section", or "submit button" element that invoked this trigger
fieldValue, // Current field value, if the `element` is a form field (otherwise it's null)
formFields, // Object containing all form fields as defined in 'form_config.json'
formFields // Object containing all form fields as defined in 'form_config.json'
) {
// Exit if the element that doesn't have any triggers defined (no logs required)
if (!elementTriggersConfig || isObjectEmpty(elementTriggersConfig)) {
Expand Down Expand Up @@ -405,7 +405,14 @@ export function runCustomTrigger(

// Run custom trigger
try {
triggerFn(formData, updateFormData, element, fieldValue, formFields, ...triggerFnArgs);
triggerFn(
formData,
updateFormData,
element,
fieldValue,
formFields,
...triggerFnArgs
);
} catch (error) {
const textAboutElement = fieldValue
? `Field "${element.name}" value: ${fieldValue}. `
Expand Down Expand Up @@ -470,7 +477,7 @@ export function validateFieldValue(field, value, writeConsoleLog) {
}

if ([null, undefined].includes(field)) {
console.error(`Argument 'field' cannot be '${field}'.`)
console.error(`Argument 'field' cannot be '${field}'.`);
return false;
}

Expand All @@ -493,88 +500,86 @@ export function validateFieldValue(field, value, writeConsoleLog) {
logMessage = `Value must be a 'string' for field '${field.name}' with type '${field.type}'.`;
} else {
if (field.type === FieldType.RADIO) {
const availableOptions = Object.values(field.options).map((o) => o.value);
const availableOptions = Object.values(field.options).map(
(o) => o.value
);

if (!availableOptions.includes(value)) {
valueIsValid = false;
logMessage = (
logMessage =
`Incorrect value for field '${field.name}' with type '${field.type}'. ` +
`Available options: ${availableOptions}. `
);
`Available options: ${availableOptions}. `;
}
}
}
}

else if (field.type === FieldType.CHECKBOX) {
} else if (field.type === FieldType.CHECKBOX) {
if (typeof value === "object" && !Array.isArray(value)) {
const availableOptions = Object.values(field.options).map((o) => o.value);

const allParamsAreBooleansWithCorrectOption = Object.entries(value).every(([k, v]) => {
return availableOptions.includes(k) && typeof v === "boolean";
});
const allParamsAreBooleansWithCorrectOption = Object.entries(value).every(
([k, v]) => {
return availableOptions.includes(k) && typeof v === "boolean";
}
);

if (!allParamsAreBooleansWithCorrectOption) {
valueIsValid = false;
logMessage = (
logMessage =
`All value parameters must be 'boolean' ` +
`for field '${field.name}' with type '${field.type}'. ` +
`Available options: ${availableOptions}. `
);
`Available options: ${availableOptions}. `;
}
} else {
valueIsValid = false;
logMessage = (
logMessage =
`Value must be an 'object' with boolean parameters ` +
`for field '${field.name}' with type '${field.type}'.`
);
`for field '${field.name}' with type '${field.type}'.`;
}
}

else if (field.type === FieldType.SELECT) {
} else if (field.type === FieldType.SELECT) {
if (field.multiple) {
if (!Array.isArray(value)) {
valueIsValid = false;
logMessage = (
logMessage =
`Value must be an 'array' for field '${field.name}' with type '${field.type}' ` +
`and '"multiple": true'.`
);
`and '"multiple": true'.`;
} else {
const availableOptions = Object.values(field.options).map((o) => o.value);
const availableOptions = Object.values(field.options).map(
(o) => o.value
);

const allParamsAreStringsWithCorrectOption = value.every((v) => {
return typeof v === "string" && availableOptions.includes(v);
});

if (!allParamsAreStringsWithCorrectOption) {
valueIsValid = false;
logMessage = (
logMessage =
`All value parameters must be 'string' ` +
`for field '${field.name}' with type '${field.type}' and '"multiple": true'. ` +
`Available options: ${availableOptions}. `
);
`Available options: ${availableOptions}. `;
}
}
} else {
if (typeof value !== "string") {
valueIsValid = false;
logMessage = (
logMessage =
`Value must be a 'string' ` +
`for field '${field.name}' with type '${field.type}' and '"multiple": false'.`
);
`for field '${field.name}' with type '${field.type}' and '"multiple": false'.`;
}
}
}

if (writeConsoleLog && !valueIsValid && logMessage) {
logMessage += ` You passed value '${JSON.stringify(value)}' with type '${typeof value}'`;
logMessage += ` You passed value '${JSON.stringify(
value
)}' with type '${typeof value}'`;
console.error(logMessage);
}

if (writeConsoleLog && valueIsValid) {
console.info(
`Value '${JSON.stringify(value)}' for field '${field.name}' ` +
`with type '${typeof value}' is valid`
`with type '${typeof value}' is valid`
);
}

Expand Down

0 comments on commit d56de4e

Please sign in to comment.