Skip to content
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

fix(utils): each must handle falsy object #605

Merged
merged 2 commits into from
Jan 31, 2022
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
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);
});