Skip to content

Commit 6df463f

Browse files
author
Krzysztof Borowy
committed
fix: check key/values before saving
1 parent b04ffbf commit 6df463f

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

lib/AsyncStorage.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,24 @@ 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') {
59+
console.warn(
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`,
61+
);
62+
}
63+
64+
if (isValuePassed && value == null) {
65+
throw new Error(
66+
`[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`,
67+
);
68+
}
69+
70+
if (isValuePassed && typeof value !== 'string') {
5771
console.warn(
58-
`[AsyncStorage] The value for key "${usedKey}" is not a string. This can lead to unexpected behavior/errors. Consider stringifying it.`,
72+
`[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`,
5973
);
6074
}
6175
}
@@ -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,10 @@ const AsyncStorage = {
336353
callback?: ?(errors: ?$ReadOnlyArray<?Error>) => void,
337354
): Promise<null> {
338355
return new Promise((resolve, reject) => {
356+
keys.forEach(key => {
357+
checkValidInput(key);
358+
});
359+
339360
RCTAsyncStorage.multiRemove(keys, function(errors) {
340361
const error = convertErrors(errors);
341362
callback && callback(error);

0 commit comments

Comments
 (0)