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

Feature: Automatically validate the type when setting a value instead of casting it #203

Open
sjieg opened this issue Dec 30, 2020 · 0 comments
Labels

Comments

@sjieg
Copy link

sjieg commented Dec 30, 2020

Hello, first of all, good stuff with this gem! Very useful making global app settings dynamic, but also to quickly create a single variable to "remember" something. Props to all the contributors and maintainer!

So I might be wrong, because I just started using this gem, but it seems that the type set for a field is being cast:

# field :foo, type: :integer
Setting.foo = 'hello' # => 0

What do you think about making it so that if the type is set and a different type is given, an error is thrown?

# field :foo, type: :integer
Setting.foo = 'hello' # InvalidTypeError - Got type String, expected type Integer
# field :bar, type :date_time
Setting.bar = '2020-12-30' # InvalidTypeErrror - Got type String, expected type DateTime

Personally, I feel like this will make it more robust to use because:

  • Validate is only required for more complex validations.
  • It prevents giving a setting an incorrect value.
  • It makes calling a setting more reliable and you know it will return the expected type.

Like I said, I just started using the gem, so I might be overlooking something obvious.

WDYT? Could this be a feature for v3? :)

@huacnlee huacnlee added the Idea label Jun 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants