Skip to content

Commit 2deaf78

Browse files
committed
Fixed up mistakes in the readme example code
1 parent a8dc2ec commit 2deaf78

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

README.md

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# JsonValidator
22

3+
[![Gem Version](http://img.shields.io/gem/v/json_validator.svg)](https://rubygems.org/gems/json_validator)
34
[![Build Status](http://img.shields.io/travis/iainbeeston/json_validator/master.svg)](https://travis-ci.org/iainbeeston/json_validator)
45
[![Code Climate](http://img.shields.io/codeclimate/github/iainbeeston/json_validator.svg)](https://codeclimate.com/github/iainbeeston/json_validator)
5-
[![Gem Version](http://img.shields.io/gem/v/json_validator.svg)](https://rubygems.org/gems/json_validator)
66

77

88
JsonValidator is an ActiveModel validator that validates any hash field against [JSONSchema](http://json-schema.org), returning errors in the model's own `errors` attribute.
@@ -18,7 +18,7 @@ If you're using Ruby on Rails and ActiveRecord, add a validation to your model l
1818
~~~ruby
1919
class Foo < ActiveRecord::Base
2020
validates :bar, json: {
21-
schema: File.read(JSON.parse('foo_schema.json'))
21+
schema: JSON.parse(File.read('foo_schema.json'))
2222
}
2323
end
2424
~~~
@@ -27,7 +27,7 @@ And you have a schema file (ie. `foo_schema.json`) like this:
2727

2828
~~~json
2929
{
30-
"$schema": "http://json-schema.org/schema#",
30+
"$schema": "http://json-schema.org/draft-04/schema#",
3131
"title": "Universal spoons schema",
3232
"properties": {
3333
"handleSize": {
@@ -39,6 +39,14 @@ And you have a schema file (ie. `foo_schema.json`) like this:
3939
}
4040
~~~
4141

42-
Then whenever an instance of `Foo` is saved, `Foo.bar` (assumed to be a hash) will be validated against the JSON schema specified. In this case, `Foo.new(bar: { handleSize: -10 })` would be invalid, but `Foo.new(bar: { handleSize: 10 })` would be valid.
42+
Then whenever an instance of `Foo` is saved, `Foo.bar` (assumed to be a hash) will be validated against the JSON schema specified. So, for example:
43+
44+
~~~ruby
45+
46+
f = Foo.new(bar: { handleSize: -10 })
47+
f.valid? # false
48+
f.errors.full_messages # ["Bar is invalid (the property '#/handleSize' did not have a minimum value of 0, inclusively)"]
49+
50+
~~~
4351

4452
The attribute being validated can be either a hash or a string (which will be parsed as JSON). The schema can be either a hash or a Proc that returns a hash (if you'd like to decide on the schema at runtime), and there's no reason why you could not load your schema from a .json file.

0 commit comments

Comments
 (0)