Validate the default key name
sindresorhus opened this issue · 4 comments
In the next major version, we should strictly validate the key name to ensure users don't accidentally make the key's unable to be observed.
The rules are as follows:
The key must be ASCII, not start with
@
, and cannot contain a dot (.
).
I think we might have three ways to do it.
- Mark
Defaults.Key
initializer asthrows
:
- pros:
- Can throw an error early when user input an invalid name.
- cons:
- Need to add
try
when initializeDefaults.Key
.
- Need to add
- Add an assertion in
Defaults.Key
initializer:
- pros:
- No need to add
try
.
- No need to add
- cons:
- Assertion is not available in release mode.
- Mark
KVO
related function(ex.Defaults.observe
) asthrows
:
- pros:
- No need to add
try
.
- No need to add
Only 2. is really feasible.
The question is, do assertions work when they are in a package and you build your app in debug mode.
Actually, a better solution could be to show a warning in Xcode using this hack: https://github.com/pointfreeco/swift-composable-architecture/blob/main/Sources/ComposableArchitecture/Internal/RuntimeWarnings.swift
Actually, a better solution could be to show a warning in Xcode using this hack: pointfreeco/swift-composable-architecture@main/Sources/ComposableArchitecture/Internal/RuntimeWarnings.swift
This looks awesome! Will try to implement this in Defaults
.