Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate configuration #3772

Merged
merged 10 commits into from
Jun 10, 2021
Merged
5 changes: 1 addition & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ x.x.x Release notes (yyyy-MM-dd)

### Fixed
* A warning to polyfill `crypto.getRandomValues` was triggered prematurely ([#3714](https://github.com/realm/realm-js/issues/3714), since v10.4.0)
* Mutual exclusive configuration options (`sync`/`inMemory` and `sync`/`migration`) could lead to a crash. ([#3771](https://github.com/realm/realm-js/issues/3771), since v1.0.0)

### Compatibility
* MongoDB Realm Cloud.
Expand Down Expand Up @@ -3569,8 +3570,6 @@ The feature known as Partial synchronization has been renamed to Query-based syn
callback would produce incorrect results.

### Internal
* Upgraded Realm Core from v10.7.1 to 10.7.1.
* Upgraded Realm Core from v10.6.0 to 10.7.1
* None.

2.0.1 Release notes (2017-10-23)
Expand All @@ -3585,8 +3584,6 @@ The feature known as Partial synchronization has been renamed to Query-based syn
* None.

### Internal
* Upgraded Realm Core from v10.7.1 to 10.7.1.
* Upgraded Realm Core from v10.6.0 to 10.7.1
* Upgraded to Realm Sync 2.1.0.

2.0.0 Release notes (2017-10-17)
Expand Down
7 changes: 7 additions & 0 deletions src/js_realm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,9 @@ bool RealmClass<T>::get_realm_config(ContextType ctx, size_t argc, const ValueTy
static const String in_memory_string = "inMemory";
ValueType in_memory_value = Object::get_property(ctx, object, in_memory_string);
if (!Value::is_undefined(ctx, in_memory_value) && Value::validated_to_boolean(ctx, in_memory_value, "inMemory")) {
if (config.force_sync_history || config.sync_config) {
throw std::invalid_argument("Options 'inMemory' and 'sync' are mutual exclusive.");
}
config.in_memory = true;
}

Expand Down Expand Up @@ -624,6 +627,10 @@ bool RealmClass<T>::get_realm_config(ContextType ctx, size_t argc, const ValueTy
static const String migration_string = "migration";
ValueType migration_value = Object::get_property(ctx, object, migration_string);
if (!Value::is_undefined(ctx, migration_value)) {
if (config.force_sync_history || config.sync_config) {
throw std::invalid_argument("Options 'migration' and 'sync' are mutual exclusive.");
}

FunctionType migration_function = Value::validated_to_function(ctx, migration_value, "migration");

if (config.schema_mode == SchemaMode::ResetFile) {
Expand Down
31 changes: 31 additions & 0 deletions tests/js/session-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,37 @@ module.exports = {
TestCase.assertNull(realm.syncSession);
},

async testRealmInvalidSyncConfiguration1() {
const config = {
sync: true,
inMemory: true,
};

return new Promise((resolve, reject) => {
return Realm.open(config)
.then(_ => reject())
.catch(_ => resolve());
});
},

async testRealmInvalidSyncConfiguration2() {
const partition = Utils.genPartition();
let credentials = Realm.Credentials.anonymous();
let app = new Realm.App(appConfig);


return new Promise((resolve, reject) => {
return app.logIn(credentials)
.then(user => {
let config = getSyncConfiguration(user, partition);
config.migration = (_) => { /* empty function */ };
return Realm.open(config)
.then(_ => reject())
.catch(_ => resolve());
});
});
},

testRealmOpen() {
if (!isNodeProcess) {
return;
Expand Down