Example for JSON Schema in Rails?

484 views
Skip to first unread message

Oliver Kuhn

unread,
Mar 18, 2015, 8:50:55 AM3/18/15
to ruby-jso...@googlegroups.com
Hi,

is out there any example, how to use JSON Schema in Rails? Cant find anything. 

Interesting for me: 

- Where to store SchemaFiles
- how to call / and load schema fieles
- do i need a helper or not...

thx in advance

Jonas Peschla

unread,
Mar 18, 2015, 12:42:26 PM3/18/15
to ruby-jso...@googlegroups.com
Hi Oliver,

I am not aware of a guide or best practices.
In our project we use json-schema to validate the payload submitted to our JSON API.
We have a base class for all API controllers which provides a parse_json and a validate_json method for guess what:

    def parse_json
      begin
        @json_data = JSON.parse(request.body.string)
      rescue
        render json: {errors: ["Unable to parse JSON: #{request.body.string}"]}.to_json, status: 400
      end
    end

    def validate_json
      errors = JSON::Validator.fully_validate(schema, @json_data, {clear_cache: false})
      if !errors.empty?
        logger.info "External API JSON Validation Error: #{errors}, #{@json_data}"
        render json: {errors: errors}.to_json, status: 422
      end
    end

The controllers inheriting from the base controller use a before_action callback to indicate when to parse and validate:
before_action :parse_json, :validate_json, only: [:create]
For validate_json to work, each controller must override/define  a schema method which returns the schema to be used for validation.

And then we have an initializer in config/initializers/json_schema.rb which uses locally cached schemas instead of fetching them (plus to register custom validators):
JSON_SCHEMAS = Dir.glob('lib/assets/schemas/*.json').reduce({}) do |memo, path|
  schema = path.match(/\/([^\/\s]+)\.schema\.json\z/)[1]
  memo.tap { |m| m[schema] = File.read(path) }
end

class CustomSchemaReader < JSON::Schema::Reader
  def read_uri(uri)
    schema = uri.path.match(/www\.example\.com\/([^\/\s]+)\z/)[1]
    JSON_SCHEMAS[schema]
  end
end

JSON::Validator.schema_reader = CustomSchemaReader.new(:accept_uri => true, :accept_file => !Rails.env.production?)

For us this works. If you are looking for using json-schema as a validator for serialized hashes in ActiveModel attributes you might be
interested in Iains gem for that: https://github.com/iainbeeston/json_validator

Regards
Jonas
--
You received this message because you are subscribed to the Google Groups "Ruby JSON Schema Library" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-json-sche...@googlegroups.com.
To post to this group, send email to ruby-jso...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-json-schema/ec5f2fce-7050-4655-b96b-54fb07092c44%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Oliver Kuhn

unread,
Mar 18, 2015, 12:54:34 PM3/18/15
to ruby-jso...@googlegroups.com
Thank you so much. Seems to solve my problem. i will try This in the next days.

Oliver Kuhn

unread,
Apr 2, 2015, 3:45:49 AM4/2/15
to ruby-jso...@googlegroups.com, git...@peschla.net
Only to give a status: i am using https://github.com/iainbeeston/json_validator for now... its very simple and all i need... thank you for this hint.

Iain Beeston

unread,
Apr 2, 2015, 5:11:52 AM4/2/15
to ruby-jso...@googlegroups.com
I'm glad you find it useful. Please let me know (via github issues) if you have any feedback or suggestions to improve it.

jenna....@gmail.com

unread,
Apr 11, 2016, 4:12:54 AM4/11/16
to Ruby JSON Schema Library, git...@peschla.net
Thanks! This was useful for me too!
Reply all
Reply to author
Forward
0 new messages