Sha256: 74f7630cddeca27152f66d829a085df9436d09f0e8b389fbe2600aead5c78773

Contents?: true

Size: 728 Bytes

Versions: 1

Compression:

Stored size: 728 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].class_name.vocabularies.keys
          record.errors.add :base, "value `#{term} for `#{attribute}` property is not a term in a controlled vocabulary #{vocabularies.join(', ')}"
        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.1.0 lib/linked_vocabs/validators/property_validator.rb