From 29af0db93d706e1e121afc86ab93cc3e7817fcfa Mon Sep 17 00:00:00 2001 From: Thenkei Date: Thu, 19 Sep 2024 15:19:55 +0200 Subject: [PATCH 1/4] fix(sequelize): model validation should handle correctly is validator --- .../model-to-collection-schema-converter.ts | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/datasource-sequelize/src/utils/model-to-collection-schema-converter.ts b/packages/datasource-sequelize/src/utils/model-to-collection-schema-converter.ts index 7c33753e8..18b2ae5e6 100644 --- a/packages/datasource-sequelize/src/utils/model-to-collection-schema-converter.ts +++ b/packages/datasource-sequelize/src/utils/model-to-collection-schema-converter.ts @@ -272,13 +272,34 @@ export default class ModelToCollectionSchemaConverter { } if (attribute.validate.is) { - const value = attribute.validate.is; + let value; - if (!Array.isArray(attribute.validate.is)) { + if ( + ( + attribute.validate.is as { + args: string | RegExp | readonly (string | RegExp)[]; + } + ).args + ) { + ({ args: value } = attribute.validate.is as { + args: string | RegExp | readonly (string | RegExp)[]; + }); + } else { + value = attribute.validate.is; + } + + if (!Array.isArray(value)) { validations.push({ operator: 'Like', value: value.toString(), }); + } else { + value.forEach(v => { + validations.push({ + operator: 'Like', + value: v.toString(), + }); + }); } } From bf25e1063f387a57cef67afbb1d5e0724109ec02 Mon Sep 17 00:00:00 2001 From: Thenkei Date: Thu, 19 Sep 2024 15:35:48 +0200 Subject: [PATCH 2/4] test: add missing tests cases --- .../model-to-collection-schema-converter.ts | 13 ++- ...del-to-collection-schema-converter.test.ts | 110 ++++++++++++++++++ 2 files changed, 117 insertions(+), 6 deletions(-) diff --git a/packages/datasource-sequelize/src/utils/model-to-collection-schema-converter.ts b/packages/datasource-sequelize/src/utils/model-to-collection-schema-converter.ts index 18b2ae5e6..26df437a7 100644 --- a/packages/datasource-sequelize/src/utils/model-to-collection-schema-converter.ts +++ b/packages/datasource-sequelize/src/utils/model-to-collection-schema-converter.ts @@ -294,12 +294,13 @@ export default class ModelToCollectionSchemaConverter { value: value.toString(), }); } else { - value.forEach(v => { - validations.push({ - operator: 'Like', - value: v.toString(), - }); - }); + // Not sure about this behavior + // value.forEach(v => { + // validations.push({ + // operator: 'Like', + // value: v.toString(), + // }); + // }); } } diff --git a/packages/datasource-sequelize/test/utils/model-to-collection-schema-converter.test.ts b/packages/datasource-sequelize/test/utils/model-to-collection-schema-converter.test.ts index a3f5f0039..208fef5cd 100644 --- a/packages/datasource-sequelize/test/utils/model-to-collection-schema-converter.test.ts +++ b/packages/datasource-sequelize/test/utils/model-to-collection-schema-converter.test.ts @@ -499,5 +499,115 @@ describe('Utils > ModelToCollectionSchemaConverter', () => { expect(ModelToCollectionSchemaConverter.convert(model, () => {})).toEqual(schema); }); + + describe('is validator', () => { + it('should work with args definition', () => { + const { sequelize } = setup(); + + const schema: CollectionSchema = { + actions: {}, + charts: [], + countable: true, + fields: expect.objectContaining({ + myPk: { + columnType: 'Number', + filterOperators: TypeConverter.operatorsForColumnType('Number'), + isPrimaryKey: true, + isSortable: true, + validation: [], + type: 'Column', + }, + myValue: { + columnType: 'String', + defaultValue: '__default__', + filterOperators: TypeConverter.operatorsForColumnType('String'), + isSortable: true, + validation: expect.arrayContaining([ + { + operator: 'Like', + value: '/^[a-z]+$/', + }, + ]), + type: 'Column', + }, + }), + searchable: false, + segments: [], + }; + + const model = sequelize.define( + '__model_a__', + { + myPk: { + type: DataTypes.INTEGER, + primaryKey: true, + }, + myValue: { + type: DataTypes.STRING, + defaultValue: '__default__', + validate: { + is: { + args: /^[a-z]+$/, + msg: 'MESSAGE_PROJECT_NAME_VALIDATION_ERROR_FORMAT', + }, + }, + }, + }, + { timestamps: true }, + ); + + expect(ModelToCollectionSchemaConverter.convert(model, () => {})).toEqual(schema); + }); + + it('should not handle array of regex', () => { + const { sequelize } = setup(); + + const schema: CollectionSchema = { + actions: {}, + charts: [], + countable: true, + fields: expect.objectContaining({ + myPk: { + columnType: 'Number', + filterOperators: TypeConverter.operatorsForColumnType('Number'), + isPrimaryKey: true, + isSortable: true, + validation: [], + type: 'Column', + }, + myValue: { + columnType: 'String', + defaultValue: '__default__', + filterOperators: TypeConverter.operatorsForColumnType('String'), + isSortable: true, + validation: [], + type: 'Column', + }, + }), + searchable: false, + segments: [], + }; + + const model = sequelize.define( + '__model_b__', + { + myPk: { + type: DataTypes.INTEGER, + primaryKey: true, + }, + myValue: { + type: DataTypes.STRING, + defaultValue: '__default__', + validate: { + is: [/^[a-z]+$/], + }, + }, + }, + { timestamps: true }, + ); + + expect(ModelToCollectionSchemaConverter.convert(model, () => {})).toEqual(schema); + }); + }); }); }); From 2645620204e06f4f651b523ec117f61a3da57e10 Mon Sep 17 00:00:00 2001 From: Thenkei Date: Thu, 19 Sep 2024 15:36:17 +0200 Subject: [PATCH 3/4] test: add missing tests cases --- .../test/utils/model-to-collection-schema-converter.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/datasource-sequelize/test/utils/model-to-collection-schema-converter.test.ts b/packages/datasource-sequelize/test/utils/model-to-collection-schema-converter.test.ts index 208fef5cd..b0c6a0938 100644 --- a/packages/datasource-sequelize/test/utils/model-to-collection-schema-converter.test.ts +++ b/packages/datasource-sequelize/test/utils/model-to-collection-schema-converter.test.ts @@ -536,7 +536,7 @@ describe('Utils > ModelToCollectionSchemaConverter', () => { }; const model = sequelize.define( - '__model_a__', + '__model__', { myPk: { type: DataTypes.INTEGER, @@ -589,7 +589,7 @@ describe('Utils > ModelToCollectionSchemaConverter', () => { }; const model = sequelize.define( - '__model_b__', + '__model__', { myPk: { type: DataTypes.INTEGER, From 4539205c2ec4af04c880fe22c2a8ab6f9eb127b6 Mon Sep 17 00:00:00 2001 From: Thenkei Date: Thu, 19 Sep 2024 15:59:48 +0200 Subject: [PATCH 4/4] test: skip mssql/server:2017-latest tests --- packages/datasource-sql/test/_helpers/connection-details.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/datasource-sql/test/_helpers/connection-details.ts b/packages/datasource-sql/test/_helpers/connection-details.ts index 4c61df60c..9212363a8 100644 --- a/packages/datasource-sql/test/_helpers/connection-details.ts +++ b/packages/datasource-sql/test/_helpers/connection-details.ts @@ -70,7 +70,8 @@ export const POSTGRESQL_DETAILS: ConnectionDetails[] = [ })); export const MSSQL_DETAILS: ConnectionDetails[] = [ - [2017, 1417], + // Remove 2017 tests for now due to instable docker + // [2017, 1417], [2022, 1422], ].map(([version, port]) => ({ name: `MS SQL Server ${version}`,