Sha256: 6864cfb927803b0c036d3dc3a61da5e06be3eb19847f1cbe1d2b2c9c7e2f2bdd

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

module ActiveModel
  class Serializer
    module Validator
      module Mixin
        extend ActiveSupport::Concern

        included do
          attr_reader :errors
        end

        # Return whether the serializer output is valid
        def valid?
          @errors = self.class.valid_against_schema?(self.class.json_schema, self)
          @errors.empty?
        end

        module ClassMethods
          # Set the JSON schema to use for this class
          def json_schema(value = nil)
            @_json_schema ||= begin
              superclass.json_schema if superclass.respond_to?(:json_schema)
            end

            return @_json_schema unless value

            if !value.is_a?(String) && !value.is_a?(Hash)
              raise InvalidSchemaError.new('Schema must be a path to a file or a Hash.')
            elsif value.is_a?(String) && !File.exists?(value)
              raise InvalidSchemaError.new('Schema file does not exist.')
            else
              @_json_schema = value
            end
          end

          # Validate the rendered data against a JSON schema file and
          # return an errors array
          def valid_against_schema?(schema, serializer)
            JSON::Validator.fully_validate(schema, serializer.to_json)
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_model_serializers_validator-1.2 lib/active_model/serializer/validator/mixin.rb
active_model_serializers_validator-1.1 lib/active_model/serializer/validator/mixin.rb