PromucFlow_constructor/app/client/src/utils/Validators.ts

180 lines
4.7 KiB
TypeScript
Raw Normal View History

2019-11-19 12:44:58 +00:00
import _ from "lodash";
import {
VALIDATION_TYPES,
2019-11-22 13:12:39 +00:00
ValidationResponse,
2019-11-19 12:44:58 +00:00
ValidationType,
Validator,
2019-11-25 05:07:27 +00:00
} from "constants/WidgetValidation";
2019-11-22 13:12:39 +00:00
import moment from "moment";
2019-12-10 13:30:16 +00:00
import { WIDGET_TYPE_VALIDATION_ERROR } from "constants/messages";
2019-11-19 12:44:58 +00:00
export const VALIDATORS: Record<ValidationType, Validator> = {
2019-11-22 13:12:39 +00:00
[VALIDATION_TYPES.TEXT]: (value: any): ValidationResponse => {
let parsed = value;
if (_.isUndefined(value)) {
2019-12-10 13:30:16 +00:00
return {
isValid: false,
parsed: "",
message: `${WIDGET_TYPE_VALIDATION_ERROR}: text`,
};
2019-11-22 13:12:39 +00:00
}
if (_.isObject(value)) {
return {
isValid: false,
parsed: JSON.stringify(value, null, 2),
message: `${WIDGET_TYPE_VALIDATION_ERROR}: text`,
};
}
2019-11-22 13:12:39 +00:00
let isValid = _.isString(value);
if (!isValid) {
try {
parsed = _.toString(value);
isValid = true;
} catch (e) {
console.error(`Error when parsing ${value} to string`);
console.error(e);
2019-12-10 13:30:16 +00:00
return {
isValid: false,
parsed: "",
message: `${WIDGET_TYPE_VALIDATION_ERROR}: text`,
};
2019-11-22 13:12:39 +00:00
}
}
return { isValid, parsed };
},
[VALIDATION_TYPES.NUMBER]: (value: any): ValidationResponse => {
let parsed = value;
if (_.isUndefined(value)) {
2019-12-10 13:30:16 +00:00
return {
isValid: false,
parsed: 0,
message: `${WIDGET_TYPE_VALIDATION_ERROR}: number`,
};
2019-11-22 13:12:39 +00:00
}
let isValid = _.isNumber(value);
if (!isValid) {
try {
parsed = _.toNumber(value);
isValid = true;
} catch (e) {
console.error(`Error when parsing ${value} to number`);
console.error(e);
2019-12-10 13:30:16 +00:00
return {
isValid: false,
parsed: 0,
message: `${WIDGET_TYPE_VALIDATION_ERROR}: number`,
};
2019-11-22 13:12:39 +00:00
}
}
return { isValid, parsed };
},
[VALIDATION_TYPES.BOOLEAN]: (value: any): ValidationResponse => {
let parsed = value;
if (_.isUndefined(value)) {
2019-12-10 13:30:16 +00:00
return {
isValid: false,
parsed: false,
message: `${WIDGET_TYPE_VALIDATION_ERROR}: boolean`,
};
2019-11-22 13:12:39 +00:00
}
let isValid = _.isBoolean(value);
if (!isValid) {
try {
parsed = !!value;
isValid = true;
} catch (e) {
console.error(`Error when parsing ${value} to boolean`);
console.error(e);
2019-12-10 13:30:16 +00:00
return {
isValid: false,
parsed: false,
message: `${WIDGET_TYPE_VALIDATION_ERROR}: boolean`,
};
2019-11-22 13:12:39 +00:00
}
}
return { isValid, parsed };
},
[VALIDATION_TYPES.OBJECT]: (value: any): ValidationResponse => {
let parsed = value;
if (_.isUndefined(value)) {
2019-12-10 13:30:16 +00:00
return {
isValid: false,
parsed: {},
message: `${WIDGET_TYPE_VALIDATION_ERROR}: Object`,
};
2019-11-22 13:12:39 +00:00
}
let isValid = _.isObject(value);
if (!isValid) {
try {
parsed = JSON.parse(value);
isValid = true;
} catch (e) {
console.error(`Error when parsing ${value} to object`);
console.error(e);
2019-12-10 13:30:16 +00:00
return {
isValid: false,
parsed: {},
message: `${WIDGET_TYPE_VALIDATION_ERROR}: Object`,
};
2019-11-22 13:12:39 +00:00
}
}
return { isValid, parsed };
},
[VALIDATION_TYPES.ARRAY]: (value: any): ValidationResponse => {
let parsed = value;
2019-11-19 12:44:58 +00:00
try {
2019-11-22 13:12:39 +00:00
if (_.isUndefined(value)) {
2019-12-10 13:30:16 +00:00
return {
isValid: false,
parsed: [],
message: `${WIDGET_TYPE_VALIDATION_ERROR}: Array/List`,
};
2019-11-19 12:44:58 +00:00
}
2019-11-22 13:12:39 +00:00
if (_.isString(value)) {
parsed = JSON.parse(parsed as string);
}
if (!Array.isArray(parsed)) {
2019-12-10 13:30:16 +00:00
return {
isValid: false,
parsed: [],
message: `${WIDGET_TYPE_VALIDATION_ERROR}: Array/List`,
};
2019-11-22 13:12:39 +00:00
}
return { isValid: true, parsed };
} catch (e) {
console.error(e);
2019-12-10 13:30:16 +00:00
return {
isValid: false,
parsed: [],
message: `${WIDGET_TYPE_VALIDATION_ERROR}: Array/List`,
};
2019-11-19 12:44:58 +00:00
}
},
2019-11-22 13:12:39 +00:00
[VALIDATION_TYPES.TABLE_DATA]: (value: any): ValidationResponse => {
const { isValid, parsed } = VALIDATORS[VALIDATION_TYPES.ARRAY](value);
if (!isValid) {
2019-12-10 13:30:16 +00:00
return {
isValid,
parsed,
message: `${WIDGET_TYPE_VALIDATION_ERROR}: Table Data`,
};
2019-11-22 13:12:39 +00:00
} else if (!_.every(parsed, datum => _.isObject(datum))) {
2019-12-10 13:30:16 +00:00
return {
isValid: false,
parsed: [],
message: `${WIDGET_TYPE_VALIDATION_ERROR}: Table Data`,
};
2019-11-22 13:12:39 +00:00
}
return { isValid, parsed };
},
[VALIDATION_TYPES.DATE]: (value: any): ValidationResponse => {
const isValid = moment(value).isValid();
const parsed = isValid ? moment(value).toDate() : new Date();
2019-12-10 13:30:16 +00:00
return {
isValid,
parsed,
message: isValid ? "" : `${WIDGET_TYPE_VALIDATION_ERROR}: Date`,
};
2019-11-22 13:12:39 +00:00
},
2019-11-19 12:44:58 +00:00
};