grayskull 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,122 @@
1
+ Grayskull
2
+ =========
3
+
4
+ Grayskull is a Ruby library for validating data files against custom schema. It currently validates YAML and JSON files. It can be used as a library in your
5
+ ruby code and also comes with a command line tool.
6
+
7
+ Installation
8
+ ------------
9
+
10
+ gem install grayskull
11
+
12
+ Usage
13
+ -----
14
+
15
+ ###Library###
16
+
17
+ require 'grayskull'
18
+ validator = Grayskull::Validator.new(file,schema)
19
+ results = validator.validate
20
+
21
+ Grayskulls `validate` function will return one of the following Hashes.
22
+
23
+ Succes:
24
+
25
+ {
26
+ "result":true
27
+ }
28
+
29
+ Failure:
30
+
31
+ {
32
+ "result":false,
33
+ "errors":errors #Array of error messages
34
+
35
+ }
36
+
37
+ ###Command Line###
38
+
39
+ grayskull validate path/to/file path/to/schema
40
+
41
+ ###Schema Files###
42
+
43
+ Schema files should be in the following format:
44
+
45
+ YAML:
46
+
47
+ sections:
48
+ - name: section1 #section name
49
+ required: yes yes|no
50
+ type: Array #Valid ruby type or a custom type as defined in types.
51
+ ok_empty: yes|no
52
+ accepts: #list of accepted types
53
+ - custom_type1
54
+ - custom_type2
55
+ types:
56
+ custom_type1:
57
+ type: Hash #Valid ruby type or a custom type as defined in types.
58
+ ok_empty: yes|no
59
+ accepts:
60
+ - String #list of accepted types
61
+ custom_type2:
62
+ type: Array
63
+ ok_empty: yes|no
64
+ accepts:
65
+ - Array
66
+
67
+ JSON
68
+
69
+ {
70
+ "sections":[
71
+ {
72
+ "name": "section_name",
73
+ "required": true|false,
74
+ "type": "Array", #Valid ruby type or a custom type as defined in types.
75
+ "ok_empty": true,
76
+ "accepts":[ #list of accepted types
77
+ "custom_type1",
78
+ "String"
79
+ ]
80
+ }
81
+ ],
82
+ "types":{
83
+ "custom_type1":{
84
+ "type":"Hash",#Valid ruby type or a custom type as defined in types.
85
+ "ok_empty":true|false,
86
+ "accepts":[ #list of accepted types
87
+ "Hash"
88
+ ]
89
+ },
90
+ }
91
+ }
92
+
93
+ As all files are converted to native ruby formats you can mix json files with yaml schemas and vice versa.
94
+
95
+ Coming Soon
96
+ -----------
97
+
98
+ * More formats
99
+ * Unit Tests
100
+
101
+ Notes
102
+ -----
103
+
104
+ This is an extraction of the validation code from my first gem skeletor. I'm still pretty new to ruby so I'd be really grateful for any constructive feedback, input, suggestions etc. As usual, if anybody wants to
105
+ contribute feel free, just check out the guidelines below.
106
+
107
+ Contributing to Skeletor
108
+ ------------------------
109
+
110
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
111
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
112
+ * Fork the project
113
+ * Start a feature/bugfix branch
114
+ * Commit and push until you are happy with your contribution
115
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
116
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
117
+
118
+ Copyright
119
+ ---------
120
+
121
+ Copyright (c) 2011 OiNutter. See LICENSE.txt for
122
+ further details.
data/lib/grayskull.rb CHANGED
@@ -3,6 +3,7 @@ require 'grayskull/version'
3
3
  # Base module for gem.
4
4
  module Grayskull
5
5
  autoload :Formats, 'grayskull/formats'
6
+ autoload :DataFile, 'grayskull/datafile'
6
7
  autoload :Validator, 'grayskull/validator'
7
8
  autoload :CLI, 'grayskull/cli'
8
9
  end
@@ -0,0 +1,23 @@
1
+ module Grayskull
2
+
3
+ # The *DataFile* class contains methods for loading
4
+ # and working with the supported data files.
5
+ class DataFile
6
+
7
+ # Loads the specified file depending on the format
8
+ def self.load(file)
9
+
10
+ format = Formats::detect_format File.basename(file)
11
+
12
+ case format
13
+ when 'yaml'
14
+ return Formats::YAMLHandler.load(file)
15
+ when 'json'
16
+ return Formats::JSONHandler.load(file)
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+
23
+ end
@@ -11,13 +11,13 @@ module Grayskull
11
11
  @schema = schema
