Skip to content

Commit da7881c

Browse files
committed
Improve Validations documentation
First, document the ActiveResource::Validations module's built-in behavior for parsing JSON and XML responses that adhere to Active Model error structures, namely `{"errors": { … }` for JSON responses and `<errors><error>…</error></errors>` in XML responses. Next, outline the Active Resource's compatibility with `ActiveModel::Validations` as well as the specialized behavior provided by `ActiveResource::Validations`.
1 parent 62b941e commit da7881c

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,39 @@ Person.delete(2) # => true
265265
Person.exists?(2) # => false
266266
```
267267

268+
### Validations
269+
270+
Resources validate their attributes with Active Model validations. When a
271+
resource is invalid, it will not issue a request:
272+
273+
```ruby
274+
class Post < ActiveResource::Base
275+
self.site = "http://blog.io"
276+
277+
validates :title, presence: true
278+
end
279+
280+
post = Post.create(title: "") # does not issue POST http://blog.io/posts.json request
281+
post.valid? # => false
282+
post.errors[:title] # => ["can't be blank"]
283+
```
284+
285+
When a resource is valid but the server responds with an error, Active Resource
286+
will add error messages in the style of Active Model validations:
287+
288+
```ruby
289+
class Post < ActiveResource::Base
290+
self.site = "http://blog.io"
291+
292+
validates :title, presence: true
293+
end
294+
295+
post = Post.create(title: "This Post is not Unique!") # issues a POST http://blog.io/posts.json request
296+
# => {"errors":{"title":"is taken"}}
297+
post.valid? # => false
298+
post.errors[:title] # => ["is taken"]
299+
```
300+
268301
### Associations
269302

270303
Relationships between resources can be declared using the standard association syntax

lib/active_resource/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ module ActiveResource
322322
# # {"errors":{"first":["cannot be empty"]}}
323323
# #
324324
#
325-
# ryan.errors.invalid?(:first) # => true
325+
# ryan.errors.include?(:first) # => true
326326
# ryan.errors.full_messages # => ['First cannot be empty']
327327
#
328328
# Learn more about Active Resource's validation features in the ActiveResource::Validations documentation.

lib/active_resource/validations.rb

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,56 @@ def from_xml(xml, save_cache = false)
7676
# Consider a Person resource on the server requiring both a +first_name+ and a +last_name+ with a
7777
# <tt>validates_presence_of :first_name, :last_name</tt> declaration in the model:
7878
#
79-
# person = Person.new(:first_name => "Jim", :last_name => "")
79+
# person = Person.new(first_name: "Jim", last_name: "")
8080
# person.save # => false (server returns an HTTP 422 status code and errors)
8181
# person.valid? # => false
8282
# person.errors.empty? # => false
8383
# person.errors.count # => 1
8484
# person.errors.full_messages # => ["Last name can't be empty"]
85-
# person.errors[:last_name] # => ["can't be empty"]
85+
# person.errors[:last_name] # => ["can't be empty"]
8686
# person.last_name = "Halpert"
8787
# person.save # => true (and person is now saved to the remote service)
8888
#
89+
# Consider a POST /people.json request that results in a 422 Unprocessable
90+
# Content response with the following +application/json+ body:
91+
#
92+
# {
93+
# "errors": {
94+
# "base": ["Something went wrong"],
95+
# "address": ["is invalid"]
96+
# }
97+
# }
98+
#
99+
# By default, Active Resource will automatically load errors from JSON response
100+
# objects that have a top-level +"errors"+ key that maps attribute names to arrays of
101+
# error message strings:
102+
#
103+
# person = Person.new(first_name: "Jim", last_name: "Halpert", address: "123 Fake Street")
104+
# person.save # => false (server returns an HTTP 422 status code and errors)
105+
# person.valid? # => false
106+
# person.errors[:base] # => ["Something went wrong"]
107+
# person.errors[:address] # => ["is invalid"]
108+
#
109+
# Consider a POST /people.xml request that results in a 422 Unprocessable
110+
# Content response with the following +application/xml+ body:
111+
#
112+
# <errors>
113+
# <error>Something went wrong</error>
114+
# <error>Address is invalid</error>
115+
# </errors>
116+
#
117+
# By default, Active Resource will automatically load errors from XML response
118+
# documents that have a top-level +<errors>+ element that contains +<error>+
119+
# children that have error message content. When an error message starts with
120+
# an attribute name, Active Resource will automatically infer that attribute
121+
# name and add the message to the attribute's errors. When an attribute name
122+
# cannot be inferred, the error message will be added to the +:base+ errors:
123+
#
124+
# person = Person.new(first_name: "Jim", last_name: "Halpert", address: "123 Fake Street")
125+
# person.save # => false (server returns an HTTP 422 status code and errors)
126+
# person.valid? # => false
127+
# person.errors[:base] # => ["Something went wrong"]
128+
# person.errors[:address] # => ["Address is invalid"]
89129
module Validations
90130
extend ActiveSupport::Concern
91131
include ActiveModel::Validations

0 commit comments

Comments
 (0)