PromucFlow_constructor/app/client/src/widgets/RangeSliderWidget/validations.ts
ChandanBalajiBP b72dea33f3
feat: Error handling phase 1 (#20629)
## Description
This PR updates the error logs 
- Establishing a consistent format for all error messages.
- Revising error titles and details for improved understanding.
- Compiling internal documentation of all error categories,
subcategories, and error descriptions.

Updated Error Interface:
https://www.notion.so/appsmith/Error-Interface-for-Plugin-Execution-Error-7b3f5323ba4c40bfad281ae717ccf79b

PRD:
https://www.notion.so/appsmith/PRD-Error-Handling-Framework-4ac9747057fd4105a9d52cb8b42f4452?pvs=4#008e9c79ff3c484abf0250a5416cf052

>TL;DR 

Fixes # 


Media
> A video or a GIF is preferred. when using Loom, don’t embed because it
looks like it’s a GIF. instead, just link to the video


## Type of change

- New feature (non-breaking change which adds functionality)


## How Has This Been Tested?

- Manual
- Jest
- Cypress

### Test Plan


### Issues raised during DP testing


## Checklist:
### Dev activity
- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] 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
- [x] PR is being merged under a feature flag


### QA activity:
- [ ] Test plan has been approved by relevant developers
- [ ] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test

---------

Co-authored-by: subrata <subrata@appsmith.com>
2023-02-18 18:25:46 +05:30

447 lines
8.1 KiB
TypeScript

