Sha256: 98302d01c4db26462bb57b08db3227935d3a7b8ee9698fe9d170a1fa258ca8ab

Contents?: true

Size: 926 Bytes

Versions: 1

Compression:

Stored size: 926 Bytes

Contents

require 'active_model'

module LinkedVocabs::Validators
  class PropertyValidator < ActiveModel::EachValidator
    def validate_each(record, attribute, values)
      values.each do |v|
        unless v.try(:in_vocab?)
          term = v.try(:rdf_subject) || v
          vocabularies = record.class.properties[attribute.to_s].class_name.vocabularies.keys
          if term.is_a? RDF::URI
            record.errors.add :base, "value `#{term}' for `#{attribute}' property is not a term in a controlled vocabulary #{vocabularies.join(', ')}"
          else
            record.errors.add :base, "`#{term}' for `#{attribute}' property is expected to be a URI, but it is a #{term.class}"
          end
        end
      end
    end
  end
end

module ActiveModel::Validations::HelperMethods
  def validates_vocabulary_of(*attr_names)
    validates_with LinkedVocabs::Validators::PropertyValidator, :attributes => attr_names
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
linked_vocabs-0.3.1 lib/linked_vocabs/validators/property_validator.rb