drapcode-utility 1.4.2 → 1.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1,2 @@
1
1
  export declare const formatFieldsOfItem: (item: any, fields: any) => any;
2
+ export declare const getFormatFieldData: (fieldData: any, fieldType: string) => any;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.formatFieldsOfItem = void 0;
3
+ exports.getFormatFieldData = exports.formatFieldsOfItem = void 0;
4
4
  var drapcode_constant_1 = require("drapcode-constant");
5
5
  var formatFieldsOfItem = function (item, fields) {
6
6
  if (!item)
@@ -20,7 +20,7 @@ var formatField = function (itemData, fields) {
20
20
  Object.keys(itemData).forEach(function (key) {
21
21
  var field = fields.find(function (field) { return field.fieldName === key; });
22
22
  newItemData[key] = field
23
- ? getFormatFieldData(itemData[key], field.type)
23
+ ? exports.getFormatFieldData(itemData[key], field.type)
24
24
  : itemData[key];
25
25
  });
26
26
  return newItemData;
@@ -80,3 +80,4 @@ var getFormatFieldData = function (fieldData, fieldType) {
80
80
  }
81
81
  return fieldData;
82
82
  };
83
+ exports.getFormatFieldData = getFormatFieldData;
@@ -88,20 +88,27 @@ var timezoneDateParse = function (value, nextDay, prevDay) {
88
88
  exports.timezoneDateParse = timezoneDateParse;
89
89
  var formatItemDates = function (item, fields, dateFormat, reverse) {
90
90
  if (item && typeof item === "object") {
91
+ var dateTimeFormat_1 = "YYYY-MM-DD";
91
92
  var dateFormat1_1 = reverse ? dateFormat : "YYYY-MM-DD";
92
93
  var dateFormat2_1 = reverse ? "YYYY-MM-DD" : dateFormat;
93
94
  Object.keys(item).forEach(function (fieldName) {
94
95
  var _a;
95
96
  var field = fields.find(function (field) { return field.fieldName === fieldName; });
96
97
  if (field && (field === null || field === void 0 ? void 0 : field.type) === "date") {
97
- if (moment(item[fieldName], dateFormat1_1).isValid()) {
98
- item[fieldName] =
99
- ((_a = field === null || field === void 0 ? void 0 : field.extraFieldSetting) === null || _a === void 0 ? void 0 : _a.dateDisplayType) === "datetime-local"
100
- ? item[fieldName]
101
- : moment(item[fieldName], dateFormat1_1).format(dateFormat2_1);
98
+ if (((_a = field === null || field === void 0 ? void 0 : field.extraFieldSetting) === null || _a === void 0 ? void 0 : _a.dateDisplayType) === "datetime-local") {
99
+ if (moment(item[fieldName], dateTimeFormat_1).isValid()) {
100
+ item[fieldName] = item[fieldName];
101
+ }
102
+ else
103
+ item[fieldName] = "";
104
+ }
105
+ else {
106
+ if (moment(item[fieldName], dateFormat1_1).isValid()) {
107
+ item[fieldName] = moment(item[fieldName], dateFormat1_1).format(dateFormat2_1);
108
+ }
109
+ else
110
+ item[fieldName] = "";
102
111
  }
103
- else
104
- item[fieldName] = "";
105
112
  }
106
113
  });
107
114
  }
@@ -28,3 +28,9 @@ export declare const replaceTransferObjectValueIntoExpression: (expression: stri
28
28
  export declare const removeMongoDbId: (result: any[]) => any[];
29
29
  export declare const processFieldsInlcude: (fieldsInclude: any) => any;
30
30
  export declare const replaceValueFromSource: (variableName: any, environment: any, tenant: any) => any;
31
+ export declare const fillDefaultValues: (collection: any, itemdata: any) => any;
32
+ export declare const validateData: (fields: any, data: any) => {
33
+ isValid: boolean;
34
+ errors: any;
35
+ };
36
+ export declare const validateUrl: (url: any) => boolean;
@@ -30,8 +30,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
30
30
  return (mod && mod.__esModule) ? mod : { "default": mod };
31
31
  };
32
32
  Object.defineProperty(exports, "__esModule", { value: true });
33
- exports.replaceValueFromSource = exports.processFieldsInlcude = exports.removeMongoDbId = exports.replaceTransferObjectValueIntoExpression = exports.unflattenObject = exports.parseValueFromData = exports.parseJsonString = exports.replaceDataValueIntoExpression = exports.formatCustomCSSClasses = exports.validateAlphanumericString = exports.convertItemToArray = exports.arraysEqual = exports.checkAndCompareValue = exports.restructureData = exports.formatCollectionAndFieldName = exports.cleanCollectionAndFieldName = exports.stringToExpression = exports.expressionToString = exports.getSpecialCharectorReplacedExpression = exports.validateUuidString = exports.validateEmail = exports.snakeCaseToTitleCase = exports.dynamicSort = exports.checkValidArray = exports.isObject = exports.isEmptyObject = exports.isEmpty = exports.clearSpaceAndReformat = exports.camelize = exports.validateString = void 0;
33
+ exports.validateUrl = exports.validateData = exports.fillDefaultValues = exports.replaceValueFromSource = exports.processFieldsInlcude = exports.removeMongoDbId = exports.replaceTransferObjectValueIntoExpression = exports.unflattenObject = exports.parseValueFromData = exports.parseJsonString = exports.replaceDataValueIntoExpression = exports.formatCustomCSSClasses = exports.validateAlphanumericString = exports.convertItemToArray = exports.arraysEqual = exports.checkAndCompareValue = exports.restructureData = exports.formatCollectionAndFieldName = exports.cleanCollectionAndFieldName = exports.stringToExpression = exports.expressionToString = exports.getSpecialCharectorReplacedExpression = exports.validateUuidString = exports.validateEmail = exports.snakeCaseToTitleCase = exports.dynamicSort = exports.checkValidArray = exports.isObject = exports.isEmptyObject = exports.isEmpty = exports.clearSpaceAndReformat = exports.camelize = exports.validateString = void 0;
34
+ var drapcode_constant_1 = require("drapcode-constant");
34
35
  var lodash_1 = __importDefault(require("lodash"));
36
+ var format_fields_1 = require("../format-fields");
37
+ var uuid_1 = require("uuid");
35
38
  // TODO: Refactor
36
39
  var derivedFieldExcludes = [
37
40
  "formatType",
@@ -256,7 +259,7 @@ var convertItemToArray = function (itemValue) {
256
259
  if (Array.isArray(itemValue)) {
257
260
  return itemValue;
258
261
  }
259
- return [itemValue];
262
+ return itemValue ? [itemValue] : [];
260
263
  };
261
264
  exports.convertItemToArray = convertItemToArray;
262
265
  var validateAlphanumericString = function (str) {
@@ -548,3 +551,176 @@ var replaceValueFromTenant = function (variableName, tenant) {
548
551
  variableName = variableName.replace(tenantFixKey, "");
549
552
  return lodash_1.default.get(tenant, variableName);
550
553
  };
554
+ var fillDefaultValues = function (collection, itemdata) {
555
+ var filledItemdata = __assign({}, itemdata);
556
+ var fieldsToSkip = [
557
+ drapcode_constant_1.FieldTypes.isDeleted.id,
558
+ drapcode_constant_1.FieldTypes.uuid.id,
559
+ drapcode_constant_1.FieldTypes.createdBy.id,
560
+ drapcode_constant_1.FieldTypes.updatedAt.id,
561
+ drapcode_constant_1.FieldTypes.createdAt.id,
562
+ ];
563
+ var fieldTypesToSkip = [drapcode_constant_1.FieldTypes.boolean.id];
564
+ var allFields = collection.fields.filter(function (field) {
565
+ return !fieldsToSkip.includes(field.fieldName) &&
566
+ !fieldTypesToSkip.includes(field.type);
567
+ });
568
+ allFields.forEach(function (field) {
569
+ var fieldName = field.fieldName;
570
+ if (!(fieldName in filledItemdata) ||
571
+ filledItemdata[fieldName] === "" ||
572
+ filledItemdata[fieldName] === 0 ||
573
+ filledItemdata[fieldName] === false) {
574
+ var formattedField = JSON.parse(JSON.stringify(field));
575
+ if (field.extraFieldSetting &&
576
+ "defaultValue" in formattedField.extraFieldSetting) {
577
+ filledItemdata[fieldName] = assignFieldValue(formattedField, field, fieldName, filledItemdata);
578
+ }
579
+ }
580
+ });
581
+ return filledItemdata;
582
+ };
583
+ exports.fillDefaultValues = fillDefaultValues;
584
+ var assignFieldValue = function (formattedField, field, fieldName, filledItemdata) {
585
+ var value = "";
586
+ value = formattedField.extraFieldSetting
587
+ ? formattedField.extraFieldSetting.defaultValue
588
+ : filledItemdata[fieldName];
589
+ value = format_fields_1.getFormatFieldData(value, field.type);
590
+ return value;
591
+ };
592
+ var validateData = function (fields, data) {
593
+ var errors = [];
594
+ fields
595
+ .filter(function (field) { return field.encrypted === false; })
596
+ .forEach(function (field) {
597
+ var fieldValue = data[field.fieldName];
598
+ if (fieldValue === null ||
599
+ fieldValue === undefined ||
600
+ typeof fieldValue === "undefined" ||
601
+ fieldValue.length <= 0) {
602
+ return;
603
+ }
604
+ var fieldTitle = field.fieldTitle.en;
605
+ if (field.type === "file" && typeof fieldValue === "string") {
606
+ try {
607
+ fieldValue = JSON.parse(fieldValue);
608
+ }
609
+ catch (error) {
610
+ errors.push("Error parsing " + fieldTitle + ": " + error.message);
611
+ return;
612
+ }
613
+ }
614
+ switch (field.type) {
615
+ case "text":
616
+ if (typeof fieldValue !== "string" &&
617
+ typeof fieldValue !== "number" &&
618
+ Object.prototype.toString.call(fieldValue) !== "[object Date]") {
619
+ errors.push(fieldTitle + " must be a string");
620
+ }
621
+ break;
622
+ case "large_text":
623
+ case "color":
624
+ if (typeof fieldValue !== "string") {
625
+ errors.push(fieldTitle + " must be a string");
626
+ }
627
+ break;
628
+ case "uuid":
629
+ if (typeof fieldValue !== "string" || !uuid_1.validate(fieldValue)) {
630
+ errors.push(fieldTitle + " must be a valid UUID string");
631
+ }
632
+ break;
633
+ case "reference":
634
+ case "belongsTo":
635
+ if (!Array.isArray(fieldValue) ||
636
+ fieldValue.some(function (item) { return typeof item !== "string"; })) {
637
+ errors.push(fieldTitle + " must be an array of valid UUID strings");
638
+ }
639
+ break;
640
+ case "createdAt":
641
+ case "updatedAt":
642
+ case "date":
643
+ if (fieldValue && isNaN(Date.parse(fieldValue))) {
644
+ errors.push(fieldTitle + " must be a valid date");
645
+ }
646
+ break;
647
+ case "boolean":
648
+ if (typeof fieldValue !== "boolean") {
649
+ errors.push(fieldTitle + " must be a boolean");
650
+ }
651
+ break;
652
+ case "email":
653
+ if (typeof fieldValue !== "string" ||
654
+ !/\S+@\S+\.\S+/.test(fieldValue)) {
655
+ errors.push(fieldTitle + " must be a valid email address");
656
+ break;
657
+ }
658
+ break;
659
+ case "tel":
660
+ var telPattern = /^\+?(\d{1,3})?[-\s]?(\(\d{1,4}\)|\d{1,4})[-\s]?(\d{1,4}[-\s]?\d{1,4}|\d{7,10})$/;
661
+ if (typeof fieldValue !== "string" || !telPattern.test(fieldValue)) {
662
+ errors.push(fieldTitle + " must be a valid phone number");
663
+ }
664
+ break;
665
+ case "static_option":
666
+ case "dynamic_option":
667
+ if (typeof fieldValue === "string") {
668
+ break;
669
+ }
670
+ else if (Array.isArray(fieldValue)) {
671
+ if (fieldValue.some(function (item) { return typeof item !== "string"; })) {
672
+ errors.push(fieldTitle + " must be an array of strings");
673
+ }
674
+ }
675
+ else {
676
+ errors.push(fieldTitle + " must be a string or an array of strings");
677
+ }
678
+ break;
679
+ case "url":
680
+ if (typeof fieldValue !== "string" || !exports.validateUrl(fieldValue)) {
681
+ errors.push(fieldTitle + " must be a valid URL");
682
+ }
683
+ break;
684
+ case "file":
685
+ if (Array.isArray(fieldValue)) {
686
+ if (fieldValue.some(function (item) {
687
+ return typeof item !== "object" ||
688
+ !item.key ||
689
+ typeof item.key !== "string";
690
+ })) {
691
+ errors.push(fieldTitle + " must be an array of objects with 'key' property as string");
692
+ }
693
+ }
694
+ else if (typeof fieldValue !== "object" ||
695
+ !fieldValue.key ||
696
+ typeof fieldValue.key !== "string") {
697
+ errors.push(fieldTitle + " must be an object with 'key' property as string");
698
+ }
699
+ break;
700
+ case "number":
701
+ if (isNaN(Number(fieldValue))) {
702
+ errors.push(fieldTitle + " must be a valid number.");
703
+ }
704
+ break;
705
+ default:
706
+ break;
707
+ }
708
+ });
709
+ if (errors.length > 0) {
710
+ return { isValid: false, errors: errors };
711
+ }
712
+ else {
713
+ return { isValid: true, errors: [] };
714
+ }
715
+ };
716
+ exports.validateData = validateData;
717
+ var validateUrl = function (url) {
718
+ var pattern = new RegExp("^((ft|htt)ps?:\\/\\/)?" +
719
+ "((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" +
720
+ "((\\d{1,3}\\.){3}\\d{1,3}))" +
721
+ "(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" +
722
+ "(\\?[;&a-z\\d%_.~+=-]*)?" +
723
+ "(\\#[-a-z\\d_]*)?$", "i");
724
+ return !!pattern.test(url);
725
+ };
726
+ exports.validateUrl = validateUrl;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drapcode-utility",
3
- "version": "1.4.2",
3
+ "version": "1.4.4",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -33,8 +33,8 @@
33
33
  "@types/express": "^4.17.7",
34
34
  "@types/voca": "^1.4.2",
35
35
  "axios": "^1.1.2",
36
- "drapcode-constant": "^1.3.8",
37
- "drapcode-logger": "^1.1.2",
36
+ "drapcode-constant": "^1.3.9",
37
+ "drapcode-logger": "^1.1.3",
38
38
  "drapcode-redis": "^1.0.5",
39
39
  "express": "^4.17.1",
40
40
  "lodash": "^4.17.21",