Skip to content

Fix: Proper handling of arrays for cloud validator #7178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions spec/CloudCode.Validator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,24 @@ describe('cloud validator', () => {
});
});

it('set params type allow array', async () => {
Parse.Cloud.define(
'hello',
() => {
return 'Hello world!';
},
{
fields: {
data: {
type: Array,
},
},
}
);
const result = await Parse.Cloud.run('hello', { data: [{ foo: 'bar' }] });
expect(result).toBe('Hello world!');
});

it('set params type', done => {
Parse.Cloud.define(
'hello',
Expand Down
5 changes: 2 additions & 3 deletions src/triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -710,9 +710,8 @@ function builtInTriggerValidator(options, request) {
}
if (opt.type) {
const type = getType(opt.type);
if (type == 'array' && !Array.isArray(val)) {
throw `Validation failed. Invalid type for ${key}. Expected: array`;
} else if (typeof val !== type) {
const valType = Array.isArray(val) ? 'array' : typeof val;
if (valType !== type) {
throw `Validation failed. Invalid type for ${key}. Expected: ${type}`;
}
}
Expand Down