schash 0.1.0 → 0.1.1

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: 14f23046a5330b40005faa16302f41f180fe76f8
4
- data.tar.gz: 5e3468253f6bd076dd2680151e19e5ce54031e1f
3
+ metadata.gz: c6e7d207a677b2b5941b9bf2f59d43fe38ee74c7
4
+ data.tar.gz: 9497b992bd3ea937fd11cfe3a35caf788f3951b3
5
5
  SHA512:
6
- metadata.gz: 5fae3816ab0fccf5b5131091b8657046038093125b19043e235687cb7b4f57257c9fb3d7e2278983d35536771767c9ba55a28f7de61376fa6d75ffd17efd0725
7
- data.tar.gz: fad4a51c9f7abef9a774171bf89e7f9c6fe51ca304ff422c5a307c7eecc301ce563cf2ad20e50a14e4c762de8537d165a73255d9edad6aded50216bb780c0147
6
+ metadata.gz: 0e69aaef9fc52845432e6ab0577577b7f48205d88e0861c17babf5bd1f4999a0b4617b9b313b2f428236c29542ac534f04937c17e6bbbd99c9ed2d819d2ea5ae
7
+ data.tar.gz: 40cdb442104335f83cec9597ceda90339bc6d3f0eaf93f3be6036883ccdbd6aa5082583553d7c6889aca12890e742c044158a07825144d25b2567a33221f4d18
data/README.md CHANGED
@@ -18,13 +18,53 @@ And then execute:
18
18
 
19
19
  $ bundle
20
20
 
21
- Or install it yourself as:
22
-
23
- $ gem install schash
24
-
25
21
  ## Usage
26
22
 
27
- TODO: Write usage instructions here
23
+ ```ruby
24
+ validator = Schash::Validator.new do
25
+ {
26
+ nginx: {
27
+ user: string, # required field
28
+ worker_processes: optional(integer), # optional field
29
+ sites: array_of({
30
+ server_name: string,
31
+ root: string,
32
+ allowed_ips: array_of(string),
33
+ }),
34
+ },
35
+ }
36
+ end
37
+
38
+ # valid example
39
+ valid = {
40
+ nginx: {
41
+ user: "www-data",
42
+ worker_processes: 4,
43
+ sites: [{
44
+ server_name: "example.com",
45
+ root: "/var/www/itamae",
46
+ allowed_ips: ["127.0.0.1/32"],
47
+ }],
48
+ },
49
+ }
50
+
51
+ validator.validate(valid) # => []
52
+
53
+ # invalid example
54
+ invalid = {
55
+ nginx: {
56
+ user: 123,
57
+ sites: {
58
+ server_name: "example.com",
59
+ root: "/var/www/itamae",
60
+ allowed_ips: ["127.0.0.1/32"],
61
+ },
62
+ },
63
+ }
64
+
65
+ validator.validate(invalid)
66
+ # => [#<struct Schash::Schema::Error position=["nginx", "user"], message="is not String">, #<struct Schash::Schema::Error position=["nginx", "sites"], message="is not an array">]
67
+ ```
28
68
 
29
69
  ## Development
30
70
 
@@ -9,6 +9,10 @@ module Schash
9
9
  Rule::ArrayOf.new(schema)
10
10
  end
11
11
 
12
+ def one_of_types(*schemas)
13
+ Rule::OneOfTypes.new(*schemas)
14
+ end
15
+
12
16
  def type(klass)
13
17
  Rule::Type.new(klass)
14
18
  end
@@ -40,6 +44,10 @@ module Schash
40
44
  def array
41
45
  type(Array)
42
46
  end
47
+
48
+ def boolean
49
+ one_of_types(TrueClass, FalseClass)
50
+ end
43
51
  end
44
52
  end
45
53
  end
@@ -1,5 +1,6 @@
1
1
  require 'schash/schema/rule/base'
2
2
  require 'schash/schema/rule/array_of'
3
+ require 'schash/schema/rule/one_of_types'
3
4
  require 'schash/schema/rule/hash'
4
5
  require 'schash/schema/rule/optional'
5
6
  require 'schash/schema/rule/type'
@@ -0,0 +1,24 @@
1
+ module Schash
2
+ module Schema
3
+ module Rule
4
+ class OneOfTypes < Base
5
+ def initialize(*klasses)
6
+ @klasses = klasses
7
+ end
8
+
9
+ def validate(target, position = [])
10
+ match = @klasses.any? do |klass|
11
+ target.is_a?(klass)
12
+ end
13
+
14
+ if match
15
+ []
16
+ else
17
+ [Error.new(position, "is not any of #{@klasses.map(&:to_s).join(', ')}")]
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
@@ -1,3 +1,3 @@
1
1
  module Schash
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryota Arai
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-03-17 00:00:00.000000000 Z
11
+ date: 2015-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -76,6 +76,7 @@ files:
76
76
  - lib/schash/schema/rule/array_of.rb
77
77
  - lib/schash/schema/rule/base.rb
78
78
  - lib/schash/schema/rule/hash.rb
79
+ - lib/schash/schema/rule/one_of_types.rb
79
80
  - lib/schash/schema/rule/optional.rb
80
81
  - lib/schash/schema/rule/type.rb
81
82
  - lib/schash/validator.rb