Module: Granola::Serializer::Schema

Included in:
Granola::Serializer
Defined in:
lib/granola/schema.rb

Overview

This module adds support for JSON Schema to Granola serializers. You should define the schema for each serializer

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


9
10
11
# File 'lib/granola/schema.rb', line 9

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#valid?Boolean

Public: Validate this serializer against it's own schema.

Returns Boolean indicating whether it's valid. In case it's not, it populates the `#validation_errors` Array.

Returns:

  • (Boolean)

26
27
28
29
30
31
32
# File 'lib/granola/schema.rb', line 26

def valid?
  validation_errors.clear
  validation_errors.concat(
    JSON::Validator.fully_validate(self.class.schema, attributes)
  )
  validation_errors.empty?
end

#validation_errorsObject

Public: List any errors that arose from validating this against its schema.

See #valid?

Returns an Array.


40
41
42
# File 'lib/granola/schema.rb', line 40

def validation_errors
  @validation_errors ||= []
end