strapi icon indicating copy to clipboard operation
strapi copied to clipboard

Custom field type: 'array' is not a valid Strapi type or it can't be used with a Custom Field

Open Thisisjuke opened this issue 3 years ago β€’ 3 comments

Bug report

Required System information

  • Node.js version: v18.12.1
  • NPM version: 8.19.2
  • Strapi version: 4.5.3
  • Database: postgresql
  • Operating system: Ubuntu 22.04.1 LTS

Describe the bug

During plugin development, I can't register my custom field as an array.

Steps to reproduce the behavior

  1. Go to plugins/my-plugin/server/register.ts
  2. Add:
export default ({ strapi }: { strapi: Strapi }) => {
  strapi.customFields.register({
    name: 'myArray',
    plugin: 'my-plugin',
    type: 'array',
  });
};
  1. Wait for a restart.
  2. See error:
Custom field type: 'array' is not a valid Strapi type or it can't be used with a Custom Field

Expected behavior

I can use an array as type. It's available here: https://docs.strapi.io/developer-docs/latest/development/backend-customization/models.html#model-attributes

Array type should create the related type in the database.

Additional context

Workaround: use a string type + JSON.stringify() & JSON.parse().

It's not a practical way of doing it and storing it.

Thisisjuke avatar Dec 07 '22 19:12 Thisisjuke

@Thisisjuke How do you resolve this problem? Input field shows "Not support"?

CategroyIds is a PostgreSQL column, which type is integer array.

Screenshot 2022-12-17 at 9 56 43 PM

smoothdvd avatar Dec 17 '22 13:12 smoothdvd

Hi @smoothdvd, your issue seems not related to my issue. But I once encountered your problem !

EDIT: You will surely have the same issue as me after you fix the "not supported" issue :slightly_frowning_face: If you find a way to not use the workaround, let me know please :pray: Workaround: use a string type + JSON.stringify() & JSON.parse().

Mine is an "issue" relative to this file. array is not isnide @strapi/strapi/lib/core/registries/custom-fields.js

const ALLOWED_TYPES = [
  'biginteger',
  'boolean',
  'date',
  'datetime',
  'decimal',
  'email',
  'enumeration',
  'float',
  'integer',
  'json',
  'password',
  'richtext',
  'string',
  'text',
  'time',
  'uid',
];
//...
get(customField) {
	const registeredCustomField = customFields[customField];
		if (!registeredCustomField) {
			throw new Error(`Could not find Custom Field: ${customField}`);
		}

	return registeredCustomField;
},

About your issue, if I remember well, it was a bad string formatting inside my content-type/page-schema.json. You have to use it like this:

    "products": {
      "type": "customField",
      "customField": "plugin::strapi-plugin-products-manager.products"
    },
  • plugin: should be the string plugin, don't change it.
  • strapi-plugin-products-manager: your plugin name, has to be registered using the same name.
  • products: should be the string used inside your app.customFields.register

I registered mine like this:

const name = pluginPkg.strapi.name;

export default {
  register(app) {
    app.customFields.register({
      name: "products", // Use this string to register it in your schema.json !
      pluginId: pluginId,
      type: "string", // My issue is that i can't use an array here
      icon: ProductIcon,
      components: {
        Input: async () => import("./components/ProductSelector"),
      },
    }),
    app.createSettingSection();
    app.addMenuLink({/* ... */});
    const plugin = {
      id: pluginId,
      initializer: Initializer,
      isReady: false,
      name,
    };

    app.registerPlugin(plugin);
  },

  bootstrap(app) {},
  async registerTrads(app) {/* ... */},
};

Hope this helps ! :v:

Thisisjuke avatar Dec 17 '22 15:12 Thisisjuke

I patched the @strapi/database package with add type 'raw', then I can get raw value returned by pg.

