commit a47fecc5b1ed809375cff4c9eda93010c43d9c76
parent 6fb92223da27845bfb79113d8c47abf32ba93ee6
Author: Sebastian <sebasjm@gmail.com>
Date: Tue, 18 Mar 2025 14:29:27 -0300
prevent undefined on root value of the form
Diffstat:
6 files changed, 45 insertions(+), 49 deletions(-)
diff --git a/packages/web-util/src/forms/forms-types.ts b/packages/web-util/src/forms/forms-types.ts
@@ -46,7 +46,7 @@ export type DoubleColumnFormSection = {
return if the field should be hidden.
receives the value after conversion and the root of the form.
*/
- hide?: (root: undefined | Record<string, any>) => boolean | undefined;
+ hide?: (root: Record<string, any>) => boolean | undefined;
};
export type UIFormElementConfig =
diff --git a/packages/web-util/src/forms/gana/VQF_902_14.ts b/packages/web-util/src/forms/gana/VQF_902_14.ts
@@ -57,13 +57,10 @@ export function VQF_902_14(
label: i18n.str`Means clarification`,
required: true,
hide(value, root) {
- if (!root) return true;
- if (
- root[TalerFormAttributes.VQF_902_14.INCRISK_MEANS.id] ===
+ return (
+ root[TalerFormAttributes.VQF_902_14.INCRISK_MEANS.id] !==
"OTHER"
- )
- return false;
- return true;
+ );
},
},
],
@@ -122,13 +119,10 @@ export function VQF_902_14(
label: i18n.str`Result clarification`,
required: true,
hide(value, root) {
- if (!root) return true;
- if (
- root[TalerFormAttributes.VQF_902_14.INCRISK_RESULT.id] ===
+ return (
+ root[TalerFormAttributes.VQF_902_14.INCRISK_RESULT.id] !==
"OTHER"
- )
- return false;
- return true;
+ );
},
},
],
diff --git a/packages/web-util/src/forms/gana/VQF_902_1_customer.ts b/packages/web-util/src/forms/gana/VQF_902_1_customer.ts
@@ -43,7 +43,7 @@ member act as director of a domiciliary company, this domiciliary company is the
title: i18n.str`Information on customer`,
description: i18n.str`The customer is a natural person`,
hide(root) {
- return !root || root["CUSTOMER_INFO_TYPE"] !== "NATURAL_PERSON";
+ return root["CUSTOMER_INFO_TYPE"] !== "NATURAL_PERSON";
},
fields: [
{
@@ -52,7 +52,7 @@ member act as director of a domiciliary company, this domiciliary company is the
type: "text",
required: true,
hide(value, root) {
- return !root || root["CUSTOMER_INFO_TYPE"] !== "NATURAL_PERSON";
+ return root["CUSTOMER_INFO_TYPE"] !== "NATURAL_PERSON";
},
},
{
@@ -61,7 +61,7 @@ member act as director of a domiciliary company, this domiciliary company is the
type: "textArea",
required: true,
hide(value, root) {
- return !root || root["CUSTOMER_INFO_TYPE"] !== "NATURAL_PERSON";
+ return root["CUSTOMER_INFO_TYPE"] !== "NATURAL_PERSON";
},
},
{
@@ -70,7 +70,7 @@ member act as director of a domiciliary company, this domiciliary company is the
type: "text",
required: false,
hide(value, root) {
- return !root || root["CUSTOMER_INFO_TYPE"] !== "NATURAL_PERSON";
+ return root["CUSTOMER_INFO_TYPE"] !== "NATURAL_PERSON";
},
},
{
@@ -79,7 +79,7 @@ member act as director of a domiciliary company, this domiciliary company is the
type: "text",
required: false,
hide(value, root) {
- return !root || root["CUSTOMER_INFO_TYPE"] !== "NATURAL_PERSON";
+ return root["CUSTOMER_INFO_TYPE"] !== "NATURAL_PERSON";
},
},
{
@@ -90,7 +90,7 @@ member act as director of a domiciliary company, this domiciliary company is the
pattern: "dd/MM/yyyy",
required: true,
hide(value, root) {
- return !root || root["CUSTOMER_INFO_TYPE"] !== "NATURAL_PERSON";
+ return root["CUSTOMER_INFO_TYPE"] !== "NATURAL_PERSON";
},
},
{
@@ -100,7 +100,7 @@ member act as director of a domiciliary company, this domiciliary company is the
choices: countryNationalityList(i18n),
required: true,
hide(value, root) {
- return !root || root["CUSTOMER_INFO_TYPE"] !== "NATURAL_PERSON";
+ return root["CUSTOMER_INFO_TYPE"] !== "NATURAL_PERSON";
},
},
{
@@ -110,7 +110,7 @@ member act as director of a domiciliary company, this domiciliary company is the
accept: "application/pdf",
required: true,
hide(value, root) {
- return !root || root["CUSTOMER_INFO_TYPE"] !== "NATURAL_PERSON";
+ return root["CUSTOMER_INFO_TYPE"] !== "NATURAL_PERSON";
},
},
],
@@ -119,7 +119,7 @@ member act as director of a domiciliary company, this domiciliary company is the
title: i18n.str`Information on customer (sole proprietor)`,
description: i18n.str`The customer is a sole proprietor`,
hide(root) {
- return !root || root["CUSTOMER_INFO_TYPE"] !== "NATURAL_PERSON";
+ return root["CUSTOMER_INFO_TYPE"] !== "NATURAL_PERSON";
},
fields: [
{
@@ -128,7 +128,7 @@ member act as director of a domiciliary company, this domiciliary company is the
type: "text",
required: false,
hide(value, root) {
- return !root || root["CUSTOMER_INFO_TYPE"] !== "NATURAL_PERSON";
+ return root["CUSTOMER_INFO_TYPE"] !== "NATURAL_PERSON";
},
},
{
@@ -138,7 +138,7 @@ member act as director of a domiciliary company, this domiciliary company is the
type: "text",
required: false,
hide(value, root) {
- return !root || root["CUSTOMER_INFO_TYPE"] !== "NATURAL_PERSON";
+ return root["CUSTOMER_INFO_TYPE"] !== "NATURAL_PERSON";
},
},
{
@@ -149,7 +149,7 @@ member act as director of a domiciliary company, this domiciliary company is the
required: false,
accept: "application/pdf",
hide(value, root) {
- return !root || root["CUSTOMER_INFO_TYPE"] !== "NATURAL_PERSON";
+ return root["CUSTOMER_INFO_TYPE"] !== "NATURAL_PERSON";
},
},
],
@@ -158,7 +158,7 @@ member act as director of a domiciliary company, this domiciliary company is the
title: i18n.str`Information on customer`,
description: i18n.str`The customer is a legal entity`,
hide(root) {
- return !root || root["CUSTOMER_INFO_TYPE"] !== "LEGAL_ENTITY";
+ return root["CUSTOMER_INFO_TYPE"] !== "LEGAL_ENTITY";
},
fields: [
{
@@ -167,7 +167,7 @@ member act as director of a domiciliary company, this domiciliary company is the
type: "text",
required: true,
hide(value, root) {
- return !root || root["CUSTOMER_INFO_TYPE"] !== "LEGAL_ENTITY";
+ return root["CUSTOMER_INFO_TYPE"] !== "LEGAL_ENTITY";
},
},
{
@@ -176,7 +176,7 @@ member act as director of a domiciliary company, this domiciliary company is the
type: "textArea",
required: true,
hide(value, root) {
- return !root || root["CUSTOMER_INFO_TYPE"] !== "LEGAL_ENTITY";
+ return root["CUSTOMER_INFO_TYPE"] !== "LEGAL_ENTITY";
},
},
{
@@ -186,7 +186,7 @@ member act as director of a domiciliary company, this domiciliary company is the
type: "text",
required: false,
hide(value, root) {
- return !root || root["CUSTOMER_INFO_TYPE"] !== "LEGAL_ENTITY";
+ return root["CUSTOMER_INFO_TYPE"] !== "LEGAL_ENTITY";
},
},
{
@@ -195,7 +195,7 @@ member act as director of a domiciliary company, this domiciliary company is the
type: "text",
required: false,
hide(value, root) {
- return !root || root["CUSTOMER_INFO_TYPE"] !== "LEGAL_ENTITY";
+ return root["CUSTOMER_INFO_TYPE"] !== "LEGAL_ENTITY";
},
},
{
@@ -204,7 +204,7 @@ member act as director of a domiciliary company, this domiciliary company is the
type: "text",
required: false,
hide(value, root) {
- return !root || root["CUSTOMER_INFO_TYPE"] !== "LEGAL_ENTITY";
+ return root["CUSTOMER_INFO_TYPE"] !== "LEGAL_ENTITY";
},
},
{
@@ -214,7 +214,7 @@ member act as director of a domiciliary company, this domiciliary company is the
accept: "application/pdf",
required: true,
hide(value, root) {
- return !root || root["CUSTOMER_INFO_TYPE"] !== "LEGAL_ENTITY";
+ return root["CUSTOMER_INFO_TYPE"] !== "LEGAL_ENTITY";
},
},
],
@@ -228,7 +228,7 @@ member act as director of a domiciliary company, this domiciliary company is the
label: i18n.str`Founders`,
type: "array",
hide(value, root) {
- return !root || root["CUSTOMER_INFO_TYPE"] !== "LEGAL_ENTITY";
+ return root["CUSTOMER_INFO_TYPE"] !== "LEGAL_ENTITY";
},
labelFieldId:
TalerFormAttributes.VQF_902_1_founder.FOUNDER_FULL_NAME.id,
diff --git a/packages/web-util/src/forms/gana/VQF_902_1_officer.ts b/packages/web-util/src/forms/gana/VQF_902_1_officer.ts
@@ -81,7 +81,6 @@ export function VQF_902_1_officer(
required: true,
hide(value, root) {
return (
- !!root &&
root["ACCEPTANCE_CORRESPONDENCE_SERVICE_TYPE"] !==
"TO_A_THIRD_PARTY"
);
@@ -95,7 +94,6 @@ export function VQF_902_1_officer(
required: true,
hide(value, root) {
return (
- !!root &&
root["ACCEPTANCE_CORRESPONDENCE_SERVICE_TYPE"] !==
"TO_A_THIRD_PARTY"
);
diff --git a/packages/web-util/src/forms/gana/VQF_902_5.ts b/packages/web-util/src/forms/gana/VQF_902_5.ts
@@ -75,14 +75,11 @@ export function VQF_902_5(
label: i18n.str`Category clarification`,
required: true,
hide(value, root) {
- if (!root) return true;
- if (
+ return (
root[
TalerFormAttributes.VQF_902_5.BIZREL_ORIGIN_CATEGORY.id
- ] === "OTHER"
- )
- return false;
- return true;
+ ] !== "OTHER"
+ );
},
},
{
diff --git a/packages/web-util/src/hooks/useForm.ts b/packages/web-util/src/hooks/useForm.ts
@@ -108,7 +108,7 @@ export function useForm<T>(
design,
formValue,
formUpdateHandler,
- i18n
+ i18n,
);
const status = {
@@ -151,6 +151,7 @@ export function getValueFromPath<T>(
/**
* Use $path to set the value $value into $object
* Don't modify $object, returns a new value
+ * returns undefined if the object is empty
*/
function setValueIntoPath(object: any, path: string[], value: any): any {
if (path.length === 0) return value;
@@ -178,7 +179,11 @@ export function undefinedIfEmpty<T extends object | undefined>(
: undefined;
}
-function checkFormFieldIsValid(formElement: UIFormElementConfig, currentValue: string | undefined, i18n: InternationalizationAPI): ErrorAndLabel | undefined {
+function checkFormFieldIsValid(
+ formElement: UIFormElementConfig,
+ currentValue: string | undefined,
+ i18n: InternationalizationAPI,
+): ErrorAndLabel | undefined {
if (!("id" in formElement)) {
return undefined;
}
@@ -201,7 +206,7 @@ function checkFormFieldIsValid(formElement: UIFormElementConfig, currentValue: s
console.error(e);
const message = i18n.str`Validation function failed. Contact developers ${String(
e,
- )}`
+ )}`;
console.log(message);
return {
label: formElement.label as TranslatedString,
@@ -209,7 +214,7 @@ function checkFormFieldIsValid(formElement: UIFormElementConfig, currentValue: s
};
}
}
- return undefined
+ return undefined;
}
function constructFormHandler<T>(
@@ -247,14 +252,16 @@ function constructFormHandler<T>(
formElement.hidden === true ||
(formElement.hide && formElement.hide(currentValue, result) === true);
- const currentError: ErrorAndLabel | undefined = !hidden ? checkFormFieldIsValid(formElement, currentValue, i18n) : undefined;
+ const currentError: ErrorAndLabel | undefined = !hidden
+ ? checkFormFieldIsValid(formElement, currentValue, i18n)
+ : undefined;
if (currentError !== undefined) {
errors = setValueIntoPath(errors, path, currentError);
}
function updater(newValue: unknown) {
- const updated = setValueIntoPath(formValue, path, newValue);
+ const updated = setValueIntoPath(formValue, path, newValue) ?? {};
onValueChange(updated);
}
@@ -264,12 +271,12 @@ function constructFormHandler<T>(
onChange: updater,
parentRef: result,
};
-
+
// FIXME: handler should not be set but we also need to refactor
// ui components.
handler = setValueIntoPath(handler, path, field);
if (!hidden) {
- result = setValueIntoPath(result, path, field.value);
+ result = setValueIntoPath(result, path, field.value) ?? {};
}
}