import { RangeSliderWidgetProps } from "./widget";
export function minValueValidation(
min: unknown,
props: RangeSliderWidgetProps,
_: any,
) {
if (_.isNil(min) || min === "") {
return {
isValid: false,
parsed: undefined,
messages: [
{
name: "ValidationError",
message: "This value is required",
},
],
};
}
const minValue = Number(min);
const maxValue = props.max;
if (!Number.isFinite(minValue)) {
return {
isValid: false,
parsed: undefined,
messages: [
{
name: "TypeError",
message: "This value must be a number",
},
],
};
}
if (!_.isNil(maxValue) && minValue >= maxValue) {
return {
isValid: false,
parsed: undefined,
messages: [
{
name: "RangeError",
message: "This value must be less than max value",
},
],
};
}
return {
isValid: true,
parsed: minValue,
messages: [
{
name: "",
message: "",
},
],
};
}
export function maxValueValidation(
max: unknown,
props: RangeSliderWidgetProps,
_: any,
) {
if (_.isNil(max) || max === "") {
return {
isValid: false,
parsed: undefined,
messages: [
{
name: "ValidationError",
message: "This value is required",
},
],
};
}
const maxValue = Number(max);
const minValue = props.min;
if (!Number.isFinite(maxValue)) {
return {
isValid: false,
parsed: undefined,
messages: [
{
name: "TypeError",
message: "This value must be a number",
},
],
};
}
if (!_.isNil(minValue) && maxValue <= minValue) {
return {
isValid: false,
parsed: undefined,
messages: [
{
name: "RangeError",
message: "This value must be greater than min value",
},
],
};
}
return {
isValid: true,
parsed: maxValue,
messages: [
{
name: "",
message: "",
},
],
};
}
export function stepSizeValidation(
step: unknown,
props: RangeSliderWidgetProps,
_: any,
) {
if (_.isNil(step) || step === "") {
return {
isValid: false,
parsed: undefined,
messages: [
{
name: "ValidationError",
message: "This value is required",
},
],
};
}
const stepValue = Number(step);
if (!Number.isFinite(stepValue)) {
return {
isValid: false,
parsed: undefined,
messages: [
{
name: "TypeError",
message: "This value must be a number",
},
],
};
}
if (stepValue < 0.1) {
return {
isValid: false,
parsed: undefined,
messages: [
{
name: "RangeError",
message: "This value must be greater than 0.1",
},
],
};
}
const minValue = props.min;
const maxValue = props.max;
const sliderRange = maxValue - minValue;
if (stepValue > sliderRange) {
return {
isValid: false,
parsed: undefined,
messages: [
{
name: "RangeError",
message: `This value must be less than ${sliderRange}`,
},
],
};
}
const minRange = props.minRange;
if (stepValue > minRange) {
return {
isValid: false,
parsed: undefined,
messages: [
{
name: "RangeError",
message: `This value must be less than or equal to minRange`,
},
],
};
}
return {
isValid: true,
parsed: stepValue,
messages: [
{
name: "",
message: "",
},
],
};
}
export function startValueValidation(
startValue: unknown,
props: RangeSliderWidgetProps,
_: any,
) {
if (_.isNil(startValue) || startValue === "") {
return {
isValid: true,
parsed: undefined,
messages: [
{
name: "",
message: "",
},
],
};
}
const defaultStartValue = Number(startValue);
const defaultEndValue = props.defaultEndValue;
const minValue = props.min;
if (!Number.isFinite(defaultStartValue)) {
return {
isValid: false,
parsed: undefined,
messages: [
{
name: "TypeError",
message: "This value must be a number",
},
],
};
}
if (!_.isNil(minValue) && defaultStartValue < minValue) {
return {
isValid: false,
parsed: undefined,
messages: [
{
name: "RangeError",
message: "This value must be greater than or equal to the min value",
},
],
};
}
if (defaultEndValue !== undefined && defaultStartValue >= defaultEndValue) {
return {
isValid: false,
parsed: undefined,
messages: [
{
name: "RangeError",
message: "This value must be less than defaultEnd value",
},
],
};
}
return {
isValid: true,
parsed: defaultStartValue,
messages: [
{
name: "",
message: "",
},
],
};
}
export function endValueValidation(
endValue: unknown,
props: RangeSliderWidgetProps,
_: any,
) {
if (_.isNil(endValue) || endValue === "") {
return {
isValid: true,
parsed: undefined,
messages: [
{
name: "",
message: "",
},
],
};
}
const defaultEndValue = Number(endValue);
const defaultStartValue = props.defaultStartValue;
const maxValue = props.max;
if (!Number.isFinite(defaultEndValue)) {
return {
isValid: false,
parsed: undefined,
messages: [
{
name: "TypeError",
message: "This value must be a number",
},
],
};
}
if (!_.isNil(maxValue) && defaultEndValue > maxValue) {
return {
isValid: false,
parsed: undefined,
messages: [
{
name: "RangeError",
message: "This value must be less than or equal to the max value",
},
],
};
}
if (defaultStartValue !== undefined && defaultEndValue <= defaultStartValue) {
return {
isValid: false,
parsed: undefined,
messages: [
{
name: "RangeError",
message: "This value must be greater than defaultStart value",
},
],
};
}
return {
isValid: true,
parsed: defaultEndValue,
messages: [
{
name: "",
message: "",
},
],
};
}
export function minRangeValidation(
minRange: unknown,
props: RangeSliderWidgetProps,
_: any,
) {
if (_.isNil(minRange) || minRange === "") {
return {
isValid: false,
parsed: undefined,
messages: [
{
name: "ValidationError",
message: "This value is required",
},
],
};
}
const defaultMinRange = Number(minRange);
const stepSize = props.step;
if (!Number.isFinite(defaultMinRange)) {
return {
isValid: false,
parsed: undefined,
messages: [
{
name: "TypeError",
message: "This value must be a number",
},
],
};
}
const minValue = props.min;
const maxValue = props.max;
const sliderRange = maxValue - minValue;
if (defaultMinRange > sliderRange) {
return {
isValid: false,
parsed: undefined,
messages: [
{
name: "RangeError",
message: `This value must be less than ${sliderRange}`,
},
],
};
}
if (defaultMinRange < 0.1) {
return {
isValid: false,
parsed: undefined,
messages: [
{
name: "RangeError",
message: "This value must be greater than 0.1",
},
],
};
}
if (defaultMinRange < stepSize) {
return {
isValid: false,
parsed: undefined,
messages: [
{
name: "RangeError",
message: "This value must be greater than or equal to step size",
},
],
};
}
return {
isValid: true,
parsed: defaultMinRange,
messages: [
{
name: "",
message: "",
},
],
};
}