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