validates_xml_of 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +66 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/config/locales/en.yml +5 -0
- data/examples/xsds/Schema.xsd +28 -0
- data/lib/validates_xml_of.rb +44 -0
- data/lib/validates_xml_of/active_model.rb +29 -0
- data/lib/validates_xml_of/matchers/validate_xml_of.rb +82 -0
- data/lib/validates_xml_of/railtie.rb +7 -0
- data/lib/validates_xml_of/validator.rb +91 -0
- data/lib/validates_xml_of/version.rb +3 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/validates_xml_of/active_model_spec.rb +84 -0
- data/spec/validates_xml_of/matchers/validate_xml_of_spec.rb +26 -0
- data/spec/validates_xml_of/validator_spec.rb +75 -0
- data/spec/validates_xml_of_spec.rb +58 -0
- data/validates_xml_of.gemspec +28 -0
- metadata +157 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f7d12165f68e7fc627eda45062330ecb1dad0ad2
|
4
|
+
data.tar.gz: 0f5721a61442208022a78504831156bb6856ff78
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 00b5ff12ee67dea442e09ad878bcebf0e180e1eb24b0c1660a18f6a6339c10852f84311f2eb51ffaf02218cfa514aff3456f2a07a78d8edd61de4595a17727d9
|
7
|
+
data.tar.gz: 5cf7c0b455579a49352c69a52a808132d697f0af941b5be21d393c835718c2e01b09d479c074fb1cae8c01f9eb63bf4ac708dc3c1e719f5423089252e547fde7
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at contato@brunoarueira.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Bruno Arueira
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# ValidatesXmlOf
|
2
|
+
|
3
|
+
Validates if a given string contains a valid xml and validates based on defined
|
4
|
+
schema too.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'validates_xml_of'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install validates_xml_of
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
### Rails
|
25
|
+
|
26
|
+
If you do not want to use xsd:
|
27
|
+
|
28
|
+
```
|
29
|
+
class Post < ActiveRecord::Base
|
30
|
+
...
|
31
|
+
|
32
|
+
validates :content, xml: true
|
33
|
+
|
34
|
+
...
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
And if you want to use xsd, you will need to define an initializer to setup a path for your schemas:
|
39
|
+
|
40
|
+
```
|
41
|
+
ValidatesXmlOf.setup do |configure|
|
42
|
+
config.schema_paths = "lib/xsds"
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
After, you need to set your validation to specify what schema you are looking for:
|
47
|
+
|
48
|
+
```
|
49
|
+
class Post < ActiveRecord::Base
|
50
|
+
...
|
51
|
+
# The schema must be in lib/xsds/Schema.xsd
|
52
|
+
validates :content, xml: { schema: 'Schema' }
|
53
|
+
...
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
***PS:*** Coming soon, I will implement a generator to create this initializer
|
58
|
+
|
59
|
+
## Contributing
|
60
|
+
|
61
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/brunoarueira/validates_xml. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
62
|
+
|
63
|
+
## License
|
64
|
+
|
65
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
66
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "validates_xml"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
<?xml version="1.0" encoding="ISO-8859-1"?>
|
2
|
+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
|
3
|
+
<xs:element name="Bar">
|
4
|
+
<xs:complexType>
|
5
|
+
<xs:sequence>
|
6
|
+
<xs:element name="foo">
|
7
|
+
<xs:complexType>
|
8
|
+
<xs:sequence>
|
9
|
+
<xs:element name="foo_bar">
|
10
|
+
<xs:complexType>
|
11
|
+
<xs:attribute name="value" use="required">
|
12
|
+
<xs:simpleType>
|
13
|
+
<xs:restriction base="xs:string">
|
14
|
+
<xs:minLength value="1" />
|
15
|
+
<xs:maxLength value="10" />
|
16
|
+
</xs:restriction>
|
17
|
+
</xs:simpleType>
|
18
|
+
</xs:attribute>
|
19
|
+
</xs:complexType>
|
20
|
+
</xs:element>
|
21
|
+
</xs:sequence>
|
22
|
+
</xs:complexType>
|
23
|
+
</xs:element>
|
24
|
+
</xs:sequence>
|
25
|
+
</xs:complexType>
|
26
|
+
</xs:element>
|
27
|
+
</xs:schema>
|
28
|
+
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
|
3
|
+
module ValidatesXmlOf
|
4
|
+
autoload :VERSION, "validates_xml_of_of/version"
|
5
|
+
|
6
|
+
class << self
|
7
|
+
attr_accessor :schema_paths
|
8
|
+
|
9
|
+
def load_i18n_locales
|
10
|
+
require 'i18n'
|
11
|
+
I18n.load_path += Dir.glob(File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'locales', '*.yml')))
|
12
|
+
end
|
13
|
+
|
14
|
+
DEFAULT_MESSAGE = 'does not appear to be a valid xml'
|
15
|
+
DEFAULT_SCHEMA_MESSAGE = 'does not appear to be a valid xml based on schema informed'
|
16
|
+
ERROR_MESSAGE_I18N_KEY = :invalid_xml
|
17
|
+
ERROR_SCHEMA_MESSAGE_I18N_KEY = :invalid_xml_based_on_schema
|
18
|
+
|
19
|
+
def default_message
|
20
|
+
i18n_defined? ? I18n.t(ERROR_MESSAGE_I18N_KEY, scope: [:errors, :messages], default: DEFAULT_MESSAGE) : DEFAULT_MESSAGE
|
21
|
+
end
|
22
|
+
|
23
|
+
def default_schema_message
|
24
|
+
i18n_defined? ? I18n.t(ERROR_SCHEMA_MESSAGE_I18N_KEY, scope: [:errors, :messages], default: DEFAULT_SCHEMA_MESSAGE): DEFAULT_SCHEMA_MESSAGE
|
25
|
+
end
|
26
|
+
|
27
|
+
protected
|
28
|
+
|
29
|
+
def i18n_defined?
|
30
|
+
defined?(I18n)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
self.schema_paths = []
|
35
|
+
|
36
|
+
def self.setup
|
37
|
+
yield self
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
require 'validates_xml_of/validator'
|
42
|
+
require 'validates_xml_of/active_model' if defined?(::ActiveModel)
|
43
|
+
require 'validates_xml_of/railtie' if defined?(::Rails::Railtie)
|
44
|
+
require 'validates_xml_of/matchers/validate_xml_of' if defined?(::RSpec)
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'validates_xml_of'
|
2
|
+
require 'active_model'
|
3
|
+
|
4
|
+
module ActiveModel
|
5
|
+
module Validations
|
6
|
+
class XmlValidator < EachValidator
|
7
|
+
def validate_each(record, attribute, value)
|
8
|
+
xml_content = value
|
9
|
+
|
10
|
+
if defined?(::CarrierWave) && value.is_a?(::CarrierWave::Uploader::Base)
|
11
|
+
xml_content = record.send(attribute).read
|
12
|
+
end
|
13
|
+
|
14
|
+
validator = ValidatesXmlOf::Validator.new(xml_content, options)
|
15
|
+
error = validator.validate
|
16
|
+
|
17
|
+
return if error.blank?
|
18
|
+
|
19
|
+
record.errors.add(attribute, error)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module HelperMethods
|
24
|
+
def validates_xml_of(*attr_names)
|
25
|
+
validates_with XmlValidator, _merge_attributes(attr_names)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'validates_xml_of'
|
2
|
+
|
3
|
+
module ValidatesXmlOf
|
4
|
+
module Matchers
|
5
|
+
class ValidateXmlOf
|
6
|
+
def initialize(attribute)
|
7
|
+
@attribute = attribute
|
8
|
+
end
|
9
|
+
|
10
|
+
def matches?(given_record)
|
11
|
+
@given_record = given_record
|
12
|
+
|
13
|
+
@expected_message ||= ValidatesXmlOf.default_message
|
14
|
+
|
15
|
+
valid_xml? && valid_xml_based_on_schema?
|
16
|
+
end
|
17
|
+
|
18
|
+
def with_schema(schema)
|
19
|
+
@schema = schema
|
20
|
+
@expected_message ||= ValidatesXmlOf.default_schema_message
|
21
|
+
|
22
|
+
self
|
23
|
+
end
|
24
|
+
|
25
|
+
def description
|
26
|
+
description = "#{@attribute} contains a valid xml"
|
27
|
+
|
28
|
+
if @schema
|
29
|
+
description << " based on '#{@schema}'"
|
30
|
+
end
|
31
|
+
|
32
|
+
description
|
33
|
+
end
|
34
|
+
|
35
|
+
def with_message(expected_message)
|
36
|
+
@expected_message = expected_message
|
37
|
+
|
38
|
+
self
|
39
|
+
end
|
40
|
+
|
41
|
+
def failure_message
|
42
|
+
"Expected #{@attribute} to contains a valid xml, but it is #{attribute_output.inspect}"
|
43
|
+
end
|
44
|
+
|
45
|
+
def failure_message_when_negated
|
46
|
+
"Expected #{@attribute} to not contains a valid xml, but it is #{attribute_output.inspect}"
|
47
|
+
end
|
48
|
+
|
49
|
+
protected
|
50
|
+
|
51
|
+
def valid_xml?
|
52
|
+
return false unless attribute_output
|
53
|
+
|
54
|
+
@given_record.valid?
|
55
|
+
|
56
|
+
!@given_record.errors[@attribute].include?(@expected_message)
|
57
|
+
end
|
58
|
+
|
59
|
+
def valid_xml_based_on_schema?
|
60
|
+
return true if @schema.nil?
|
61
|
+
|
62
|
+
@given_record.valid?
|
63
|
+
|
64
|
+
!@given_record.errors[@attribute].include?(@expected_message)
|
65
|
+
end
|
66
|
+
|
67
|
+
def attribute_output
|
68
|
+
value = @given_record.send(@attribute)
|
69
|
+
|
70
|
+
if defined?(::CarrierWave) && value.is_a?(::CarrierWave::Uploader::Base)
|
71
|
+
value = @given_record.send(@attribute).read
|
72
|
+
end
|
73
|
+
|
74
|
+
value
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def validate_xml_of(*args)
|
79
|
+
ValidateXmlOf.new(*args)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
module ValidatesXmlOf
|
2
|
+
class Validator
|
3
|
+
def initialize(xml, options = {})
|
4
|
+
self.xml = xml
|
5
|
+
self.options = options
|
6
|
+
end
|
7
|
+
|
8
|
+
def validate
|
9
|
+
message = nil
|
10
|
+
|
11
|
+
if xml.nil? || xml.empty? || !xml.is_a?(String)
|
12
|
+
if options[:schema]
|
13
|
+
message = merged_options[:schema_message]
|
14
|
+
else
|
15
|
+
message = merged_options[:message]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
if !is_a_valid_xml?(xml)
|
20
|
+
message = merged_options[:message]
|
21
|
+
end
|
22
|
+
|
23
|
+
return message unless message.nil?
|
24
|
+
|
25
|
+
if options[:schema]
|
26
|
+
schema = lookup_schema_file(options[:schema])
|
27
|
+
|
28
|
+
return merged_options[:schema_message] if schema.nil?
|
29
|
+
|
30
|
+
if !schema.nil? && !is_a_valid_xml_based_on_schema?(xml, schema)
|
31
|
+
message = merged_options[:schema_message]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
return message
|
36
|
+
end
|
37
|
+
|
38
|
+
protected
|
39
|
+
|
40
|
+
attr_accessor :xml, :options
|
41
|
+
|
42
|
+
def default_options
|
43
|
+
@default_options ||= {
|
44
|
+
message: ValidatesXmlOf.default_message,
|
45
|
+
schema_message: ValidatesXmlOf.default_schema_message
|
46
|
+
}
|
47
|
+
end
|
48
|
+
|
49
|
+
def merged_options
|
50
|
+
@merged_options ||= options.merge(default_options) { |key, old, new| old }
|
51
|
+
end
|
52
|
+
|
53
|
+
def is_a_valid_xml?(document_content)
|
54
|
+
Nokogiri::XML(document_content).errors.empty?
|
55
|
+
end
|
56
|
+
|
57
|
+
def is_a_valid_xml_based_on_schema?(document_content, schema)
|
58
|
+
schema = Nokogiri::XML::Schema(schema)
|
59
|
+
document = Nokogiri::XML(document_content)
|
60
|
+
|
61
|
+
schema.validate(document).empty?
|
62
|
+
end
|
63
|
+
|
64
|
+
def lookup_schema_file(schema_name)
|
65
|
+
paths = ValidatesXmlOf.schema_paths
|
66
|
+
schema_file = nil
|
67
|
+
|
68
|
+
return schema_file if paths.empty? || paths.nil?
|
69
|
+
|
70
|
+
if paths.is_a?(String)
|
71
|
+
schema_file = schema_file(paths, schema_name)
|
72
|
+
else
|
73
|
+
paths.reject(&:nil?).each do |path|
|
74
|
+
schema_file = schema_file(path, schema_name)
|
75
|
+
|
76
|
+
break unless schema_file
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
schema_file
|
81
|
+
end
|
82
|
+
|
83
|
+
def schema_file(path, schema_name)
|
84
|
+
schema_file_name = File.join(path, "#{schema_name}.xsd")
|
85
|
+
|
86
|
+
if File.exist?(schema_file_name) && !File.directory?(schema_file_name)
|
87
|
+
return File.open(schema_file_name)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'validates_xml_of/active_model'
|
3
|
+
|
4
|
+
describe ActiveModel::Validations::XmlValidator do
|
5
|
+
let(:content) { |example| example.description }
|
6
|
+
|
7
|
+
describe 'validate without schema' do
|
8
|
+
subject do |example|
|
9
|
+
class Person
|
10
|
+
include ActiveModel::Validations
|
11
|
+
|
12
|
+
attr_reader :content
|
13
|
+
|
14
|
+
def initialize(content)
|
15
|
+
@content = content.freeze
|
16
|
+
end
|
17
|
+
|
18
|
+
validates :content, xml: true
|
19
|
+
end
|
20
|
+
|
21
|
+
Person.new(example.example_group_instance.content).tap(&:valid?).errors.full_messages
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'valid' do
|
25
|
+
it '<?xml version="1.0"?><foo></foo>' do
|
26
|
+
expect(subject).to be_empty
|
27
|
+
end
|
28
|
+
|
29
|
+
it '<?xml version="1.0"?><foo value="10" />' do
|
30
|
+
expect(subject).to be_empty
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'invalid' do
|
35
|
+
it '<?xml version="1.0"?><foo' do
|
36
|
+
expect(subject).to include 'Content does not appear to be a valid xml'
|
37
|
+
end
|
38
|
+
|
39
|
+
it '<?xml version="1.0"?><foo>' do
|
40
|
+
expect(subject).to include 'Content does not appear to be a valid xml'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe 'validate with schema' do
|
46
|
+
subject do |example|
|
47
|
+
class Person
|
48
|
+
include ActiveModel::Validations
|
49
|
+
|
50
|
+
attr_reader :content
|
51
|
+
|
52
|
+
def initialize(content)
|
53
|
+
@content = content.freeze
|
54
|
+
end
|
55
|
+
|
56
|
+
validates :content, xml: { schema: 'Schema' }
|
57
|
+
end
|
58
|
+
|
59
|
+
Person.new(example.example_group_instance.content).tap(&:valid?).errors.full_messages
|
60
|
+
end
|
61
|
+
|
62
|
+
before do
|
63
|
+
ValidatesXmlOf.setup do |config|
|
64
|
+
config.schema_paths = "examples/xsds"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'valid' do
|
69
|
+
it '<?xml version="1.0"?><Bar><foo><foo_bar value="baz" /></foo></Bar>' do
|
70
|
+
expect(subject).to be_empty
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context 'invalid' do
|
75
|
+
it '<?xml version="1.0"?><foo' do
|
76
|
+
expect(subject).to include 'Content does not appear to be a valid xml'
|
77
|
+
end
|
78
|
+
|
79
|
+
it '<?xml version="1.0"?><foo>' do
|
80
|
+
expect(subject).to include 'Content does not appear to be a valid xml'
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "validates_xml_of/matchers/validate_xml_of"
|
3
|
+
|
4
|
+
class Post
|
5
|
+
include ::ActiveModel::Validations
|
6
|
+
|
7
|
+
attr_accessor :content, :content2, :number
|
8
|
+
|
9
|
+
validates :content, xml: true
|
10
|
+
validates :content2, xml: { schema: 'Schema' }
|
11
|
+
end
|
12
|
+
|
13
|
+
RSpec.configure do |config|
|
14
|
+
config.include ValidatesXmlOf::Matchers
|
15
|
+
end
|
16
|
+
|
17
|
+
describe Post do
|
18
|
+
before do
|
19
|
+
subject.content = '<?xml version="1.0"?><foo></foo>'
|
20
|
+
subject.content2 = '<?xml version="1.0"?><Bar><foo><foo_bar value="baz" /></foo></Bar>'
|
21
|
+
end
|
22
|
+
|
23
|
+
it { should validate_xml_of(:content) }
|
24
|
+
it { should validate_xml_of(:content2).with_schema('Schema') }
|
25
|
+
it { should_not validate_xml_of(:number) }
|
26
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ValidatesXmlOf::Validator do
|
4
|
+
before do
|
5
|
+
I18n.enforce_available_locales = false
|
6
|
+
|
7
|
+
I18n.locale = :en
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '#validate' do
|
11
|
+
context 'returns the invalid message' do
|
12
|
+
let(:malformed_xml) { '<?xml version="1.0"?><foo' }
|
13
|
+
|
14
|
+
it 'when not informed a xml' do
|
15
|
+
validator = described_class.new(nil)
|
16
|
+
|
17
|
+
expect(validator.validate).to eq 'does not appear to be a valid xml'
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'when informed a malformed xml' do
|
21
|
+
validator = described_class.new(malformed_xml)
|
22
|
+
|
23
|
+
expect(validator.validate).to eq 'does not appear to be a valid xml'
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'customized' do
|
27
|
+
validator = described_class.new(malformed_xml, message: 'it is not a xml')
|
28
|
+
|
29
|
+
expect(validator.validate).to eq 'it is not a xml'
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'returns the invalid message based on schema' do
|
34
|
+
let(:formed_xml) { '<?xml version="1.0"?><foo></foo>' }
|
35
|
+
|
36
|
+
before do
|
37
|
+
ValidatesXmlOf.schema_paths = "examples/xsds"
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'when informed an invalid xml from the schema point of view' do
|
41
|
+
validator = described_class.new(formed_xml, schema: 'Schema')
|
42
|
+
|
43
|
+
expect(validator.validate).to eq 'does not appear to be a valid xml based on schema informed'
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'customized' do
|
47
|
+
validator = described_class.new(formed_xml, schema: 'Schema', schema_message: 'the xml is malformed')
|
48
|
+
|
49
|
+
expect(validator.validate).to eq 'the xml is malformed'
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'returns nil' do
|
54
|
+
let(:formed_xml) { '<?xml version="1.0"?><foo></foo>' }
|
55
|
+
|
56
|
+
before do
|
57
|
+
ValidatesXmlOf.schema_paths = "examples/xsds"
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'when xml is well formed' do
|
61
|
+
validator = described_class.new(formed_xml)
|
62
|
+
|
63
|
+
expect(validator.validate).to be_nil
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'when xml is well formed from the schema point of view' do
|
67
|
+
formed_xml_based_schema = '<?xml version="1.0"?><Bar><foo><foo_bar value="baz" /></foo></Bar>'
|
68
|
+
|
69
|
+
validator = described_class.new(formed_xml_based_schema, schema: 'Schema')
|
70
|
+
|
71
|
+
expect(validator.validate).to be_nil
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'i18n'
|
3
|
+
|
4
|
+
describe ValidatesXmlOf do
|
5
|
+
describe 'setup' do
|
6
|
+
it 'set schema_paths through setup block' do
|
7
|
+
ValidatesXmlOf.setup do |config|
|
8
|
+
config.schema_paths = "examples/xsds"
|
9
|
+
end
|
10
|
+
|
11
|
+
expect(ValidatesXmlOf.schema_paths).to eq "examples/xsds"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '.default_message' do
|
16
|
+
context 'without i18n' do
|
17
|
+
before do
|
18
|
+
expect(subject).to receive(:i18n_defined?).and_return(false)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'returns the DEFAULT_MESSAGE constant' do
|
22
|
+
expect(ValidatesXmlOf.default_message).to eq 'does not appear to be a valid xml'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'with i18n' do
|
27
|
+
before do
|
28
|
+
expect(I18n).to receive(:t).with(any_args).and_return('is not a xml')
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'returns the i18n defined message' do
|
32
|
+
expect(ValidatesXmlOf.default_message).to eq 'is not a xml'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '.default_schema_message' do
|
38
|
+
context 'without i18n' do
|
39
|
+
before do
|
40
|
+
expect(subject).to receive(:i18n_defined?).and_return(false)
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'returns the DEFAULT_SCHEMA_MESSAGE constant' do
|
44
|
+
expect(ValidatesXmlOf.default_schema_message).to eq 'does not appear to be a valid xml based on schema informed'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context 'with i18n' do
|
49
|
+
before do
|
50
|
+
expect(I18n).to receive(:t).with(any_args).and_return('xml input is not valid for the schema')
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'returns the i18n defined message' do
|
54
|
+
expect(ValidatesXmlOf.default_message).to eq 'xml input is not valid for the schema'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'validates_xml_of/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "validates_xml_of"
|
8
|
+
spec.version = ValidatesXmlOf::VERSION
|
9
|
+
spec.authors = ["Bruno Arueira"]
|
10
|
+
spec.email = ["contato@brunoarueira.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Validates if an attribute has a valid xml content or a xsd valid content.}
|
13
|
+
spec.description = spec.summary
|
14
|
+
spec.homepage = "https://github.com/brunoarueira/validates_xml"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "i18n"
|
22
|
+
spec.add_dependency "nokogiri"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
spec.add_development_dependency "activemodel", ">= 3.0"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validates_xml_of
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bruno Arueira
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: i18n
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: nokogiri
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.11'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.11'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: activemodel
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
description: Validates if an attribute has a valid xml content or a xsd valid content.
|
98
|
+
email:
|
99
|
+
- contato@brunoarueira.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".travis.yml"
|
107
|
+
- CODE_OF_CONDUCT.md
|
108
|
+
- Gemfile
|
109
|
+
- LICENSE.txt
|
110
|
+
- README.md
|
111
|
+
- Rakefile
|
112
|
+
- bin/console
|
113
|
+
- bin/setup
|
114
|
+
- config/locales/en.yml
|
115
|
+
- examples/xsds/Schema.xsd
|
116
|
+
- lib/validates_xml_of.rb
|
117
|
+
- lib/validates_xml_of/active_model.rb
|
118
|
+
- lib/validates_xml_of/matchers/validate_xml_of.rb
|
119
|
+
- lib/validates_xml_of/railtie.rb
|
120
|
+
- lib/validates_xml_of/validator.rb
|
121
|
+
- lib/validates_xml_of/version.rb
|
122
|
+
- spec/spec_helper.rb
|
123
|
+
- spec/validates_xml_of/active_model_spec.rb
|
124
|
+
- spec/validates_xml_of/matchers/validate_xml_of_spec.rb
|
125
|
+
- spec/validates_xml_of/validator_spec.rb
|
126
|
+
- spec/validates_xml_of_spec.rb
|
127
|
+
- validates_xml_of.gemspec
|
128
|
+
homepage: https://github.com/brunoarueira/validates_xml
|
129
|
+
licenses:
|
130
|
+
- MIT
|
131
|
+
metadata: {}
|
132
|
+
post_install_message:
|
133
|
+
rdoc_options: []
|
134
|
+
require_paths:
|
135
|
+
- lib
|
136
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
137
|
+
requirements:
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
version: '0'
|
141
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
requirements: []
|
147
|
+
rubyforge_project:
|
148
|
+
rubygems_version: 2.5.2
|
149
|
+
signing_key:
|
150
|
+
specification_version: 4
|
151
|
+
summary: Validates if an attribute has a valid xml content or a xsd valid content.
|
152
|
+
test_files:
|
153
|
+
- spec/spec_helper.rb
|
154
|
+
- spec/validates_xml_of/active_model_spec.rb
|
155
|
+
- spec/validates_xml_of/matchers/validate_xml_of_spec.rb
|
156
|
+
- spec/validates_xml_of/validator_spec.rb
|
157
|
+
- spec/validates_xml_of_spec.rb
|