Sha256: 263cf7451e59da7af76ffa92086bac126d71a0adef5f23b64d5c8e0d6ccc1dad

Contents?: true

Size: 884 Bytes

Versions: 2

Compression:

Stored size: 884 Bytes

Contents

module Strain
  class Variant < ActiveRecord::Base

    has_many :articles, inverse_of: :variant

    validates_presence_of :name
    validates_presence_of :schema
    validate :schema_must_be_valid
    validate :schema_root_must_be_object
    validates :name, uniqueness: { case_sensitive: false }

    private

    def schema_must_be_valid
      meta_schema = JSON.load(
        File.read("#{::Strain::Engine.root}/lib/json_schema/draft_4_meta_schema.json")
      )
      unless JSON::Validator.validate(meta_schema, self.schema)
        errors.add(:schema, "must be a valid Draft 4 JSON Variant.")
      end
    end

    def schema_root_must_be_object
      return if schema.blank?
      #TODO: check for schema w/o 'type' key set at all
      unless schema.fetch("type") == "object"
        errors.add(:schema, "must have an object as root element.")
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
strain-0.0.2 app/models/strain/variant.rb
strain-0.0.1 app/models/strain/variant.rb