Sha256: a10b122b520e6694fa221da9fedb3253a9d128aab91ccef69dde6b06c404113a

Contents?: true

Size: 657 Bytes

Versions: 2

Compression:

Stored size: 657 Bytes

Contents

require 'couch/generators/named_base'

module Couch::Generators
  class ValidationGenerator < NamedBase
    def inject_validations
      inject_into_file "validate_doc_update.js", model_validations, :after => "function (newDoc, oldDoc, userCtx) {\n"
    end

    protected

    def model_validations
      str = <<-STR
  if(newDoc.type == '#{model_name}') {
    // validations for #{model_name}
      STR

      attributes.each do |attribute|
        str << <<-STR
    if (typeof(newDoc['#{attribute}']) === 'undefined') {
      throw({ forbidden: '#{attribute} is required' });
    }
        STR
      end

      str << "  }\n"
      str
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
couch-0.0.4 lib/couch/generators/validation/validation_generator.rb
couch-0.0.3 lib/couch/generators/validation/validation_generator.rb