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

No way to error on unknown properties #220

Open
imoverclocked opened this issue Jun 28, 2017 · 5 comments
Open

No way to error on unknown properties #220

imoverclocked opened this issue Jun 28, 2017 · 5 comments

Comments

@imoverclocked
Copy link

Given a class:

class SongRepresenter < Representable::Decorator
  include Representable::JSON

  property :title
  property :track
end

and some data to instantiate it with:

{"title": "Foggy Mountain Breakdown", "track": 2, "genre": "bluegrass"}

There is no way to discover that genre is about to be harshly ignored or dropped on the ground. Another similar problem with slightly different data:

{"title": "Foggy Mountain Breakdown", "trach": 2}

since trach != track, our optional attribute is also silently dropped without warning.

@imoverclocked
Copy link
Author

[4] pry(main)> class Song
[4] pry(main)*   attr_accessor :title
[4] pry(main)*   attr_accessor :track
[4] pry(main)*   attr_accessor :foo
[4] pry(main)* end
=> nil
[5] pry(main)> puts SongRepresenter.new(Song.new()).from_json(%{ {"title":"Roxanne", "foo": "bar", "baz": "quux"} }).to_json
{"title":"Roxanne"}
=> nil

@imoverclocked imoverclocked changed the title No way to exclude unknown properties No way to error on unknown properties Jun 28, 2017
@apotonick
Copy link
Member

Yes, that's per design! Representable is a document mapper and not a form object.

We're actually thinking about allowing the detection of that, since it will also be very helpful in Reform.

@apotonick
Copy link
Member

apotonick commented Jun 28, 2017

The problem is that this might appear very simple with hashes (as in, find all keys in the incoming doc, compare to what we have), however, with XML documents as an example, this gets much trickier. I am wondering if that should probably be some Reform-specific feature, instead. 🤔

@imoverclocked
Copy link
Author

Yeah, I was browsing through the source to see if there would be any quick+clean way to patch this in but nothing popped out at me. The current design feels pretty heavy towards not having this feature.

Some extremely pragmatic part of me is tempted to just serialize/unserialize and compare the result to see if everything is accounted for. The rest of me is beating the pragmatic part of me down and hoping for a better solution.

@apotonick
Copy link
Member

I understand - and I recommend you to check out how we handle that in Trailblazer or directly in Reform, where the form object applies a deserializing representer to itself. The representer per design has no knowledge about validations of any kind, but just parses values it knows to the form. The form then says "that's fine" or marks errors.

You can also override from_hash in every Representer and do the check there. The list of properties can be retrieved via Representer::definitions.

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