Sha256: 6e19ef93fa21f4f6638846e8f404e717229f6be322413d7f5a54c5788b885e04

Contents?: true

Size: 822 Bytes

Versions: 1

Compression:

Stored size: 822 Bytes

Contents

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

        # Validate the rendered data against a JSON schema file
        def valid_against_schema?(schema)
          JSON::Validator.validate(schema, self.to_json)
        end

        # Return whether the serializer output is valid
        def valid?
          valid_against_schema?(self.class.json_schema)
        end

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

            return @_json_schema unless value
            @_json_schema = value
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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