Skip to content

Commit 5c1618e

Browse files
committed
Change to throw error
1 parent d27a4ef commit 5c1618e

File tree

3 files changed

+124
-72
lines changed

3 files changed

+124
-72
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ ___
2626
- IMPROVE: Added new account lockout policy option `accountLockout.unlockOnPasswordReset` to automatically unlock account on password reset. [#7146](https://github.com/parse-community/parse-server/pull/7146). Thanks to [Manuel Trezza](https://github.com/mtrezza).
2727
- IMPROVE: Parse Server is from now on continuously tested against all recent MongoDB versions that have not reached their end-of-life support date. Added MongoDB compatibility table to Parse Server docs. [7161](https://github.com/parse-community/parse-server/pull/7161). Thanks to [Manuel Trezza](https://github.com/mtrezza).
2828
- IMPROVE: Parse Server is from now on continuously tested against all recent Node.js versions that have not reached their end-of-life support date. [7161](https://github.com/parse-community/parse-server/pull/7177). Thanks to [Manuel Trezza](https://github.com/mtrezza).
29-
- IMPROVE: Cloud Validators will now log on invalid configuration [#7154](https://github.com/parse-community/parse-server/pull/7154). Thanks to [dblythy](https://github.com/dblythy)
29+
- IMPROVE: Throw error on invalid Cloud Function validation configuration. [#7154](https://github.com/parse-community/parse-server/pull/7154). Thanks to [dblythy](https://github.com/dblythy)
3030
- IMPROVE: Allow Cloud Validator `options` to be async [#7155](https://github.com/parse-community/parse-server/pull/7155). Thanks to [dblythy](https://github.com/dblythy)
3131
- IMPROVE: Optimize queries on classes with pointer permissions. [#7061](https://github.com/parse-community/parse-server/pull/7061). Thanks to [Pedro Diaz](https://github.com/pdiaz)
3232
- IMPROVE: Parse Server will from now on be continuously tested against all relevant Postgres versions (minor versions). Added Postgres compatibility table to Parse Server docs. [#7176](https://github.com/parse-community/parse-server/pull/7176). Thanks to [Corey Baker](https://github.com/cbaker6).

spec/CloudCode.Validator.spec.js

Lines changed: 115 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,8 +1420,6 @@ describe('cloud validator', () => {
14201420
});
14211421

14221422
it('does not log on valid config', () => {
1423-
const logger = require('../lib/logger').logger;
1424-
spyOn(logger, 'error').and.callFake(() => {});
14251423
Parse.Cloud.define('myFunction', () => {}, {
14261424
requireUser: true,
14271425
requireMaster: true,
@@ -1448,68 +1446,127 @@ describe('cloud validator', () => {
14481446
},
14491447
},
14501448
});
1451-
expect(logger.error).not.toHaveBeenCalled();
14521449
});
14531450
it('Logs on invalid config', () => {
1454-
const logger = require('../lib/logger').logger;
1455-
spyOn(logger, 'error').and.callFake(() => {});
1456-
Parse.Cloud.define('myFunction', () => {}, {
1457-
requiredUser: true,
1458-
requireUser: ['foo'],
1459-
requireMaster: ['foo'],
1460-
validateMasterKey: ['foo'],
1461-
skipWithMasterKey: ['foo'],
1462-
requireUserKeys: true,
1463-
fields: true,
1464-
});
1465-
expect(logger.error).toHaveBeenCalledWith(
1466-
'requiredUser is not a supported parameter for Parse.Cloud validators.'
1467-
);
1468-
expect(logger.error).toHaveBeenCalledWith(
1469-
'Invalid type for Parse.Cloud validator key requireUser. Expected boolean, actual array'
1470-
);
1471-
expect(logger.error).toHaveBeenCalledWith(
1472-
'Invalid type for Parse.Cloud validator key requireMaster. Expected boolean, actual array'
1473-
);
1474-
expect(logger.error).toHaveBeenCalledWith(
1475-
'Invalid type for Parse.Cloud validator key validateMasterKey. Expected boolean, actual array'
1476-
);
1477-
expect(logger.error).toHaveBeenCalledWith(
1478-
'Invalid type for Parse.Cloud validator key skipWithMasterKey. Expected boolean, actual array'
1479-
);
1480-
expect(logger.error).toHaveBeenCalledWith(
1481-
'Invalid type for Parse.Cloud validator key fields. Expected array|object, actual boolean'
1482-
);
1483-
expect(logger.error).toHaveBeenCalledWith(
1484-
'Invalid type for Parse.Cloud validator key requireUserKeys. Expected array|object, actual boolean'
1485-
);
1451+
const fields = [
1452+
{
1453+
field: 'requiredUser',
1454+
value: true,
1455+
error: 'requiredUser is not a supported parameter for Cloud Function validations.',
1456+
},
1457+
{
1458+
field: 'requireUser',
1459+
value: [],
1460+
error:
1461+
'Invalid type for Cloud Function validation key requireUser. Expected boolean, actual array',
1462+
},
1463+
{
1464+
field: 'requireMaster',
1465+
value: [],
1466+
error:
1467+
'Invalid type for Cloud Function validation key requireMaster. Expected boolean, actual array',
1468+
},
1469+
{
1470+
field: 'validateMasterKey',
1471+
value: [],
1472+
error:
1473+
'Invalid type for Cloud Function validation key validateMasterKey. Expected boolean, actual array',
1474+
},
1475+
{
1476+
field: 'skipWithMasterKey',
1477+
value: [],
1478+
error:
1479+
'Invalid type for Cloud Function validation key skipWithMasterKey. Expected boolean, actual array',
1480+
},
1481+
{
1482+
field: 'requireAllUserRoles',
1483+
value: true,
1484+
error:
1485+
'Invalid type for Cloud Function validation key requireAllUserRoles. Expected array|function, actual boolean',
1486+
},
1487+
{
1488+
field: 'requireAnyUserRoles',
1489+
value: true,
1490+
error:
1491+
'Invalid type for Cloud Function validation key requireAnyUserRoles. Expected array|function, actual boolean',
1492+
},
1493+
{
1494+
field: 'fields',
1495+
value: true,
1496+
error:
1497+
'Invalid type for Cloud Function validation key fields. Expected array|object, actual boolean',
1498+
},
1499+
{
1500+
field: 'requireUserKeys',
1501+
value: true,
1502+
error:
1503+
'Invalid type for Cloud Function validation key requireUserKeys. Expected array|object, actual boolean',
1504+
},
1505+
];
1506+
for (const field of fields) {
1507+
try {
1508+
Parse.Cloud.define('myFunction', () => {}, {
1509+
[field.field]: field.value,
1510+
});
1511+
fail(`Expected error registering invalid Cloud Function validation ${field.field}.`);
1512+
} catch (e) {
1513+
expect(e).toBe(field.error);
1514+
}
1515+
}
14861516
});
14871517

14881518
it('Logs on invalid config', () => {
1489-
const logger = require('../lib/logger').logger;
1490-
spyOn(logger, 'error').and.callFake(() => {});
1491-
Parse.Cloud.define('myFunction', () => {}, {
1492-
fields: {
1493-
name: {
1494-
constant: ['foo'],
1495-
required: ['foo'],
1496-
error: ['foo'],
1497-
otherKey: true,
1498-
},
1519+
const fields = [
1520+
{
1521+
field: 'otherKey',
1522+
value: true,
1523+
error: 'otherKey is not a supported parameter for Cloud Function validations.',
14991524
},
1500-
});
1501-
expect(logger.error).toHaveBeenCalledWith(
1502-
'otherKey is not a supported parameter for Parse.Cloud validators.'
1503-
);
1504-
expect(logger.error).toHaveBeenCalledWith(
1505-
'Invalid type for Parse.Cloud validator key constant. Expected boolean, actual array'
1506-
);
1507-
expect(logger.error).toHaveBeenCalledWith(
1508-
'Invalid type for Parse.Cloud validator key required. Expected boolean, actual array'
1509-
);
1510-
expect(logger.error).toHaveBeenCalledWith(
1511-
'Invalid type for Parse.Cloud validator key error. Expected string, actual array'
1512-
);
1525+
{
1526+
field: 'constant',
1527+
value: [],
1528+
error:
1529+
'Invalid type for Cloud Function validation key constant. Expected boolean, actual array',
1530+
},
1531+
{
1532+
field: 'required',
1533+
value: [],
1534+
error:
1535+
'Invalid type for Cloud Function validation key required. Expected boolean, actual array',
1536+
},
1537+
{
1538+
field: 'error',
1539+
value: [],
1540+
error:
1541+
'Invalid type for Cloud Function validation key error. Expected string, actual array',
1542+
},
1543+
];
1544+
for (const field of fields) {
1545+
try {
1546+
Parse.Cloud.define('myFunction', () => {}, {
1547+
fields: {
1548+
name: {
1549+
[field.field]: field.value,
1550+
},
1551+
},
1552+
});
1553+
fail(`Expected error registering invalid Cloud Function validation ${field.field}.`);
1554+
} catch (e) {
1555+
expect(e).toBe(field.error);
1556+
}
1557+
try {
1558+
Parse.Cloud.define('myFunction', () => {}, {
1559+
requireUserKeys: {
1560+
name: {
1561+
[field.field]: field.value,
1562+
},
1563+
},
1564+
});
1565+
fail(`Expected error registering invalid Cloud Function validation ${field.field}.`);
1566+
} catch (e) {
1567+
expect(e).toBe(field.error);
1568+
}
1569+
}
15131570
});
15141571

15151572
it('set params options function async', async () => {

src/cloud-code/Parse.Cloud.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,44 +27,39 @@ function validateValidator(validator) {
2727
};
2828
const allowedKeys = {
2929
requireUser: [Boolean],
30-
requireAnyUserRoles: [Boolean],
31-
requireAllUserRoles: [Boolean],
30+
requireAnyUserRoles: [Array, 'function'],
31+
requireAllUserRoles: [Array, 'function'],
3232
requireMaster: [Boolean],
3333
validateMasterKey: [Boolean],
3434
skipWithMasterKey: [Boolean],
3535
requireUserKeys: [Array, Object],
3636
fields: [Array, Object],
3737
};
38-
const config = Config.get(Parse.applicationId);
39-
const logger = config.loggerController;
4038
const getType = fn => {
4139
if (Array.isArray(fn)) {
4240
return 'array';
4341
}
44-
if (fn === 'Any') {
42+
if (fn === 'Any' || fn === 'function') {
4543
return fn;
4644
}
4745
const type = typeof fn;
4846
if (typeof fn === 'function') {
4947
const match = fn && fn.toString().match(/^\s*function (\w+)/);
50-
return (match ? match[1] : '').toLowerCase();
48+
return (match ? match[1] : 'function').toLowerCase();
5149
}
5250
return type;
5351
};
5452
const checkKey = (key, data, validatorParam) => {
5553
const parameter = data[key];
5654
if (!parameter) {
57-
logger.error(`${key} is not a supported parameter for Parse.Cloud validators.`);
58-
return;
55+
throw `${key} is not a supported parameter for Cloud Function validations.`;
5956
}
6057
const types = parameter.map(type => getType(type));
6158
const type = getType(validatorParam);
6259
if (!types.includes(type) && !types.includes('Any')) {
63-
logger.error(
64-
`Invalid type for Parse.Cloud validator key ${key}. Expected ${types.join(
65-
'|'
66-
)}, actual ${type}`
67-
);
60+
throw `Invalid type for Cloud Function validation key ${key}. Expected ${types.join(
61+
'|'
62+
)}, actual ${type}`;
6863
}
6964
};
7065
for (const key in validator) {

0 commit comments

Comments
 (0)