dslh 0.3.9 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bea460ff3013100d73e63f5ad28d76314ff27922
4
- data.tar.gz: d786b97c2645b741c5dbd301f3a9826f663931a0
3
+ metadata.gz: ed45c1e092e5109296acdff1cbdf5f04480d54ea
4
+ data.tar.gz: c01f03de3c202a12f34311ebd38355976547849f
5
5
  SHA512:
6
- metadata.gz: 44c114e62597267087a86d9676cea3df76ae42a5533d98d8eb45692469ae9d3da2a48dbf6aec8bff32e990927effda441bfd8e9a2cd3790a4f6238c8c10a566d
7
- data.tar.gz: 249d851a76f7026e6c334583fa78f71e0aff4629bba2e049aee8ea2bbed83372864ac56008864a49bbbde4de0c977d3032c18a3cfc552f136e905ebee7f161f3
6
+ metadata.gz: 7a4334b6e9a8a5893090b0c94e1cb0bd2f96ada901d6bfa4781b65b1f8a50b013b614516a3c63043f7639e140444987b17f4f1db98abadc229a17807f2ba8e14
7
+ data.tar.gz: 12f0f82b30ccbd566b568d4cec226273d6ed96e2e6cc0ee7c4c90b18956e9b7b3d9cf2ab4eb8f97f46c37e9d256b6e430bc717200e0172424effa5fe497f50c0
data/README.md CHANGED
@@ -197,3 +197,63 @@ puts dsl
197
197
  # }
198
198
  # }