12
12
 
13
13
  if file.kind_of?(String) && !Formats::FILENAME_PATTERN.match(file).nil?
14
- @loaded_file = self.load(file)
14
+ @loaded_file = DataFile.load(file)
15
15
  else
16
16
  @loaded_file = file
17
17
  end
18
18
 
19
19
  if schema.kind_of?(String) && !Formats::FILENAME_PATTERN.match(schema).nil?
20
- @loaded_schema = self.load(schema)
20
+ @loaded_schema = DataFile.load(schema)
21
21
  else
22
22
  @loaded_schema = schema
23
23
  end
@@ -26,21 +26,7 @@ module Grayskull
26
26
 
27
27
  @errors = []
28
28
  end
29
-
30
- # Loads the specified file depending on the format
31
- def load(file)
32
-
33
- format = Formats::detect_format File.basename(file)
34
29
 
35
- case format
36
- when 'yaml'
37
- return Formats::YAMLHandler.load(file)
38
- when 'json'
39
- return Formats::JSONHandler.load(file)
40
- end
41
-
42
- end
43
-
44
30
  # Validates the file against the schema
45
31
  def validate()
46
32
  failed = []
@@ -1,4 +1,4 @@
1
1
  module Grayskull
2
2
  # Current version number
3
- VERSION = "0.1.6"
3
+ VERSION = "0.1.7"
4
4
  end
metadata CHANGED
@@ -1,60 +1,50 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: grayskull
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 1
8
- - 6
9
- version: 0.1.6
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.7
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Will McKenzie
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-10-02 00:00:00 +01:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2011-10-03 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: thor
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70341851833000 !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- segments:
29
- - 0
30
- version: "0"
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.14.6
31
22
  type: :runtime
32
- version_requirements: *id001
33
- - !ruby/object:Gem::Dependency
34
- name: json
35
23
  prerelease: false
36
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *70341851833000
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ requirement: &70341851832220 !ruby/object:Gem::Requirement
37
28
  none: false
38
- requirements:
39
- - - ">="
40
- - !ruby/object:Gem::Version
41
- segments:
42
- - 0
43
- version: "0"
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 1.6.1
44
33
  type: :runtime
45
- version_requirements: *id002
34
+ prerelease: false
35
+ version_requirements: *70341851832220
46
36
  description: Will validate YAML and JSON files based on a provided schema
47
- email:
37
+ email:
48
38
  - will@oinutter.co.uk
49
- executables:
39
+ executables:
50
40
  - grayskull
51
41
  extensions: []
52
-
53
42
  extra_rdoc_files: []
54
-
55
- files:
43
+ files:
44
+ - README.md
56
45
  - LICENSE
57
46
  - lib/grayskull/cli.rb
47
+ - lib/grayskull/DataFile.rb
58
48
  - lib/grayskull/formats/json_handler.rb
59
49
  - lib/grayskull/formats/yaml_handler.rb
60
50
  - lib/grayskull/formats.rb
@@ -62,37 +52,29 @@ files:
62
52
  - lib/grayskull/version.rb
63
53
  - lib/grayskull.rb
64
54
  - bin/grayskull
65
- has_rdoc: true
66
55
  homepage: http://github.com/OiNutter/grayskull
67
- licenses:
56
+ licenses:
68
57
  - MIT
69
58
  post_install_message:
70
59
  rdoc_options: []
71
-
72
- require_paths:
60
+ require_paths:
73
61
  - lib
74
- required_ruby_version: !ruby/object:Gem::Requirement
62
+ required_ruby_version: !ruby/object:Gem::Requirement
75
63
  none: false
76
- requirements:
77
- - - ">="
78
- - !ruby/object:Gem::Version
79
- segments:
80
- - 0
81
- version: "0"
82
- required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
69
  none: false
84
- requirements:
85
- - - ">="
86
- - !ruby/object:Gem::Version
87
- segments:
88
- - 0
89
- version: "0"
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
90
74
  requirements: []
91
-
92
75
  rubyforge_project: grayskull
93
- rubygems_version: 1.3.7
76
+ rubygems_version: 1.8.10
94
77
  signing_key:
95
78
  specification_version: 3
96
79
  summary: Validates data files based on a provided schema
97
80
  test_files: []
98
-