Skip to content

Commit

Permalink
fix(@aws-amplify/datastore): fix validation for array with optional e…
Browse files Browse the repository at this point in the history
…lement (aws-amplify#7216)
  • Loading branch information
wei authored and CryogenicPlanet committed Jan 20, 2021
1 parent 47bba96 commit c0ecd11
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 7 deletions.
84 changes: 79 additions & 5 deletions packages/datastore/__tests__/DataStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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(() => {
Expand All @@ -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({
Expand Down Expand Up @@ -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, <any>123],
}),
});
}).toThrowError(
'All elements in the misc array should be of type string | null | undefined, [null,number] received. ,123'
);

expect(
new Model(<any>{ extraAttribute: 'some value', field1: 'some value' })
).toHaveProperty('extraAttribute');
Expand Down Expand Up @@ -881,6 +946,7 @@ export declare class Metadata {
readonly rewards: string[];
readonly penNames?: string[];
readonly nominations: string[];
readonly misc?: (string | null)[];
constructor(init: Metadata);
}

Expand Down Expand Up @@ -1005,6 +1071,14 @@ function testSchema(): Schema {
type: 'String',
isRequired: false,
attributes: [],
},
misc: {
name: 'misc',
isArray: true,
type: 'String',
isRequired: false,
isArrayNullable: true,
attributes: [],
}
},
},
Expand Down
4 changes: 2 additions & 2 deletions packages/datastore/src/datastore/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down

0 comments on commit c0ecd11

Please sign in to comment.