@webbycrown/advanced-fields 1.0.7 → 1.0.9

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.
@@ -39,6 +39,25 @@ const customFields = [
39
39
  return null;
40
40
  }
41
41
  },
42
+ {
43
+ name: "color",
44
+ type: "string",
45
+ validate: (value, { required, options = {} }) => {
46
+ const { customErrorMessage = "" } = options;
47
+ const stringValue = value === void 0 || value === null ? "" : String(value).trim();
48
+ if (required && !stringValue) {
49
+ return customErrorMessage || "This field is required";
50
+ }
51
+ if (!stringValue) {
52
+ return null;
53
+ }
54
+ const hexRegex = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
55
+ if (!hexRegex.test(stringValue)) {
56
+ return customErrorMessage || "Invalid color (use hex like #RRGGBB)";
57
+ }
58
+ return null;
59
+ }
60
+ },
42
61
  {
43
62
  name: "checkbox",
44
63
  type: "json",
@@ -98,41 +117,50 @@ const customFields = [
98
117
  }
99
118
  return null;
100
119
  }
120
+ },
121
+ {
122
+ name: "enumeration",
123
+ type: "json",
124
+ validate: (value, { required, options = {} }) => {
125
+ const {
126
+ minChoices = 0,
127
+ maxChoices = 0,
128
+ customErrorMessage = ""
129
+ } = options;
130
+ if (required) {
131
+ if (!value || Array.isArray(value) && value.length === 0 || typeof value === "string" && value.trim() === "" || value === null || value === void 0) {
132
+ return customErrorMessage || "This field is required";
133
+ }
134
+ }
135
+ if (!value || Array.isArray(value) && value.length === 0) {
136
+ return null;
137
+ }
138
+ const values = Array.isArray(value) ? value : [];
139
+ if (minChoices > 0 && values.length < minChoices) {
140
+ return customErrorMessage || `Please select at least ${minChoices} option${minChoices > 1 ? "s" : ""}`;
141
+ }
142
+ if (maxChoices > 0 && values.length > maxChoices) {
143
+ return customErrorMessage || `Please select at most ${maxChoices} option${maxChoices > 1 ? "s" : ""}`;
144
+ }
145
+ return null;
146
+ }
101
147
  }
102
148
  ];
103
149
  var server = {
104
150
  register({ strapi }) {
105
- if (!strapi.customFields) {
106
- return;
107
- }
108
151
  customFields.forEach((field) => {
109
- try {
110
- const fieldConfig = {
111
- name: field.name,
112
- plugin: PLUGIN_ID,
113
- type: field.type
114
- };
115
- if (field.validate) {
116
- fieldConfig.validate = field.validate;
117
- }
118
- strapi.customFields.register(fieldConfig);
119
- } catch (error) {
152
+ const fieldConfig = {
153
+ name: field.name,
154
+ plugin: PLUGIN_ID,
155
+ type: field.type
156
+ };
157
+ if (field.validate) {
158
+ fieldConfig.validate = field.validate;
120
159
  }
160
+ strapi.customFields.register(fieldConfig);
121
161
  });
122
162
  },
123
163
  bootstrap({ strapi }) {
124
- setTimeout(() => {
125
- if (strapi.customFields && typeof strapi.customFields.get === "function") {
126
- customFields.forEach((field) => {
127
- try {
128
- const registeredField = strapi.customFields.get(
129
- `${PLUGIN_ID}.${field.name}`
130
- );
131
- } catch (error) {
132
- }
133
- });
134
- }
135
- }, 1e3);
136
164
  }
137
165
  };
138
166
  const index = /* @__PURE__ */ getDefaultExportFromCjs(server);
@@ -38,6 +38,25 @@ const customFields = [
38
38
  return null;
39
39
  }
40
40
  },
