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 +4 -4
- data/README.md +45 -5
- data/lib/schash/schema/dsl.rb +8 -0
- data/lib/schash/schema/rule.rb +1 -0
- data/lib/schash/schema/rule/one_of_types.rb +24 -0
- data/lib/schash/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6e7d207a677b2b5941b9bf2f59d43fe38ee74c7
|
4
|
+
data.tar.gz: 9497b992bd3ea937fd11cfe3a35caf788f3951b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
|
data/lib/schash/schema/dsl.rb
CHANGED
@@ -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
|
data/lib/schash/schema/rule.rb
CHANGED
@@ -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
|
+
|
data/lib/schash/version.rb
CHANGED
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.
|
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-
|
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
|