Skip to content

Commit cddf98f

Browse files
Krzysztof Borowytido64
Krzysztof Borowy
authored andcommitted
fix: check key/values before saving (#259)
1 parent 6142db6 commit cddf98f

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

lib/AsyncStorage.js

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

55-
function checkValueTypeNotString(value: any, usedKey: string) {
56-
if (typeof value !== 'string') {
55+
function checkValidInput(usedKey: string, value: any) {
56+
const isValuePassed = arguments.length > 1;
57+
58+
if (typeof usedKey !== 'string') {
5759
console.warn(
58-
`[AsyncStorage] The value for key "${usedKey}" is not a string. This can lead to unexpected behavior/errors. Consider stringifying it.`,
60+
`[AsyncStorage] Using ${typeof usedKey} type for key is not supported. This can lead to unexpected behavior/errors. Use string instead.\nKey passed: ${usedKey}\n`,
5961
);
6062
}
63+
64+
if (isValuePassed && typeof value !== 'string') {
65+
if (value == null) {
66+
throw new Error(
67+
`[AsyncStorage] Passing null/undefined as value is not supported. If you want to remove value, Use .remove method instead.\nPassed value: ${value}\nPassed key: ${usedKey}\n`,
68+
);
69+
} else {
70+
console.warn(
71+
`[AsyncStorage] The value for key "${usedKey}" is not a string. This can lead to unexpected behavior/errors. Consider stringifying it.\nPassed value: ${value}\nPassed key: ${usedKey}\n`,
72+
);
73+
}
74+
}
6175
}
6276

6377
/**
@@ -82,6 +96,7 @@ const AsyncStorage = {
8296
callback?: ?(error: ?Error, result: string | null) => void,
8397
): Promise<string | null> {
8498
return new Promise((resolve, reject) => {
99+
checkValidInput(key);
85100
RCTAsyncStorage.multiGet([key], function(errors, result) {
86101
// Unpack result to get value from [[key,value]]
87102
const value = result && result[0] && result[0][1] ? result[0][1] : null;
@@ -107,8 +122,8 @@ const AsyncStorage = {
107122
callback?: ?(error: ?Error) => void,
108123
): Promise<null> {
109124
return new Promise((resolve, reject) => {
125+
checkValidInput(key, value);
110126
RCTAsyncStorage.multiSet([[key, value]], function(errors) {
111-
checkValueTypeNotString(value, key);
112127
const errs = convertErrors(errors);
113128
callback && callback(errs && errs[0]);
114129
if (errs) {
@@ -130,6 +145,7 @@ const AsyncStorage = {
130145
callback?: ?(error: ?Error) => void,
131146
): Promise<null> {
132147
return new Promise((resolve, reject) => {
148+
checkValidInput(key);
133149
RCTAsyncStorage.multiRemove([key], function(errors) {
134150
const errs = convertErrors(errors);
135151
callback && callback(errs && errs[0]);
@@ -156,6 +172,7 @@ const AsyncStorage = {
156172
callback?: ?(error: ?Error) => void,
157173
): Promise<null> {
158174
return new Promise((resolve, reject) => {
175+
checkValidInput(key, value);
159176
RCTAsyncStorage.multiMerge([[key, value]], function(errors) {
160177
const errs = convertErrors(errors);
161178
callback && callback(errs && errs[0]);
@@ -311,7 +328,7 @@ const AsyncStorage = {
311328
): Promise<null> {
312329
return new Promise((resolve, reject) => {
313330
keyValuePairs.forEach(([key, value]) => {
314-
checkValueTypeNotString(value, key);
331+
checkValidInput(key, value);
315332
});
316333

317334
RCTAsyncStorage.multiSet(keyValuePairs, function(errors) {
@@ -336,6 +353,8 @@ const AsyncStorage = {
336353
callback?: ?(errors: ?$ReadOnlyArray<?Error>) => void,
337354
): Promise<null> {
338355
return new Promise((resolve, reject) => {
356+
keys.forEach(checkValidInput);
357+
339358
RCTAsyncStorage.multiRemove(keys, function(errors) {
340359
const error = convertErrors(errors);
341360
callback && callback(error);

0 commit comments

Comments
 (0)