Skip to content

Commit 0fdad61

Browse files
committed
new: allow options to be async on Cloud Validator
1 parent f1394a9 commit 0fdad61

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed

spec/CloudCode.Validator.spec.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,4 +1428,78 @@ describe('cloud validator', () => {
14281428
done();
14291429
}
14301430
});
1431+
1432+
it('set params options function async', async () => {
1433+
Parse.Cloud.define(
1434+
'hello',
1435+
() => {
1436+
return 'Hello world!';
1437+
},
1438+
{
1439+
fields: {
1440+
data: {
1441+
type: String,
1442+
required: true,
1443+
options: async val => {
1444+
await new Promise(resolve => {
1445+
setTimeout(resolve, 500);
1446+
});
1447+
return val === 'f';
1448+
},
1449+
error: 'Validation failed.',
1450+
},
1451+
},
1452+
}
1453+
);
1454+
try {
1455+
await Parse.Cloud.run('hello', { data: 'd' });
1456+
} catch (error) {
1457+
expect(error.code).toEqual(Parse.Error.VALIDATION_ERROR);
1458+
expect(error.message).toEqual('Validation failed.');
1459+
}
1460+
await Parse.Cloud.run('hello', { data: 'f' });
1461+
});
1462+
1463+
it('basic beforeSave requireUserKey as custom async function', async () => {
1464+
Parse.Cloud.beforeSave(Parse.User, () => {}, {
1465+
fields: {
1466+
accType: {
1467+
default: 'normal',
1468+
constant: true,
1469+
},
1470+
},
1471+
});
1472+
Parse.Cloud.define(
1473+
'secureFunction',
1474+
() => {
1475+
return "Here's all the secure data!";
1476+
},
1477+
{
1478+
requireUserKeys: {
1479+
accType: {
1480+
options: async val => {
1481+
await new Promise(resolve => {
1482+
setTimeout(resolve, 500);
1483+
});
1484+
return ['admin', 'admin2'].includes(val);
1485+
},
1486+
error: 'Unauthorized.',
1487+
},
1488+
},
1489+
}
1490+
);
1491+
const user = new Parse.User();
1492+
user.set('username', 'testuser');
1493+
user.set('password', 'p@ssword');
1494+
user.set('accType', 'admin');
1495+
await user.signUp();
1496+
expect(user.get('accType')).toBe('normal');
1497+
try {
1498+
await Parse.Cloud.run('secureFunction');
1499+
fail('function should only be available to admin users');
1500+
} catch (error) {
1501+
expect(error.code).toEqual(Parse.Error.VALIDATION_ERROR);
1502+
expect(error.message).toEqual('Unauthorized.');
1503+
}
1504+
});
14311505
});

src/triggers.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -662,11 +662,11 @@ async function builtInTriggerValidator(options, request, auth) {
662662
}
663663
};
664664

665-
const validateOptions = (opt, key, val) => {
665+
const validateOptions = async (opt, key, val) => {
666666
let opts = opt.options;
667667
if (typeof opts === 'function') {
668668
try {
669-
const result = opts(val);
669+
const result = await opts(val);
670670
if (!result && result != null) {
671671
throw opt.error || `Validation failed. Invalid value for ${key}.`;
672672
}
@@ -699,6 +699,7 @@ async function builtInTriggerValidator(options, request, auth) {
699699
requiredParam(key);
700700
}
701701
} else {
702+
const optionPromises = [];
702703
for (const key in options.fields) {
703704
const opt = options.fields[key];
704705
let val = params[key];
@@ -731,10 +732,11 @@ async function builtInTriggerValidator(options, request, auth) {
731732
}
732733
}
733734
if (opt.options) {
734-
validateOptions(opt, key, val);
735+
optionPromises.push(validateOptions(opt, key, val));
735736
}
736737
}
737738
}
739+
await Promise.all(optionPromises);
738740
}
739741
let userRoles = options.requireAnyUserRoles;
740742
let requireAllRoles = options.requireAllUserRoles;
@@ -780,12 +782,14 @@ async function builtInTriggerValidator(options, request, auth) {
780782
}
781783
}
782784
} else if (typeof userKeys === 'object') {
785+
const optionPromises = [];
783786
for (const key in options.requireUserKeys) {
784787
const opt = options.requireUserKeys[key];
785788
if (opt.options) {
786-
validateOptions(opt, key, reqUser.get(key));
789+
optionPromises.push(validateOptions(opt, key, reqUser.get(key)));
787790
}
788791
}
792+
await Promise.all(optionPromises);
789793
}
790794
}
791795

0 commit comments

Comments
 (0)