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

Prevent model from being created if there is any unknown attribute #337

Open
tanapoln opened this issue Aug 17, 2015 · 3 comments
Open

Prevent model from being created if there is any unknown attribute #337

tanapoln opened this issue Aug 17, 2015 · 3 comments

Comments

@tanapoln
Copy link

tanapoln commented Aug 17, 2015

For example,

class RegisterModel
    include Virtus.model(unknown: false)
    attribute :name, String
end

RegisterModel.new(name: "Ben Smith", mal_input: "---") #this should fail and raise error

@neumachen
Copy link

You can just override the initializer and do the logic there.

@fr33z3
Copy link

fr33z3 commented Dec 15, 2015

I don't think that the behaviour when model raises exception when you are trying to get an instance of it is useful. Sounds like anti-pattern. Instead of that you should probably define some method named valid? to check if model is valid or not and then manually raise an exception.

@zhulik
Copy link

zhulik commented Apr 28, 2017

Sorry for necroposting, but it is default behavior in ActiveRecord and can really save you from typos in you code. I think optional mode with restricted unknown fields is a good idea.

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

4 participants