diff --git a/packages/datastore/__tests__/DataStore.ts b/packages/datastore/__tests__/DataStore.ts index 1871c0d7c47..88a1b35362f 100644 --- a/packages/datastore/__tests__/DataStore.ts +++ b/packages/datastore/__tests__/DataStore.ts @@ -439,7 +439,7 @@ describe('DataStore tests', () => { }), }); }).toThrowError( - 'All elements in the rewards array should be of type string, [object] received. ' + 'All elements in the rewards array should be of type string, [null] received. ' ); expect(() => { @@ -458,7 +458,7 @@ describe('DataStore tests', () => { emails: [null], }); }).toThrowError( - 'All elements in the emails array should be of type string, [object] received. ' + 'All elements in the emails array should be of type string, [null] received. ' ); expect(() => { @@ -467,9 +467,7 @@ describe('DataStore tests', () => { dateCreated: new Date().toISOString(), ips: [null], }); - }).toThrowError( - 'All elements in the ips array should be of type string | null | undefined, [object] received. ' - ); + }).not.toThrow(); expect(() => { new Model({ @@ -580,6 +578,73 @@ describe('DataStore tests', () => { 'All elements in the tags array should be of type string | null | undefined, [number] received. 1234' ); + expect(() => { + new Model({ + field1: 'someField', + dateCreated: new Date().toISOString(), + metadata: new Metadata({ + author: 'Some author', + rewards: [], + nominations: [], + misc: [null], + }), + }); + }).not.toThrow(); + + expect(() => { + new Model({ + field1: 'someField', + dateCreated: new Date().toISOString(), + metadata: new Metadata({ + author: 'Some author', + rewards: [], + nominations: [], + misc: [undefined], + }), + }); + }).not.toThrow(); + + expect(() => { + new Model({ + field1: 'someField', + dateCreated: new Date().toISOString(), + metadata: new Metadata({ + author: 'Some author', + rewards: [], + nominations: [], + misc: [undefined, null], + }), + }); + }).not.toThrow(); + + expect(() => { + new Model({ + field1: 'someField', + dateCreated: new Date().toISOString(), + metadata: new Metadata({ + author: 'Some author', + rewards: [], + nominations: [], + misc: [null, 'ok'], + }), + }); + }).not.toThrow(); + + expect(() => { + new Model({ + field1: 'someField', + dateCreated: new Date().toISOString(), + metadata: new Metadata({ + author: 'Some author', + rewards: [], + nominations: [], + misc: [null, 123], + }), + }); + }).toThrowError( + 'All elements in the misc array should be of type string | null | undefined, [null,number] received. ,123' + ); + expect( new Model({ extraAttribute: 'some value', field1: 'some value' }) ).toHaveProperty('extraAttribute'); @@ -881,6 +946,7 @@ export declare class Metadata { readonly rewards: string[]; readonly penNames?: string[]; readonly nominations: string[]; + readonly misc?: (string | null)[]; constructor(init: Metadata); } @@ -1005,6 +1071,14 @@ function testSchema(): Schema { type: 'String', isRequired: false, attributes: [], + }, + misc: { + name: 'misc', + isArray: true, + type: 'String', + isRequired: false, + isArrayNullable: true, + attributes: [], } }, }, diff --git a/packages/datastore/src/datastore/datastore.ts b/packages/datastore/src/datastore/datastore.ts index 9046c506771..0549ec2d27a 100644 --- a/packages/datastore/src/datastore/datastore.ts +++ b/packages/datastore/src/datastore/datastore.ts @@ -272,10 +272,10 @@ const validateModelFields = (modelDefinition: SchemaModel | SchemaNonModel) => ( if ( !isNullOrUndefined(v) && (<[]>v).some( - e => typeof e !== jsType || (isNullOrUndefined(e) && isRequired) + e => isNullOrUndefined(e) ? isRequired : typeof e !== jsType ) ) { - const elemTypes = (<[]>v).map(e => typeof e).join(','); + const elemTypes = (<[]>v).map(e => e === null ? 'null' : typeof e).join(','); throw new Error( `All elements in the ${name} array should be of type ${errorTypeText}, [${elemTypes}] received. ${v}`