Skip to content

Commit 3d3b793

Browse files
authored
fix: Throw if key is empty in set(...) (#919)
* fix: Throw if key is empty in `set(...)` * docs: Add docs for empty key * Add harness test for empty key * Also throw in mocks and web
1 parent cbcb4bd commit 3d3b793

File tree

5 files changed

+13
-5
lines changed

5 files changed

+13
-5
lines changed

example/__tests__/MMKV.harness.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -825,9 +825,10 @@ describe('MMKV Error Handling & Edge Cases', () => {
825825

826826
describe('Error Scenarios', () => {
827827
it('should handle invalid key operations gracefully', () => {
828-
// Empty string key should work
829-
storage.set('', 'empty-key-value');
830-
expect(storage.getString('')).toStrictEqual('empty-key-value');
828+
// Empty string key should throw
829+
expect(() => {
830+
storage.set('', 'empty-key-value');
831+
}).toThrow()
831832
});
832833

833834
it('should handle concurrent operations', () => {

packages/react-native-mmkv/cpp/HybridMMKV.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ template <class... Ts>
7171
overloaded(Ts...) -> overloaded<Ts...>;
7272

7373
void HybridMMKV::set(const std::string& key, const std::variant<std::string, double, bool, std::shared_ptr<ArrayBuffer>>& value) {
74+
if (key.empty()) [[unlikely]] {
75+
throw std::runtime_error("Cannot set a value for an empty key!");
76+
}
77+
7478
// Pattern-match each potential value in std::variant
7579
std::visit(overloaded{[&](const std::string& string) {
7680
// string

packages/react-native-mmkv/src/createMMKV/createMMKV.web.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export function createMMKV(
9191
},
9292
remove: (key) => storage().removeItem(prefixedKey(key)) ?? false,
9393
set: (key, value) => {
94+
if (key === '') throw new Error('Cannot set a value for an empty key!')
9495
storage().setItem(prefixedKey(key), value.toString())
9596
},
9697
getString: (key) => storage().getItem(prefixedKey(key)) ?? undefined,

packages/react-native-mmkv/src/createMMKV/createMockMMKV.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export function createMockMMKV(): MMKV {
3030
return deleted
3131
},
3232
set: (key, value) => {
33+
if (key === '') throw new Error('Cannot set a value for an empty key!')
3334
storage.set(key, value)
3435
notifyListeners(key)
3536
},

packages/react-native-mmkv/src/specs/MMKV.nitro.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ export interface Listener {
66

77
export interface MMKV extends HybridObject<{ ios: 'c++'; android: 'c++' }> {
88
/**
9-
* Set a value for the given `key`.
9+
* Set a {@linkcode value} for the given {@linkcode key}.
1010
*
11-
* @throws an Error if the value cannot be set.
11+
* @throws an Error if the {@linkcode key} is empty.
12+
* @throws an Error if the {@linkcode value} cannot be set.
1213
*/
1314
set(key: string, value: boolean | string | number | ArrayBuffer): void
1415
/**

0 commit comments

Comments
 (0)