simple_schema 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2011 Concrete Solutions
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
File without changes
@@ -0,0 +1,30 @@
1
+ module SimpleSchema
2
+ class Validator
3
+ class InvalidSchemaType < Exception
4
+ attr_reader :type
5
+ def initialize(type)
6
+ @type = type
7
+ super "Schema must be either an Array or a Hash, got #{type}"
8
+ end
9
+ end
10
+
11
+ class InvalidDataType < Exception
12
+ attr_reader :type
13
+ def initialize(type)
14
+ @type = type
15
+ super "Data must be a Hash, got #{type}"
16
+ end
17
+ end
18
+
19
+ class MissingAttribute < Exception
20
+ attr_reader :attribute, :fragments
21
+
22
+ def initialize(attribute, fragments)
23
+ @attribute = attribute
24
+ @fragments = fragments
25
+ super "Missing #{fragments.join('/')}"
26
+ end
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,160 @@
1
+ module SimpleSchema
2
+ class Validator
3
+ attr_reader :schema, :fragments, :data
4
+
5
+ def initialize(schema, data)
6
+ if !schema.is_a?(Hash) && !schema.is_a?(Array)
7
+ raise InvalidSchemaType.new(schema.class)
8
+ end
9
+
10
+ if !data.is_a?(Hash)
11
+ raise InvalidDataType.new(data.class)
12
+ end
13
+
14
+ @data = data
15
+ @fragments = []
16
+ @schema = build_schema({ :properties => schema })[:properties]
17
+ end
18
+
19
+ def valid?
20
+ begin
21
+ validate(@schema, @data)
22
+ rescue MissingAttribute
23
+ false
24
+ end
25
+ end
26
+
27
+ def validate!
28
+ validate(@schema, @data)
29
+ end
30
+
31
+ def translated_data
32
+ translate_data(@data, @schema)
33
+ end
34
+
35
+ private
36
+
37
+ def schema_defaults
38
+ { :required => true, :type => :string }
39
+ end
40
+
41
+ def normalize_properties(properties)
42
+ return properties unless properties.is_a?(Array)
43
+ h = {}
44
+ properties.each do |key|
45
+ h[key] = {}
46
+ end
47
+ h
48
+ end
49
+
50
+ def build_schema(current_schema)
51
+ schema = schema_defaults
52
+
53
+ current_schema.each do |property, value|
54
+ property = property.to_sym
55
+
56
+ case property
57
+ when :required
58
+ schema[:required] = value
59
+ when :type
60
+ schema[:type] = value.to_sym
61
+ when :properties
62
+ schema[:type] = :object
63
+ schema[:properties] = Hash.new
64
+
65
+ normalize_properties(value).each do |property, value|
66
+ original_key = property.to_sym
67
+ property = value[:key_name] unless value[:key_name].nil?
68
+ value[:key_name] = original_key
69
+ schema[:properties][property.to_sym] = build_schema(value)
70
+ end
71
+ when :items
72
+ schema[:type] = :array
73
+ schema[:items] = Hash.new
74
+
75
+ normalize_properties(value).each do |property, value|
76
+ original_key = property.to_sym
77
+ property = value[:key_name] unless value[:key_name].nil?
78
+ value[:key_name] = original_key
79
+ schema[:items][property.to_sym] = build_schema(value)
80
+ end
81
+ when :key_name
82
+ schema[:key_name] = value
83
+ end
84
+ end
85
+
86
+ schema
87
+ end
88
+
89
+ def validate(current_schema, data)
90
+ current_schema.each do |property_name, schema|
91
+
92
+ @fragments << property_name
93
+
94
+ required_value = schema[:required]
95
+
96
+ if required_value.respond_to?(:call)
97
+ required_value = required_value.call(@data, @schema)
98
+ end
99
+
100
+ if required_value && (data.respond_to?(:has_key?) && !data.has_key?(property_name))
101
+ raise MissingAttribute.new(property_name, fragments)
102
+ end
103
+
104
+ if required_value
105
+
106
+ case schema[:type]
107
+ when :string
108
+ if !data[property_name].is_a?(String) || data[property_name].strip.empty?
109
+ raise MissingAttribute.new(property_name, fragments)
110
+ end
111
+ when :object
112
+ if !data[property_name].is_a?(Hash) || !data[property_name].any?
113
+ raise MissingAttribute.new(property_name, fragments)
114
+ end
115
+ validate(schema[:properties], data[property_name])
116
+ when :array
117
+ if !data[property_name].is_a?(Array) || !data[property_name].any?
118
+ raise MissingAttribute.new(property_name, fragments)
119
+ end
120
+ data[property_name].each_with_index do |item, index|
121
+ @fragments << index
122
+ validate(schema[:items], item)
123
+ @fragments.pop
124
+ end
125
+ end
126
+ end
127
+
128
+ @fragments.pop
129
+ end
130
+ end
131
+
132
+ def translate_data(data, current_schema)
133
+ new_data = {}
134
+
135
+ data.each do |key, value|
136
+ if !current_schema || !current_schema.has_key?(key)
137
+ new_data[key] = value
138
+ next
139
+ end
140
+
141
+ new_key = current_schema[key][:key_name]
142
+
143
+ if value.is_a?(Array)
144
+ new_value = []
145
+ value.each do |value|
146
+ new_value << translate_data(value, current_schema[key][:items])
147
+ end
148
+ new_data[new_key] = new_value
149
+ elsif value.is_a?(Hash)
150
+ new_data[new_key] = translate_data(value, current_schema[key][:properties])
151
+ else
152
+ new_data[new_key] = value
153
+ end
154
+ end
155
+
156
+ new_data
157
+ end
158
+ end
159
+ end
160
+
@@ -0,0 +1,4 @@
1
+ module SimpleSchema
2
+ VERSION = "0.0.1"
3
+ end
4
+
@@ -0,0 +1,3 @@
1
+ require 'simple_schema/errors'
2
+ require 'simple_schema/validator'
3
+
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_schema
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Lenon Marcel
9
+ - Celestino Gomes
10
+ - Renato Elias
11
+ - Madson Cardoso
12
+ - Marcelo Linhares
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+ date: 2011-08-16 00:00:00.000000000Z
17
+ dependencies:
18
+ - !ruby/object:Gem::Dependency
19
+ name: rspec
20
+ requirement: &12972440 !ruby/object:Gem::Requirement
21
+ none: false
22
+ requirements:
23
+ - - ! '>='
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
26
+ type: :development
27
+ prerelease: false
28
+ version_requirements: *12972440
29
+ description: Simple schema validator for Ruby
30
+ email:
31
+ - lenon.marcel@gmail.com
32
+ - tinorj@gmail.com
33
+ - renato.elias@gmail.com
34
+ - madsonmac@gmail.com
35
+ - marcelolinhares@gmail.com
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - lib/simple_schema/version.rb
41
+ - lib/simple_schema/validator.rb
42
+ - lib/simple_schema/errors.rb
43
+ - lib/simple_schema.rb
44
+ - LICENSE
45
+ - README
46
+ homepage: http://github.com/concretesolutions/simple_schema/
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.6
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Simple schema validator for Ruby
70
+ test_files: []