diff --git a/node_modules/@strapi/database/lib/fields/index.js b/node_modules/@strapi/database/lib/fields/index.js
index 897fa91..8bdc9d6 100644
--- a/node_modules/@strapi/database/lib/fields/index.js
+++ b/node_modules/@strapi/database/lib/fields/index.js
@@ -32,6 +32,7 @@ const typeToFieldMap = {
   datetime: DatetimeField,
   timestamp: TimestampField,
   boolean: BooleanField,
+  raw: Field,
 };
 
 const createField = (attribute) => {
 
diff --git a/node_modules/@strapi/database/lib/schema/schema.js b/node_modules/@strapi/database/lib/schema/schema.js
index 643d7b7..871211c 100644
--- a/node_modules/@strapi/database/lib/schema/schema.js
+++ b/node_modules/@strapi/database/lib/schema/schema.js
@@ -176,6 +176,9 @@ const getColumnType = (attribute) => {
     case 'boolean': {
       return { type: 'boolean' };
     }
+    case 'raw': {
+      return { type: 'raw' };
+    }
     default: {
       throw new Error(`Unknown type ${attribute.type}`);
     }
diff --git a/node_modules/@strapi/database/lib/types/index.js b/node_modules/@strapi/database/lib/types/index.js
index 9d710a7..98a52e6 100644
--- a/node_modules/@strapi/database/lib/types/index.js
+++ b/node_modules/@strapi/database/lib/types/index.js
@@ -19,6 +19,7 @@ const SCALAR_TYPES = [
   'datetime',
   'timestamp',
   'boolean',
+  'raw',
 ];
 
 const STRING_TYPES = ['string', 'text', 'uid', 'email', 'enumeration', 'richtext'];

Ref by https://medium.com/@dallasclark/how-to-create-a-custom-admin-ui-field-component-in-strapi-2c9cd367f262, the limit of ALLOWED_TYPES is resolved.

smoothdvd avatar Dec 17 '22 16:12 smoothdvd

closing as this isn't a bug but instead is a strapi feature in itself: https://feedback.strapi.io/developer-experience/p/allow-array-or-multi-select-field-type

derrickmehaffy avatar Feb 21 '23 14:02 derrickmehaffy

@derrickmehaffy I know it's closed, I will stay with this workaround and store strings 😐 But in strapi v4 official documentation, it says that array is an official and supported type: Screenshot_2023-02-21-15-19-06-12_be80aec1db9a2b53c9d399db0c602181.jpg Strapi documentation : content-types

Thisisjuke avatar Feb 21 '23 14:02 Thisisjuke

@derrickmehaffy if it's not a bug and then "not a feature", then it should be removed from the documentation where it is clearly misleading.

Also, this has been flagged as a bug by people from Strapi in this thread, why is there such a sudden change for an actual needed feature ?

theosenoussaoui avatar Feb 21 '23 14:02 theosenoussaoui

@derrickmehaffy I know it's closed, I will stay with this workaround and store strings 😐 But in strapi v4 official documentation, it says that array is an official and supported type: Screenshot_2023-02-21-15-19-06-12_be80aec1db9a2b53c9d399db0c602181.jpg Strapi documentation : content-types

Ah yeah that needs to be removed, we have never had an actual array data type

derrickmehaffy avatar Feb 21 '23 14:02 derrickmehaffy

@derrickmehaffy if it's not a bug and then "not a feature", then it should be removed from the documentation where it is clearly misleading.

Also, this has been flagged as a bug by people from Strapi in this thread, why is there such a sudden change for an actual needed feature ?

Old issue cleanup, trying to narrow down the issues to actual bugs and not enhancements or feature requests

derrickmehaffy avatar Feb 21 '23 14:02 derrickmehaffy

Removed from the docs in our docs-next which is due to be released tomorrow I think: https://docs-next.strapi.io/dev-docs/backend-customization/models#model-attributes

derrickmehaffy avatar Feb 21 '23 14:02 derrickmehaffy