Skip to content

Commit

Permalink
fix(add field decorator): throw when put space inside fieldName
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-moncel committed Jul 18, 2023
1 parent 7eed822 commit 61cb38e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
CollectionSchema,
DataSourceDecorator,
FieldSchema,
FieldValidator,
Filter,
PaginatedFilter,
Projection,
Expand Down Expand Up @@ -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]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.`,
);
});
Expand Down
11 changes: 11 additions & 0 deletions packages/datasource-toolkit/src/validation/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`,
);
}
}
}

0 comments on commit 61cb38e

Please sign in to comment.