validates_xml 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +11 -0
- data/lib/validates_xml.rb +48 -0
- data/rails/init.rb +2 -0
- metadata +70 -0
data/README.textile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
h2. ValidatesXml
|
2
|
+
|
3
|
+
Provides validates_xml method allowing to validate xml correctness of ActiveRecord attributes.
|
4
|
+
|
5
|
+
h2. Acknowledgment
|
6
|
+
|
7
|
+
It's just a repacking as gem of plugin published on http://code.google.com/p/validates-xml/ by johnwulff
|
8
|
+
|
9
|
+
h2. License
|
10
|
+
|
11
|
+
MIT License
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module ValidatesXML
|
2
|
+
def self.validates_xml(xml, options={})
|
3
|
+
begin
|
4
|
+
REXML::Document.new("<base>#{xml}</base>")
|
5
|
+
rescue REXML::ParseException => ex
|
6
|
+
return "is not valid xml"
|
7
|
+
end
|
8
|
+
|
9
|
+
return nil
|
10
|
+
end
|
11
|
+
|
12
|
+
module Validations
|
13
|
+
def validates_xml(*attr_names)
|
14
|
+
configuration = { :message => I18n.translate('activerecord.errors.messages')[:invalid],
|
15
|
+
:on => :save,
|
16
|
+
:with => nil }
|
17
|
+
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
|
18
|
+
validates_each(attr_names, configuration) do |record, attr_name, value|
|
19
|
+
begin
|
20
|
+
REXML::Document.new("<base>#{value}</base>")
|
21
|
+
rescue REXML::ParseException => ex
|
22
|
+
record.errors.add(attr_name,
|
23
|
+
"is not valid xml")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
if defined?(ActiveModel)
|
31
|
+
class XmlFormatValidator < ActiveModel::EachValidator
|
32
|
+
def validate_each(record, attribute, value)
|
33
|
+
err = ValidatesXML::validates_xml(value, options)
|
34
|
+
record.errors[attribute] << err unless err.nil?
|
35
|
+
record.errors[attribute].flatten!
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
module ActiveModel::Validations::HelperMethods
|
40
|
+
def validates_xml(*attr_names)
|
41
|
+
validates_with XmlFormatValidator, _merge_attributes(attr_names)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
else
|
45
|
+
class ActiveRecord::Base
|
46
|
+
extend ValidatesXML::Validations
|
47
|
+
end
|
48
|
+
end
|
data/rails/init.rb
ADDED
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validates_xml
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 1.0.3
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Lukasz Piestrzeniewicz
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2009-08-29 00:00:00 +02:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: activerecord
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 2.0.0
|
25
|
+
type: :runtime
|
26
|
+
version_requirements: *id001
|
27
|
+
description: Provides validates_xml method allowing to validate xml correctness of ActiveRecord attributes.
|
28
|
+
email:
|
29
|
+
- bragi@ragnarson.com
|
30
|
+
executables: []
|
31
|
+
|
32
|
+
extensions: []
|
33
|
+
|
34
|
+
extra_rdoc_files:
|
35
|
+
- README.textile
|
36
|
+
files:
|
37
|
+
- lib/validates_xml.rb
|
38
|
+
- rails/init.rb
|
39
|
+
- README.textile
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: http://github.com/bragi/validates_xml
|
42
|
+
licenses: []
|
43
|
+
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options:
|
46
|
+
- --main
|
47
|
+
- README.textile
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project: validates_xml
|
65
|
+
rubygems_version: 1.5.2
|
66
|
+
signing_key:
|
67
|
+
specification_version: 2
|
68
|
+
summary: Rails helper for validating xml attributes.
|
69
|
+
test_files: []
|
70
|
+
|