Skip to content

Commit e7f144c

Browse files
jkossekKrzysztof Borowy
authored and
Krzysztof Borowy
committed
feat: add warning to set functions when value is not a string
* feat: add warning for non-string values
1 parent c48c81e commit e7f144c

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

lib/AsyncStorage.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ type MultiRequest = {|
5252
reject: ?(error?: any) => void,
5353
|};
5454

55+
function checkValueTypeNotString(value: any, usedKey: string) {
56+
if (typeof value !== 'string') {
57+
console.warn(
58+
`[AsyncStorage] The value for key "${usedKey}" is not a string. This can lead to unexpected behavior/errors. Consider stringifying it.`,
59+
);
60+
}
61+
}
62+
5563
/**
5664
* `AsyncStorage` is a simple, unencrypted, asynchronous, persistent, key-value
5765
* storage system that is global to the app. It should be used instead of
@@ -100,6 +108,7 @@ const AsyncStorage = {
100108
): Promise<null> {
101109
return new Promise((resolve, reject) => {
102110
RCTAsyncStorage.multiSet([[key, value]], function(errors) {
111+
checkValueTypeNotString(value, key);
103112
const errs = convertErrors(errors);
104113
callback && callback(errs && errs[0]);
105114
if (errs) {
@@ -301,6 +310,10 @@ const AsyncStorage = {
301310
callback?: ?(errors: ?$ReadOnlyArray<?Error>) => void,
302311
): Promise<null> {
303312
return new Promise((resolve, reject) => {
313+
keyValuePairs.forEach(([key, value]) => {
314+
checkValueTypeNotString(value, key);
315+
});
316+
304317
RCTAsyncStorage.multiSet(keyValuePairs, function(errors) {
305318
const error = convertErrors(errors);
306319
callback && callback(error);

0 commit comments

Comments
 (0)