Skip to content

Commit

Permalink
improve description comment
Browse files Browse the repository at this point in the history
  • Loading branch information
maximpn committed Dec 5, 2024
1 parent 3daf405 commit fa0b2ba
Showing 1 changed file with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,41 @@ interface UseFormWithWarnReturn<T extends FormData = FormData, I extends FormDat
}

/**
* Form lib implements warning functionality via non blocking validators. `validations` allows to
* specify validation configuration with validator functions and extra parameters including
* `isBlocking`. Validators marked as `isBlocking` will produce non blocking validation errors
* a.k.a. warnings.
*
* The problem with the supported approach is lack of flexibility and necessary API like one for getting
* only blocking or non blocking errors. Flexibility requirement comes from complex async validators
* producing blocking and non blocking validation errors. There is no way to use `isBlocking` configuration
* option to separate errors. Separating such validating functions in two would lead to sending two
* HTTP requests and performing another async operations twice.
*
* On top of just having an ability to mark validation errors as non blocking via `isBlocking: false`
* configuration we require a way to return blocking and non blocking errors from a single validation
* function. It'd be possible by returning an error with `isBlocking` (or `isWarning`) flag along with
* `message` and `code` fields from a validator function. Attempts to reuse `__isBlocking__` internal
* field lead to inconsistent behavior.
*
* `useFormWithWarn` implements warnings (non blocking errors) on top of `FormHook` using validation
* error codes as a flexible way to determine whether an error is a blocking error or it's a warning.
* It provides little interface extension to simplify errors and warnings consumption
*
* In some cases business logic requires implementing functionality to allow users perform an action
* despite non-critical validation errors a.k.a. warnings. Usually it's also required to inform users
* about warnings they got before proceeding for example via a modal.
*
* Since `FormHook` returned by `useForm` lacks of such functionality. `useFormWithWarn` is here to
* provide warnings functionality. Besides that it it's replaceable with `useForm`.
* Since `FormHook` returned by `useForm` lacks of such functionality `useFormWithWarn` is here to
* provide warnings functionality. It could be used and passed as `FormHook` when warnings functionality
* isn't required making absolutely transparent.
*
* **Important:** Validators use short circuiting by default. It means that any failed validation in
* `validations` configuration array will prevent the rest validators from running. When used with warnings
* it may lead to bugs when validator checks first for warnings. You have to make sure a value is validated
* for errors first and then for warnings.
*
* There is a ticket to move this functionality to Form lib https://github.com/elastic/kibana/issues/203097.
*/
export function useFormWithWarn<T extends FormData = FormData, I extends FormData = T>(
formConfig: FormWithWarnConfig<T, I>
Expand Down

0 comments on commit fa0b2ba

Please sign in to comment.