PromucFlow_constructor/app/client/src/constants/PropertyControlConstants.tsx
Rishabh Rathod 8fe7f9291d
feat: Add validation dependency (#17138)
## Problem

**Why do we need validation dependency?**

- When on change of particular property value if other property needs revalidation then current evaluation architecture do not support such validation run.

https://www.notion.so/appsmith/Validation-Dependency-d7623a5625bd4aa187a3ae2372d3ac07

**Issues to resolve**

Fixes #15303
Fixes #17159
Fixes https://github.com/appsmithorg/appsmith/issues/16170

## Solution

**New Validation Flow**

validate a property after its evaluation and check if there are other properties that need to revalidate on a change of this value. 

- if yes run validation for those properties.

How does validation dependency get created?

- every widget’s property will keep static `dependencies` array in `validation` property like shown below.

```jsx
{
	validation: {
    type: ValidationTypes.FUNCTION,
    params: {
      fn: defaultOptionValueValidation,
      expected: {
        type: 'value1 or { "label": "label1", "value": "value1" }',
        example: `value1 | { "label": "label1", "value": "value1" }`,
        autocompleteDataType: AutocompleteDataType.STRING,
      },
      dependentPaths: ["serverSideFiltering", "options"],
    },
    dependencies: ["serverSideFiltering", "options"],
  }
}
```

This `dependentPaths` array will be used to form validationDependencyMap.
 
#### Changes

- The code editor component wasn't re-rendering for a few cases. Due to this updates were not shown until the component state was changed.

- In CreateFirstTree, After evaluateTree we run `validateTree`. In `validateTree`, we validate all the properties in `validationPaths`. Here, if the errors were resolved on revalidation, it didn't reset the validation errors stored in data tree before. Now, this PR adds the reset logic. 
- Created `validationDependencyMap` to re-validate properties on change of the property their validation depends upon. 
- After each property evaluates in `updateDataTree`, we check if re-validation is needed, if yes then we re-validate dependent properties.  


## Type of change

- Bug fix (non-breaking change which fixes an issue)
- New feature (non-breaking change which adds functionality)

## How Has This Been Tested?

- jest test

**Test Plan**
https://github.com/appsmithorg/TestSmith/issues/2078

## Checklist:

- [x] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my feature works
- [x] New and existing unit tests pass locally with my changes
2022-10-21 10:50:57 +05:30

140 lines
4.9 KiB
TypeScript

import { getPropertyControlTypes } from "components/propertyControls";
import {
ValidationResponse,
ValidationTypes,
} from "constants/WidgetValidation";
import { EvaluationSubstitutionType } from "entities/DataTree/dataTreeFactory";
import { CodeEditorExpected } from "components/editorComponents/CodeEditor";
import { UpdateWidgetPropertyPayload } from "actions/controlActions";
import { AppTheme } from "entities/AppTheming";
import { WidgetProps } from "widgets/BaseWidget";
const ControlTypes = getPropertyControlTypes();
export type ControlType = typeof ControlTypes[keyof typeof ControlTypes];
export type PropertyPaneSectionConfig = {
sectionName: string | ((props: WidgetProps, propertyPath: string) => string);
id?: string;
children: PropertyPaneConfig[];
collapsible?: boolean;
hidden?: (props: any, propertyPath: string) => boolean;
isDefaultOpen?: boolean;
propertySectionPath?: string;
};
export type PropertyHookUpdates = {
propertyPath: string;
propertyValue?: unknown;
isDynamicPropertyPath?: boolean; // Toggles the property mode to JS
shouldDeleteProperty?: boolean; // Deletes the property, propertyValue is ignored
};
export type PanelConfig = {
editableTitle: boolean;
titlePropertyName: string;
panelIdPropertyName: string;
children?: PropertyPaneConfig[];
contentChildren?: PropertyPaneConfig[];
styleChildren?: PropertyPaneConfig[];
updateHook: (
props: any,
propertyPath: string,
propertyValue: any,
) => Array<PropertyHookUpdates> | undefined;
};
export type PropertyPaneControlConfig = {
id?: string;
label: string | ((props: WidgetProps, propertyPath: string) => string);
propertyName: string;
// Serves in the tooltip
helpText?: string;
//Dynamic text serves below the property pane inputs
helperText?: ((props: any) => string) | string;
isJSConvertible?: boolean;
customJSControl?: string;
controlType: ControlType;
validationMessage?: string;
dataTreePath?: string;
children?: PropertyPaneConfig[];
panelConfig?: PanelConfig;
updateRelatedWidgetProperties?: (
propertyName: string,
propertyValue: any,
props: any,
) => UpdateWidgetPropertyPayload[];
updateHook?: (
props: any,
propertyName: string,
propertyValue: any,
) => Array<PropertyHookUpdates> | undefined;
hidden?: (props: any, propertyPath: string) => boolean;
invisible?: boolean;
isBindProperty: boolean;
isTriggerProperty: boolean;
validation?: ValidationConfig;
useValidationMessage?: boolean;
additionalAutoComplete?: (
props: any,
) => Record<string, Record<string, unknown>>;
evaluationSubstitutionType?: EvaluationSubstitutionType;
dependencies?: string[];
evaluatedDependencies?: string[]; // dependencies to be picked from the __evaluated__ object
expected?: CodeEditorExpected;
getStylesheetValue?: (
props: any,
propertyPath: string,
stylesheet?: AppTheme["stylesheet"][string],
) => AppTheme["stylesheet"][string][string];
// TODO(abhinav): To fix this, rename the options property of the controls which use this
// Alternatively, create a new structure
options?: any;
};
type ValidationConfigParams = {
min?: number; // min allowed for a number
max?: number; // max allowed for a number
natural?: boolean; // is a positive integer
default?: unknown; // default for any type
unique?: boolean | string[]; // unique in an array (string if a particular path is unique)
required?: boolean; // required type
// required is now used to check if value is an empty string.
requiredKey?: boolean; //required key
regex?: RegExp; // validator regex for text type
allowedKeys?: Array<{
// Allowed keys in an object type
name: string;
type: ValidationTypes;
params?: ValidationConfigParams;
}>;
allowedValues?: unknown[]; // Allowed values in a string and array type
children?: ValidationConfig; // Children configurations in an ARRAY or OBJECT_ARRAY type
fn?: (
value: unknown,
props: any,
_?: any,
moment?: any,
) => ValidationResponse; // Function in a FUNCTION type
fnString?: string; // AUTO GENERATED, SHOULD NOT BE SET BY WIDGET DEVELOPER
expected?: CodeEditorExpected; // FUNCTION type expected type and example
strict?: boolean; //for strict string validation of TEXT type
ignoreCase?: boolean; //to ignore the case of key
type?: ValidationTypes; // Used for ValidationType.TABLE_PROPERTY to define sub type
params?: ValidationConfigParams; // Used for ValidationType.TABLE_PROPERTY to define sub type params
limitLineBreaks?: boolean; // Used for ValidationType.TEXT to limit line breaks in a large json object.
};
export type ValidationConfig = {
type: ValidationTypes;
params?: ValidationConfigParams;
dependentPaths?: string[];
};
export type PropertyPaneConfig =
| PropertyPaneSectionConfig
| PropertyPaneControlConfig;
export interface ActionValidationConfigMap {
[configPropety: string]: ValidationConfig;
}