https://github.com/magicmark/deep-non-null
deep-non-null
https://github.com/magicmark/deep-non-null
Last synced: about 1 year ago
JSON representation
deep-non-null
- Host: GitHub
- URL: https://github.com/magicmark/deep-non-null
- Owner: magicmark
- Created: 2020-10-05T07:53:18.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2020-10-06T04:33:53.000Z (over 5 years ago)
- Last Synced: 2025-02-16T03:25:32.794Z (about 1 year ago)
- Language: JavaScript
- Size: 90.8 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# deep-non-null
A type guard to ensure all properties in an object are not null or undefined.
Useful for unmarshalling data from the outside world (such as handling the
response object from a GraphQL query)
## Usage
```js
import isDeepNonNull from 'deep-non-null';
isDeepNonNull({
foo: {
bar: 'baz',
},
});
//=> true
isDeepNonNull({
foo: {
bar: null,
},
});
//=> false
```
**Real world example**
```jsx
import isDeepNonNull from 'deep-non-null';
function MyComponent() {
const { data, error, loading } = useQuery(GET_MY_DATA);
if (loading) return 'Loading...';
if (error) throw error;
if (!isDeepNonNull) throw new Error('Response contained null attributes');
// Everything in `data` is guaranteed to be not null - which typescript should know about
return
{data.foo.bar};
}
```
_(Our 'real world' example doesn't print the attribute that was null to avoid logging [PII](https://en.wikipedia.org/wiki/Personal_data). In non production settings, you could use `options.throwError` to get a nice error message containing this.)_
## Install
```
$ yarn add deep-non-null
```
## API
### `isDeepNonNull`
Type: `Function`
Signature: `(obj: Object, options?: Object) => boolean`
**Options**
- `options.throwError`: Throw an error instead of returning a boolean if null or undefined is found in the object
### `isDeepNonNullWithAllowedPaths`
Type: `Function`
Signature: `(obj: Object, options?: Object) => boolean`
**Options**
- `options.throwError`: Throw an error instead of returning a boolean if null or undefined is found in the object
- `options.allowedNull`: A list of [JSONPaths](https://github.com/dchester/jsonpath) to exclude from the null checking
## Contact
- [@mark_larah - https://twitter.com/mark_larah](https://twitter.com/mark_larah)