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

Strict mode and empty values #335

Open
samstickland opened this issue Aug 13, 2015 · 1 comment
Open

Strict mode and empty values #335

samstickland opened this issue Aug 13, 2015 · 1 comment

Comments

@samstickland
Copy link

samstickland commented Aug 13, 2015

I feel like I must be missing something obvious. Using Virtus as to support an HTML form must be one of the intended use cases, no?

I have some fields, some with custom rails validations, some optional. Out of the box Virtus will store the original string value when coercion fails. So if someone puts 'aaa' into my DateTime field it now stores 'aaa'. I can't enable strict mode because that doesn't seem to allow nil values for my optional values.

So it seems like the only solution is to add additional rails validations to check the stored datatypes? At which point it seems like I'm not really using virtus at all? I could do this with "include ActiveModel::Model" and regular attr_accessor statements.

Am I missing something or have I misunderstood the intended use of this gem?

@grantspeelman
Copy link

I was thinking the same thing and considering how would the interface for something like this look.

Maybe something like this:

class NewFeatureRequest
  include Virtus.model(store_coercion_errors: true)

  attribute :title, String
  attribute :date_requested, Date
end

feature = NewFeatureRequest.new('title' => 'Store Failed Coercions', 'date_requested' => 'Not Sure')
expect(feature.date_requested).to eq('Not Sure')
expect(feature.coercion_errors['date_requested']).to be_kind_of(Virtus::CoercionError)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants