Skip to content

Commit

Permalink
fix(utils): each must handle falsy object (#605)
Browse files Browse the repository at this point in the history
  • Loading branch information
redgeoff authored Jan 31, 2022
1 parent d4d83e4 commit 2a8d8f2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/access-control.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import get from 'lodash/get';
import clone from 'lodash/clone';
import Roles from './roles';
import utils from './utils/utils';

// e.g.
// access: {
Expand Down Expand Up @@ -110,7 +111,9 @@ export default class AccessControl {

canAccess(operation, access, indexedRoles, fieldValues, isOwner) {
const errors = [];
Object.keys(fieldValues).forEach((name) => {
// Note: we use `utils.each`, instead of `Object.keys(fieldValues)` as fieldValues can be
// undefined when called by mson-server
utils.each(fieldValues, (value, name) => {
if (
!this._canAccessField(operation, access, indexedRoles, name, isOwner)
) {
Expand Down
5 changes: 4 additions & 1 deletion src/component/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { cloneDeep } from '../utils/deep-clone';
import { filter } from '../compiler/query';
import PropFiller from '../compiler/prop-filler';
import queryToProps from '../component/query-to-props';
import utils from '../utils/utils';

export default class Validator {
constructor(props) {
Expand All @@ -15,7 +16,9 @@ export default class Validator {

// Performs in place fill to prepare for query
_fillWhere(where) {
Object.entries(where).forEach(([name, node]) => {
// Note: we use `utils.each`, instead of `Object.entries(where)` as where can be
// undefined when called by mson-server
utils.each(where, (node, name) => {
// Leaf node?
if (typeof node === 'string') {
where[name] = this._fillProps(node);
Expand Down
19 changes: 11 additions & 8 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,18 @@ export class Utils {
}
}

// Note: only use this if you need to exit the loop prematurely by having onItem() return false.
// Otherwise, just use Object.entries(), Object.keys(), or Object.values().
// Note: only use this if you need to exit the loop prematurely by having onItem() return false;
// or if obj may be falsy. Otherwise, just use Object.entries(), Object.keys(), or
// Object.values().
each(obj, onItem) {
const entries = Object.entries(obj);
for (let i = 0; i < entries.length; i++) {
const [key, value] = entries[i];
const again = onItem(value, key);
if (again === false) {
break;
if (obj !== undefined && obj !== null) {
const entries = Object.entries(obj);
for (let i = 0; i < entries.length; i++) {
const [key, value] = entries[i];
const again = onItem(value, key);
if (again === false) {
break;
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/utils/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,13 @@ it('should convert to RegExp', () => {
expect(utils.isRegExpString('/[1-9]AB/')).toEqual(true);
expect(utils.isRegExpString('(')).toEqual(false);
});

it('each should handle falsy values', () => {
const onItem = jest.fn();

utils.each(null, onItem);
expect(onItem).toHaveBeenCalledTimes(0);

utils.each(undefined, onItem);
expect(onItem).toHaveBeenCalledTimes(0);
});

0 comments on commit 2a8d8f2

Please sign in to comment.