41
+ {
42
+ name: "color",
43
+ type: "string",
44
+ validate: (value, { required, options = {} }) => {
45
+ const { customErrorMessage = "" } = options;
46
+ const stringValue = value === void 0 || value === null ? "" : String(value).trim();
47
+ if (required && !stringValue) {
48
+ return customErrorMessage || "This field is required";
49
+ }
50
+ if (!stringValue) {
51
+ return null;
52
+ }
53
+ const hexRegex = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
54
+ if (!hexRegex.test(stringValue)) {
55
+ return customErrorMessage || "Invalid color (use hex like #RRGGBB)";
56
+ }
57
+ return null;
58
+ }
59
+ },
41
60
  {
42
61
  name: "checkbox",
43
62
  type: "json",
@@ -97,41 +116,50 @@ const customFields = [
97
116
  }
98
117
  return null;
99
118
  }
119
+ },
120
+ {
121
+ name: "enumeration",
122
+ type: "json",
123
+ validate: (value, { required, options = {} }) => {
124
+ const {
125
+ minChoices = 0,
126
+ maxChoices = 0,
127
+ customErrorMessage = ""
128
+ } = options;
129
+ if (required) {
130
+ if (!value || Array.isArray(value) && value.length === 0 || typeof value === "string" && value.trim() === "" || value === null || value === void 0) {
131
+ return customErrorMessage || "This field is required";
132
+ }
133
+ }
134
+ if (!value || Array.isArray(value) && value.length === 0) {
135
+ return null;
136
+ }
137
+ const values = Array.isArray(value) ? value : [];
138
+ if (minChoices > 0 && values.length < minChoices) {
139
+ return customErrorMessage || `Please select at least ${minChoices} option${minChoices > 1 ? "s" : ""}`;
140
+ }
141
+ if (maxChoices > 0 && values.length > maxChoices) {
142
+ return customErrorMessage || `Please select at most ${maxChoices} option${maxChoices > 1 ? "s" : ""}`;
143
+ }
144
+ return null;
145
+ }
100
146
  }
101
147
  ];
102
148
  var server = {
103
149
  register({ strapi }) {
104
- if (!strapi.customFields) {
105
- return;
106
- }
107
150
  customFields.forEach((field) => {
108
- try {
109
- const fieldConfig = {
110
- name: field.name,
111
- plugin: PLUGIN_ID,
112
- type: field.type
113
- };
114
- if (field.validate) {
115
- fieldConfig.validate = field.validate;
116
- }
117
- strapi.customFields.register(fieldConfig);
118
- } catch (error) {
151
+ const fieldConfig = {
152
+ name: field.name,
153
+ plugin: PLUGIN_ID,
154
+ type: field.type
155
+ };
156
+ if (field.validate) {
157
+ fieldConfig.validate = field.validate;
119
158
  }
159
+ strapi.customFields.register(fieldConfig);
120
160
  });
121
161
  },
122
162
  bootstrap({ strapi }) {
123
- setTimeout(() => {
124
- if (strapi.customFields && typeof strapi.customFields.get === "function") {
125
- customFields.forEach((field) => {
126
- try {
127
- const registeredField = strapi.customFields.get(
128
- `${PLUGIN_ID}.${field.name}`
129
- );
130
- } catch (error) {
131
- }
132
- });
133
- }
134
- }, 1e3);
135
163
  }
136
164
  };
137
165
  const index = /* @__PURE__ */ getDefaultExportFromCjs(server);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webbycrown/advanced-fields",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "keywords": [
5
5
  "strapi",
6
6
  "plugin",
@@ -76,7 +76,8 @@
76
76
  "email": "info@webbycrown.com",
77
77
  "url": "https://webbycrown.com"
78
78
  },
79
- "homepage": "https://github.com/webbycrown/strapi-advanced-fields",
79
+ "homepage": "https://www.webbycrown.com/guides/advanced-fields-for-strapi/implementation-guide",
80
+ "documentation": "https://www.webbycrown.com/guides/advanced-fields-for-strapi/implementation-guide",
80
81
  "repository": {
81
82
  "type": "git",
82
83
  "url": "https://github.com/webbycrown/strapi-advanced-fields.git"