Is there an ESLint rule to ensure safe access of object properties?

Viewed 157

I have a JavaScript app with data coming in in JSON format. The data contains deeply nested objects.

We have had bugs where deep object properties were attempted to be read and errored out. I would like a lint rule that ensures all reads of object properties are guarded.

Example of code to allow:

const reward = data && data.activity && data.activity.reward;
// or
const reward = data?.activity?.reward;

Example of code to disallow:

const reward = data.activity.reward;

Is this lintable?

0 Answers
Related