Sha256: 106d0fcee49798428951125f6a3c9ac3e6ca29b46c6741b8e05d84a7bd8e59fe
Contents?: true
Size: 1.12 KB
Versions: 1
Compression:
Stored size: 1.12 KB
Contents
require 'json_validator_meta' # activemodel validators have an undeclared dependency on the hash extensions from active support require 'active_support/core_ext/hash' require 'active_model/validator' require 'json' require 'json-schema' class JsonValidator < ActiveModel::EachValidator VERSION = JsonValidatorMeta::VERSION def validate_each(record, attribute, value) raw_errors = JSON::Validator.fully_validate(schema(record), json(value)) translated_errors = raw_errors.map do |e| translate_message(e) end translated_errors.each do |e| record.errors.add(attribute, e) end end def schema(record) if options[:schema].respond_to?(:call) options[:schema].call(record) else options[:schema].to_hash end end def json(value) if value.respond_to?(:to_hash) value.to_hash else JSON.parse(value.to_s) end end def translate_message(msg) # remove suffix msg.gsub!(/ in schema .*$/, '') end def check_validity! fail ArgumentError, :'Schema unspecified. Please specify :schema as either a Proc or a hash' if options[:schema].nil? end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
json_validator-0.0.1 | lib/json_validator.rb |