From 33ddf578ca6ce79e89c383b0e0bc1538a476704d Mon Sep 17 00:00:00 2001 From: Thenkei Date: Thu, 15 Jun 2023 13:22:24 +0200 Subject: [PATCH 1/2] fix: validate fields on user facing collections relaxed collection) --- .../src/context/relaxed-wrappers/collection.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/datasource-customizer/src/context/relaxed-wrappers/collection.ts b/packages/datasource-customizer/src/context/relaxed-wrappers/collection.ts index 70e1bef262..7940e2c57a 100644 --- a/packages/datasource-customizer/src/context/relaxed-wrappers/collection.ts +++ b/packages/datasource-customizer/src/context/relaxed-wrappers/collection.ts @@ -11,6 +11,7 @@ import { PaginatedFilter, Projection, RecordData, + RecordValidator, Sort, } from '@forestadmin/datasource-toolkit'; @@ -83,6 +84,8 @@ export default class RelaxedCollection< * ]); */ create(data: TPartialSimpleRow[]): Promise[]> { + RecordValidator.validate(this.collection, data); + return this.collection.create(this.caller, data) as Promise[]>; } @@ -132,6 +135,8 @@ export default class RelaxedCollection< update(filter: TFilter, patch: TPartialSimpleRow): Promise { const filterInstance = this.buildFilter(filter); + RecordValidator.validate(this.collection, patch); + return this.collection.update(this.caller, filterInstance, patch); } From 17c1fcef27136e52a610662213af2f603d349eac Mon Sep 17 00:00:00 2001 From: Thenkei Date: Thu, 15 Jun 2023 13:40:23 +0200 Subject: [PATCH 2/2] test: add unit tests --- .../src/context/relaxed-wrappers/collection.ts | 2 -- .../test/context/wrapper.test.ts | 16 +++++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/datasource-customizer/src/context/relaxed-wrappers/collection.ts b/packages/datasource-customizer/src/context/relaxed-wrappers/collection.ts index 7940e2c57a..b3e5678181 100644 --- a/packages/datasource-customizer/src/context/relaxed-wrappers/collection.ts +++ b/packages/datasource-customizer/src/context/relaxed-wrappers/collection.ts @@ -84,8 +84,6 @@ export default class RelaxedCollection< * ]); */ create(data: TPartialSimpleRow[]): Promise[]> { - RecordValidator.validate(this.collection, data); - return this.collection.create(this.caller, data) as Promise[]>; } diff --git a/packages/datasource-customizer/test/context/wrapper.test.ts b/packages/datasource-customizer/test/context/wrapper.test.ts index 78d20b59e8..8da40ce318 100644 --- a/packages/datasource-customizer/test/context/wrapper.test.ts +++ b/packages/datasource-customizer/test/context/wrapper.test.ts @@ -38,7 +38,13 @@ describe('RelaxedWrappers', () => { beforeEach(() => { caller = factories.caller.build(); - collection = factories.collection.build(); + collection = factories.collection.build({ + schema: { + fields: { + name: factories.columnSchema.build({ columnType: 'String' }), + }, + }, + }); relaxed = new RelatedCollection(collection, caller); }); @@ -154,6 +160,14 @@ describe('RelaxedWrappers', () => { ); }); + test('should validate patch operation before forwarding update', async () => { + await expect(() => + relaxed.update({ segment: 'some_segment' }, { nonexistingField: 'newValue' }), + ).toThrow('Unknown field "nonexistingField"'); + + expect(collection.update).not.toHaveBeenCalled(); + }); + test('should forward delete and transform filter', async () => { await relaxed.delete({ segment: 'some_segment' });