From 61cb38e33941592726fd4914e433330fedadfe22 Mon Sep 17 00:00:00 2001 From: Arnaud Moncel Date: Tue, 18 Jul 2023 16:20:50 +0200 Subject: [PATCH] fix(add field decorator): throw when put space inside fieldName --- .../src/decorators/computed/collection.ts | 2 ++ .../src/decorators/rename-field/collection.ts | 9 ++------- .../decorators/computed/collection.test.ts | 18 ++++++++++++++++++ .../decorators/rename-field/collection.test.ts | 2 +- .../datasource-toolkit/src/validation/field.ts | 11 +++++++++++ 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/packages/datasource-customizer/src/decorators/computed/collection.ts b/packages/datasource-customizer/src/decorators/computed/collection.ts index 8108648454..c6805499da 100644 --- a/packages/datasource-customizer/src/decorators/computed/collection.ts +++ b/packages/datasource-customizer/src/decorators/computed/collection.ts @@ -35,6 +35,8 @@ export default class ComputedCollection extends CollectionDecorator { } registerComputed(name: string, computed: ComputedDefinition): void { + FieldValidator.validateName(this.name, name); + // Check that all dependencies exist and are columns for (const field of computed.dependencies) { FieldValidator.validate(this, field); diff --git a/packages/datasource-customizer/src/decorators/rename-field/collection.ts b/packages/datasource-customizer/src/decorators/rename-field/collection.ts index ef28df59e6..bc1c67c6f3 100644 --- a/packages/datasource-customizer/src/decorators/rename-field/collection.ts +++ b/packages/datasource-customizer/src/decorators/rename-field/collection.ts @@ -6,6 +6,7 @@ import { CollectionSchema, DataSourceDecorator, FieldSchema, + FieldValidator, Filter, PaginatedFilter, Projection, @@ -34,13 +35,7 @@ export default class RenameFieldCollectionDecorator extends CollectionDecorator let initialName = currentName; - if (/ /.test(newName)) { - const sanitizedName = newName.replace(/ (.)/g, (_, s) => s.toUpperCase()); - throw new Error( - `The renaming of field '${currentName}' must not contain space.` + - ` Something like '${sanitizedName}' should work has expected.`, - ); - } + FieldValidator.validateName(this.name, newName); // Revert previous renaming (avoids conflicts and need to recurse on this.toSubCollection). if (this.toChildCollection[currentName]) { diff --git a/packages/datasource-customizer/test/decorators/computed/collection.test.ts b/packages/datasource-customizer/test/decorators/computed/collection.test.ts index 1e3b7dbba3..8c1690cbfa 100644 --- a/packages/datasource-customizer/test/decorators/computed/collection.test.ts +++ b/packages/datasource-customizer/test/decorators/computed/collection.test.ts @@ -114,6 +114,24 @@ describe('ComputedDecorator', () => { }).toThrow("Unexpected field type: 'books.author' (found 'ManyToOne' expected 'Column')"); }); + test('should throw when adding field with name including space', () => { + expect(() => + newPersons.registerComputed('full name', { + columnType: 'String', + dependencies: ['firstName', 'lastName'], + getValues: records => { + return new Promise(resolve => { + const result = records.map(record => `${record.firstName} ${record.lastName}`); + setTimeout(() => resolve(result)); + }); + }, + }), + ).toThrow( + `The name of field 'full name' you configured on 'persons' must not contain space.` + + ` Something like 'fullName' should work has expected.`, + ); + }); + describe('With a computed', () => { beforeEach(() => { newPersons.registerComputed('fullName', { diff --git a/packages/datasource-customizer/test/decorators/rename-field/collection.test.ts b/packages/datasource-customizer/test/decorators/rename-field/collection.test.ts index 71b3ff5b95..326da77e9b 100644 --- a/packages/datasource-customizer/test/decorators/rename-field/collection.test.ts +++ b/packages/datasource-customizer/test/decorators/rename-field/collection.test.ts @@ -142,7 +142,7 @@ describe('RenameFieldCollectionDecorator', () => { test('should throw when renaming with a name including space', () => { expect(() => newPersons.renameField('id', 'the key')).toThrow( - `The renaming of field 'id' must not contain space.` + + `The name of field 'the key' you configured on 'persons' must not contain space.` + ` Something like 'theKey' should work has expected.`, ); }); diff --git a/packages/datasource-toolkit/src/validation/field.ts b/packages/datasource-toolkit/src/validation/field.ts index bf699b50d4..907e7ac6ba 100644 --- a/packages/datasource-toolkit/src/validation/field.ts +++ b/packages/datasource-toolkit/src/validation/field.ts @@ -87,4 +87,15 @@ export default class FieldValidator { ); } } + + static validateName(collectionName: string, name: string) { + if (/ /.test(name)) { + const sanitizedName = name.replace(/ (.)/g, (_, s) => s.toUpperCase()); + throw new Error( + `The name of field '${name}' you configured on` + + ` '${collectionName}' must not contain space.` + + ` Something like '${sanitizedName}' should work has expected.`, + ); + } + } }