Skip to content

Commit e43870f

Browse files
committed
fix(context.team): Validate null team values
1 parent e519608 commit e43870f

File tree

9 files changed

+23
-5
lines changed

9 files changed

+23
-5
lines changed

src/helpers/getUsedIfis.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import { isPlainObject } from 'lodash';
2+
13
export default (data: any): string[] => {
4+
if (!isPlainObject(data)) return [];
25
const ifis = ['mbox', 'mbox_sha1sum', 'openid', 'account'];
36
const keys = Object.keys(data);
47
return ifis.filter(ifi => keys.includes(ifi));

src/helpers/statementRules.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { composeRules, Rule } from 'rulr';
22
import ContextPropWarning from '../warnings/ContextPropWarning';
33
import VoidWarning from '../warnings/VoidWarning';
4-
import { isObject } from 'lodash';
4+
import { isObject, isPlainObject } from 'lodash';
55

66
export default composeRules([
77
(data, path) => {
8+
if (!isPlainObject(data)) return [];
89
const objectIsActivity = (
910
isObject(data.object) &&
1011
(data.object.objectType === 'Activity' || data.object.objectType === undefined)
@@ -17,6 +18,7 @@ export default composeRules([
1718
return invalidContext ? [new ContextPropWarning(data, path)] : [];
1819
},
1920
(data, path) => {
21+
if (!isPlainObject(data)) return [];
2022
const voidVerbId = 'http://adlnet.gov/expapi/verbs/voided';
2123
const objectIsStatementRef = (
2224
data.object && data.object.objectType === 'StatementRef'

src/schemaRules/agent.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { composeRules, restrictToSchema, Rule } from 'rulr';
22
import agentSchema from '../helpers/agentSchema';
33
import getUsedIfis from '../helpers/getUsedIfis';
4+
import { isPlainObject } from 'lodash';
5+
46
import IfiCountWarning from '../warnings/IfiCountWarning';
57
import NoIfiWarning from '../warnings/NoIfiWarning';
68

79
export default composeRules([
810
restrictToSchema(agentSchema),
911
(data, path) => {
12+
if (!isPlainObject(data)) return [];
1013
const usedIfis = getUsedIfis(data);
1114
if (usedIfis.length > 1) return [new IfiCountWarning(data, path, usedIfis)];
1215
if (usedIfis.length === 0) return [new NoIfiWarning(data, path)];

src/schemaRules/definition.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
restrictToSchema, restrictToCollection, composeRules, optional, Rule, Warning
33
} from 'rulr';
4+
import { isPlainObject } from 'lodash';
45
import InvalidComponentsWarning from '../warnings/InvalidComponentsWarning';
56
import MissingInteractionTypeWarning from '../warnings/MissingInteractionTypeWarning';
67
import {
@@ -56,7 +57,7 @@ export default composeRules([
5657
steps: optional(restrictToCollection(() => interactionComponent)),
5758
}),
5859
(data, path): Warning[] => {
59-
if (data == null || data.constructor !== Object) return [];
60+
if (!isPlainObject(data)) return [];
6061
const interactionType = data.interactionType;
6162
const unsupportedComponents = getUnsupportedComponents(interactionType);
6263
const invalidComponents = unsupportedComponents.filter(
@@ -68,7 +69,7 @@ export default composeRules([
6869
return [];
6970
},
7071
(data, path): Warning[] => {
71-
if (data == null || data.constructor !== Object) return [];
72+
if (!isPlainObject(data)) return [];
7273
const missingInteractionType = (
7374
data.correctResponsesPattern !== undefined &&
7475
data.interactionType === undefined

src/schemaRules/group.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
optional,
66
Rule,
77
} from 'rulr';
8+
import { isPlainObject } from 'lodash';
89
import agentSchema from '../helpers/agentSchema';
910
import getUsedIfis from '../helpers/getUsedIfis';
1011
import restrictToValue from '../helpers/restrictToValue';
@@ -18,6 +19,7 @@ export default composeRules([
1819
member: optional(restrictToCollection(() => actor)),
1920
})),
2021
(data, path) => {
22+
if (!isPlainObject(data)) return [];
2123
const usedIfis = getUsedIfis(data);
2224
const usedMember = (
2325
data.member != null &&

src/schemaRules/groupAuthority.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import { actor } from '../factory';
33
import MembersTypeWarning from '../warnings/MembersTypeWarning';
44
import MembersLengthWarning from '../warnings/MembersLengthWarning';
55
import restrictToValue from '../helpers/restrictToValue';
6-
import { isObject } from 'lodash';
6+
import { isPlainObject } from 'lodash';
77

88
export default composeRules([
99
restrictToSchema({
1010
objectType: optional(restrictToValue('Group')),
1111
member: optional(restrictToCollection(() => actor))
1212
}),
1313
(data, path) => {
14-
if (!isObject(data)) return [];
14+
if (!isPlainObject(data)) return [];
1515
const members = Array.isArray(data.member) ? data.member.length : 0;
1616

1717
if (members !== 2) return [new MembersTypeWarning(data, path)];

src/schemaRules/result.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,29 @@ import {
77
duration,
88
extensions,
99
} from '../factory';
10+
import { isPlainObject } from 'lodash';
1011
import RawLessThanMinWarning from '../warnings/RawLessThanMinWarning';
1112
import RawMoreThanMaxWarning from '../warnings/RawMoreThanMaxWarning';
1213
import MinMoreThanMaxWarning from '../warnings/MinMoreThanMaxWarning';
1314

1415
const checkRawMoreThanMin = (data: any, path: string[]): Warning[] => {
16+
if (!isPlainObject(data)) return [];
1517
if (data.raw !== undefined && data.min !== undefined && data.raw < data.min) {
1618
return [new RawLessThanMinWarning(data, path, data.raw, data.min)];
1719
}
1820
return [];
1921
};
2022

2123
const checkRawLessThanMax = (data: any, path: string[]): Warning[] => {
24+
if (!isPlainObject(data)) return [];
2225
if (data.raw !== undefined && data.max !== undefined && data.raw > data.max) {
2326
return [new RawMoreThanMaxWarning(data, path, data.raw, data.max)];
2427
}
2528
return [];
2629
};
2730

2831
const checkMinLessThanMax = (data: any, path: string[]): Warning[] => {
32+
if (!isPlainObject(data)) return [];
2933
if (data.min !== undefined && data.max !== undefined && data.min > data.max) {
3034
return [new MinMoreThanMaxWarning(data, path, data.min, data.max)];
3135
}

src/schemaRules/subStatement.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import statementSchema from '../helpers/statementSchema';
33
import statementRules from '../helpers/statementRules';
44
import restrictToValue from '../helpers/restrictToValue';
55
import SubStatementWarning from '../warnings/SubStatementWarning';
6+
import { isPlainObject } from 'lodash';
67

78
export default composeRules([
89
restrictToSchema(Object.assign({}, statementSchema, {
910
objectType: optional(restrictToValue('SubStatement')),
1011
})),
1112
statementRules,
1213
(data, path) => {
14+
if (!isPlainObject(data)) return [];
1315
const objectIsSubStatement = (
1416
data.object && data.object.objectType === 'SubStatement'
1517
);

src/tests/schemaRules/context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const validData = {
3636

3737
export default (test: Test) => {
3838
itsInvalid(10, 'not an object', test);
39+
itsInvalid({ team: null }, 'team must be an object', test);
3940
describeOptionalProp('registration', uuid, validData, test);
4041
describeOptionalProp('instructor', actor, validData, test);
4142
describeOptionalProp('team', (test: Test) => {

0 commit comments

Comments
 (0)