Class: Graphiti::RequestValidators::Validator
- Defined in:
- lib/graphiti/request_validators/validator.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Instance Method Summary collapse
- #deserialized_payload ⇒ Object
-
#initialize(root_resource, raw_params, action) ⇒ Validator
constructor
A new instance of Validator.
- #validate ⇒ Object
- #validate! ⇒ Object
Constructor Details
#initialize(root_resource, raw_params, action) ⇒ Validator
Returns a new instance of Validator.
6 7 8 9 10 11 |
# File 'lib/graphiti/request_validators/validator.rb', line 6 def initialize(root_resource, raw_params, action) @root_resource = root_resource @params = normalized_params(raw_params) @errors = Graphiti::Util::SimpleErrors.new(raw_params) @action = action end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
4 5 6 |
# File 'lib/graphiti/request_validators/validator.rb', line 4 def errors @errors end |
Instance Method Details
#deserialized_payload ⇒ Object
45 46 47 |
# File 'lib/graphiti/request_validators/validator.rb', line 45 def deserialized_payload @deserialized_payload ||= Graphiti::Deserializer.new(@params) end |
#validate ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/graphiti/request_validators/validator.rb', line 13 def validate # Right now, all requests - even reads - go through the validator # In the future these should have their own validation logic, but # for now we can just bypass return true unless @params.has_key?(:data) resource = @root_resource if @params[:data].has_key?(:type) if ( = deserialized_payload.[:type].try(:to_sym)) if @root_resource.type != && @root_resource.polymorphic? resource = @root_resource.class.resource_for_type().new end end typecast_attributes(resource, deserialized_payload.attributes, @action, deserialized_payload.[:payload_path]) process_relationships(resource, deserialized_payload.relationships, deserialized_payload.[:payload_path]) else errors.add(:"data.type", :missing) end errors.blank? end |
#validate! ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/graphiti/request_validators/validator.rb', line 37 def validate! unless validate raise @error_class || Graphiti::Errors::InvalidRequest, errors end true end |