199
199
  ```
200
+
201
+ ### Validate schema using [Kwalify](http://www.kuwata-lab.com/kwalify/)
202
+
203
+ ```sh
204
+ gem install kwalify
205
+ ```
206
+
207
+ ```ruby
208
+ schema = <<-EOS
209
+ type: map
210
+ mapping:
211
+ "company":
212
+ type: str
213
+ required: yes
214
+ "email":
215
+ type: str
216
+ "employees":
217
+ type: seq
218
+ sequence:
219
+ - type: map
220
+ mapping:
221
+ "code":
222
+ type: int
223
+ required: yes
224
+ "name":
225
+ type: str
226
+ required: yes
227
+ "email":
228
+ type: str
229
+ EOS
230
+ EOS
231
+
232
+ begin
233
+ Dslh.eval(:schema => schema) do
234
+ block = proc do
235
+ company "winebarrel inc."
236
+ email "webmaster@winebarrel.com"
237
+ employees do |*|
238
+ code "foo"
239
+ name 101
240
+ email "foo@winebarrel.com"
241
+ end
242
+ employees do |*|
243
+ code1 102
244
+ name1 "bar"
245
+ email1 "bar@winebarrel.com"
246
+ end
247
+ end
248
+ end
249
+ rescue Dslh::ValidationError => e
250
+ puts e.message
251
+ # => [/employees/0/code] 'foo': not a integer.
252
+ # [/employees/0/name] '101': not a string.
253
+ # [/employees/1] key 'code:' is required.
254
+ # [/employees/1] key 'name:' is required.
255
+ # [/employees/1/code1] key 'code1:' is undefined.
256
+ # [/employees/1/name1] key 'name1:' is undefined.
257
+ # [/employees/1/email1] key 'email1:' is undefined.
258
+ end
259
+ ```
@@ -21,4 +21,5 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency 'bundler'
22
22
  spec.add_development_dependency 'rake'
23
23
  spec.add_development_dependency 'rspec', '>= 3.0.0'
24
+ spec.add_development_dependency 'kwalify'
24
25
  end
@@ -1,10 +1,40 @@
1
1
  require 'dslh/version'
2
2
  require 'stringio'
3
3
  require 'pp'
4
+ require 'yaml'
4
5
 
5
6
  class Dslh
7
+ class ValidationError < StandardError
8
+ attr_reader :errors
9
+
10
+ def initialize(root_errors)
11
+ super(root_errors.map {|e| e.to_s }.join("\n"))
12
+ @errors = root_errors
13
+ end
14
+ end
15
+
6
16
  INDENT_SPACES = ' '
7
17
 
18
+ VALID_OPTIONS = [
19
+ :allow_duplicate,
20
+ :allow_empty_args,
21
+ :conv,
22
+ :dump_old_hash_array_format,
23
+ :exclude_key,
24
+ :filename,
25
+ :force_dump_braces,
26
+ :ignore_methods,
27
+ :initial_depth,
28
+ :key_conv,
29
+ :lineno,
30
+ :scope_hook,
31
+ :scope_vars,
32
+ :time_inspecter,
33
+ :use_braces_instead_of_do_end,
34
+ :schema,
35
+ :value_conv,
36
+ ]
37
+
8
38
  class << self
9
39
  def eval(expr_or_options = nil, options = nil, &block)
10
40
  if options and not options.kind_of?(Hash)
@@ -38,6 +68,12 @@ class Dslh
38
68
  end # of class methods
39
69
 
40
70
  def initialize(options = {})
71
+ invlid_options = options.keys - VALID_OPTIONS
72
+
73
+ unless invlid_options.empty?
74
+ raise ArgumentError, 'invalid option ' + invlid_options.map {|i| i.inspect }.join(',')
75
+ end
76
+
41
77
  @options = {
42
78
  :time_inspecter => method(:inspect_time),
43
79
  :dump_old_hash_array_format => false,
@@ -84,6 +120,26 @@ class Dslh
84
120
  scope.instance_eval(&block)
85
121
  end
86
122
 
123
+ if schema = @options[:schema]
124
+ begin
125
+ require 'kwalify'
126
+ rescue LoadError
127
+ raise 'cannot load "kwalify". please install "kwalify"'
128
+ end
129
+
130
+ unless schema.kind_of?(String)
131
+ raise TypeError, "wrong schema type #{schema.class} (expected String)"
132
+ end
133
+
134
+ schema = Kwalify::Yaml.load(schema)
135
+ validator = Kwalify::Validator.new(schema)
136
+ errors = validator.validate(retval)
137
+
138
+ unless errors.empty?
139
+ raise ValidationError, errors
140
+ end
141
+ end
142
+
87
143
  return retval
88
144
  end
89
145
 
@@ -1,3 +1,3 @@
1
1
  class Dslh
2
- VERSION = '0.3.9'
2
+ VERSION = '0.4.0'
3
3
  end
@@ -2921,4 +2921,108 @@ glossary {
2921
2921
  }
2922
2922
  EOS
2923
2923
  end
2924
+
2925
+ it 'ignore specific methods' do
2926
+ h = Dslh.eval do
2927
+ sprintf "100"
2928
+ end
2929
+
2930
+ expect(h).to eq({})
2931
+
2932
+ h = Dslh.eval(:ignore_methods => :sprintf) do
2933
+ sprintf "100"
2934
+ end
2935
+
2936
+ expect(h).to eq({"sprintf" => "100"})
2937
+ end
2938
+
2939
+ context 'when validate schema' do
2940
+ let(:schema) do
2941
+ <<-EOS
2942
+ type: map
2943
+ mapping:
2944
+ "company":
2945
+ type: str
2946
+ required: yes
2947
+ "email":
2948
+ type: str
2949
+ "employees":
2950
+ type: seq
2951
+ sequence:
2952
+ - type: map
2953
+ mapping:
2954
+ "code":
2955
+ type: int
2956
+ required: yes
2957
+ "name":
2958
+ type: str
2959
+ required: yes
2960
+ "email":
2961
+ type: str
2962
+ EOS
2963
+ end
2964
+
2965
+ let(:expected_errmsg) do
2966
+ <<-EOS.chomp
2967
+ [/employees/0/code] 'foo': not a integer.
2968
+ [/employees/0/name] '101': not a string.
2969
+ [/employees/1] key 'code:' is required.
2970
+ [/employees/1] key 'name:' is required.
2971
+ [/employees/1/code1] key 'code1:' is undefined.
2972
+ [/employees/1/name1] key 'name1:' is undefined.
2973
+ [/employees/1/email1] key 'email1:' is undefined.
2974
+ EOS
2975
+ end
2976
+
2977
+ it 'has no error' do
2978
+ h = Dslh.eval(:schema => schema) do
2979
+ company "winebarrel inc."
2980
+ email "webmaster@winebarrel.com"
2981
+ employees do |*|
2982
+ code 101
2983
+ name "foo"
2984
+ email "foo@winebarrel.com"
2985
+ end
2986
+ employees do |*|
2987
+ code 102
2988
+ name "bar"
2989
+ email "bar@winebarrel.com"
2990
+ end
2991
+ end
2992
+
2993
+ expect(h).to eq(
2994
+ {"company"=>"winebarrel inc.",
2995
+ "email"=>"webmaster@winebarrel.com",
2996
+ "employees"=>
2997
+ [{"code"=>101, "name"=>"foo", "email"=>"foo@winebarrel.com"},
2998
+ {"code"=>102, "name"=>"bar", "email"=>"bar@winebarrel.com"}]}
2999
+ )
3000
+ end
3001
+
3002
+ it 'has errors' do
3003
+ block = proc do
3004
+ company "winebarrel inc."
3005
+ email "webmaster@winebarrel.com"
3006
+ employees do |*|
3007
+ code "foo"
3008
+ name 101
3009
+ email "foo@winebarrel.com"
3010
+ end
3011
+ employees do |*|
3012
+ code1 102
3013
+ name1 "bar"
3014
+ email1 "bar@winebarrel.com"
3015
+ end
3016
+ end
3017
+
3018
+ begin
3019
+ h = Dslh.eval(:schema => schema, &block)
3020
+ fail "must raise validation error"
3021
+ rescue Dslh::ValidationError => e
3022
+ errmsg = e.errors.map {|i| i.to_s }.join("\n")
3023
+ expect(errmsg).to eq expected_errmsg
3024
+ expect(e.message).to eq expected_errmsg
3025
+ end
3026
+ end
3027
+ end
2924
3028
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dslh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.9
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genki Sugawara
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-19 00:00:00.000000000 Z
11
+ date: 2016-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: 3.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: kwalify
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: It define Hash as a DSL.
56
70
  email:
57
71
  - sugawara@cookpad.com
@@ -93,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
107
  version: '0'
94
108
  requirements: []
95
109
  rubyforge_project:
96
- rubygems_version: 2.4.5.1
110
+ rubygems_version: 2.5.2
97
111
  signing_key:
98
112
  specification_version: 4
99
113
  summary: It define Hash as a DSL.