sindresorhus/Defaults

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.

  1. Mark Defaults.Key initializer as throws:
  • pros:
    1. Can throw an error early when user input an invalid name.
  • cons:
    1. Need to add try when initialize Defaults.Key.
  1. Add an assertion in Defaults.Key initializer:
  • pros:
    1. No need to add try.
  • cons:
    1. Assertion is not available in release mode.
  1. Mark KVO related function(ex. Defaults.observe) as throws:
  • pros:
    1. No need to add try.

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: pointfreeco/swift-composable-architecture@main/Sources/ComposableArchitecture/Internal/RuntimeWarnings.swift

This looks awesome! Will try to implement this in Defaults.