hash_schema 0.1.2 → 0.1.3

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: d3b7349c659a2350aa25c7458299b28585588c51
4
- data.tar.gz: 8fd0f8a09a4efc41f210c8a249248a1ea3d209d1
3
+ metadata.gz: 772c753165bbc1e83ce3c65fe5b45bb49ec3365c
4
+ data.tar.gz: 625cddb7219d5a8e04157d3710382d4e01725f52
5
5
  SHA512:
6
- metadata.gz: cf3e54beb99430de451ac85d7c1094fceec8e10c1096c55eefa740a75fbbdd816d1d627ef9cd722797786d307bf53892fb669f8b5e5e970f4c186e65c61812c9
7
- data.tar.gz: cda472ff701fc474a1846e31c0ed90f14a9b3486fbfa34415d4fbb7b14a8ba769da48f84adde52361494ead9fb1a52fee36253e160a3359250b700969065d413
6
+ metadata.gz: 90439941e93b65e6880ec0683d23c20466264a13eca95c566eb680dc43e25f5f83abe14a0f53e72046350cb9b701e452d2fbf530bb4d47c51421488983f73f54
7
+ data.tar.gz: abdc89cc5ef3026d536f7656be1060f6a109ab3a999a37d4b2618ff88f81a1a1e13ba5e17df5828e90b9e4346f1a90ef92f8df16517667edec85475698676dd0
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # HashSchema
1
+ # HashSchema [![Gem Version](https://badge.fury.io/rb/hash_schema.svg)](http://badge.fury.io/rb/hash_schema)
2
2
 
3
3
  A ruby gem that validates Hash against some schema, works for hashes created from loading json and yml files, and suchlike
4
4
 
@@ -18,7 +18,172 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ ### Defined schemas
22
+
23
+ * StringSchema
24
+ * NumberSchema
25
+ * BooleanSchema
26
+ * EnumSchema
27
+ * OptionalSchema
28
+ * OrSchema
29
+ * ArraySchema
30
+ * **HashSchema**
31
+
32
+ ### API
33
+
34
+ - `#valdiate` :: Hash -> Hash
35
+ - `#pretty_validate` :: Hash -> String **(*)**
36
+ - `#interpret` :: Hash -> [String]
37
+
38
+ **(*)** `#pretty_validate` is just a `JSON.pretty_generate` wrapper of `#validate`
39
+
40
+ *See below for more details*
41
+
42
+ ### Demo
43
+
44
+ *Demo Setup*
45
+
46
+ ```ruby
47
+ require 'json'
48
+ require 'hash_schema'
49
+
50
+ class Test
51
+ include HashSchema
52
+
53
+ Definition = HashSchema.new(
54
+ string_good: StringSchema.new,
55
+ string_bad: StringSchema.new,
56
+ number_good: NumberSchema.new,
57
+ number_bad: NumberSchema.new,
58
+ boolean_good: BooleanSchema.new,
59
+ boolean_bad: BooleanSchema.new,
60
+ enum_good1: EnumSchema.new('z', 0),
61
+ enum_good2: EnumSchema.new('z', 0),
62
+ enum_bad: EnumSchema.new('z', 0),
63
+ optional_good: OptionalSchema.new(1),
64
+ optional_bad: OptionalSchema.new(1),
65
+ optional_key_missing: OptionalSchema.new(1),
66
+ mandatory_key_missing: 'value',
67
+ or_good1: OrSchema.new(StringSchema.new, NumberSchema.new),
68
+ or_good2: OrSchema.new(StringSchema.new, NumberSchema.new),
69
+ or_bad: OrSchema.new(StringSchema.new, NumberSchema.new),
70
+ array_good: ArraySchema.new(NumberSchema.new),
71
+ array_bad: ArraySchema.new(NumberSchema.new),
72
+ object_array_good: ArraySchema.new(HashSchema.new(x: NumberSchema.new, y: NumberSchema.new)),
73
+ object_array_bad: ArraySchema.new(HashSchema.new(x: NumberSchema.new, y: NumberSchema.new)),
74
+ object_array_missing: ArraySchema.new(HashSchema.new(x: NumberSchema.new, y: NumberSchema.new)),
75
+ nested_hash: HashSchema.new(string: 'xyz', number: 987, boolean: true),
76
+ nested_hash_missing: HashSchema.new
77
+ )
78
+ end
79
+
80
+ data = {
81
+ string_good: '123',
82
+ string_bad: 123,
83
+ number_good: 456,
84
+ number_bad: '456',
85
+ boolean_good: true,
86
+ boolean_bad: 'false',
87
+ enum_good1: 'z',
88
+ enum_good2: 0,
89
+ enum_bad: 'a',
90
+ optional_good: 1,
91
+ optional_bad: 2,
92
+ or_good1: 'abc',
93
+ or_good2: 123,
94
+ or_bad: false,
95
+ array_good: [0.5, 3.1, 4.2, 8],
96
+ array_bad: [nil, 'a', false],
97
+ object_array_good: [{ x: 0.5, y: 0.3 }, { x: 4, y: 3 }],
98
+ object_array_bad: [{ x: 3 }, { y: 4 }],
99
+ nested_hash: {}
100
+ }
101
+ ```
102
+
103
+ *Demo output*
104
+
105
+ ```ruby
106
+ puts Test::Definition.pretty_validate(data) # outputs the following
107
+ ```
108
+
109
+ {
110
+ "string_good": null,
111
+ "string_bad": "Expected String but got 123",
112
+ "number_good": null,
113
+ "number_bad": "Expected Number but got \"456\"",
114
+ "boolean_good": null,
115
+ "boolean_bad": "Expected Boolean but got \"false\"",
116
+ "enum_good1": null,
117
+ "enum_good2": null,
118
+ "enum_bad": "Expected \"z\" or 0 but got \"a\"",
119
+ "optional_good": null,
120
+ "optional_bad": "Expected 1 but got 2",
121
+ "optional_key_missing": null,
122
+ "mandatory_key_missing": "Expected \"value\" but got Nothing",
123
+ "or_good1": null,
124
+ "or_good2": null,
125
+ "or_bad": "Expected String or Number but got false",
126
+ "array_good": [
127
+ null,
128
+ null,
129
+ null,
130
+ null
131
+ ],
132
+ "array_bad": [
133
+ "Expected Number but got nil",
134
+ "Expected Number but got \"a\"",
135
+ "Expected Number but got false"
136
+ ],
137
+ "object_array_good": [
138
+ {
139
+ "x": null,
140
+ "y": null
141
+ },
142
+ {
143
+ "x": null,
144
+ "y": null
145
+ }
146
+ ],
147
+ "object_array_bad": [
148
+ {
149
+ "x": null,
150
+ "y": "Expected Number but got Nothing"
151
+ },
152
+ {
153
+ "x": "Expected Number but got Nothing",
154
+ "y": null
155
+ }
156
+ ],
157
+ "object_array_missing": "Expected [Hash] but got Nothing",
158
+ "nested_hash": {
159
+ "string": "Expected \"xyz\" but got Nothing",
160
+ "number": "Expected 987 but got Nothing",
161
+ "boolean": "Expected true but got Nothing"
162
+ },
163
+ "nested_hash_missing": "Expected Hash but got Nothing"
164
+ }
165
+
166
+ ```ruby
167
+ puts Test::Definition.interpret(data) # outputs the following
168
+ ```
169
+
170
+ root:{} > .string_bad > Expected String but got 123
171
+ root:{} > .number_bad > Expected Number but got "456"
172
+ root:{} > .boolean_bad > Expected Boolean but got "false"
173
+ root:{} > .enum_bad > Expected "z" or 0 but got "a"
174
+ root:{} > .optional_bad > Expected 1 but got 2
175
+ root:{} > .mandatory_key_missing > Expected "value" but got Nothing
176
+ root:{} > .or_bad > Expected String or Number but got false
177
+ root:{} > array_bad:[] > #0 > Expected Number but got nil
178
+ root:{} > array_bad:[] > #1 > Expected Number but got "a"
179
+ root:{} > array_bad:[] > #2 > Expected Number but got false
180
+ root:{} > object_array_bad:[] > #0:{} > .y > Expected Number but got Nothing
181
+ root:{} > object_array_bad:[] > #1:{} > .x > Expected Number but got Nothing
182
+ root:{} > .object_array_missing > Expected [Hash] but got Nothing
183
+ root:{} > nested_hash:{} > .string > Expected "xyz" but got Nothing
184
+ root:{} > nested_hash:{} > .number > Expected 987 but got Nothing
185
+ root:{} > nested_hash:{} > .boolean > Expected true but got Nothing
186
+ root:{} > .nested_hash_missing > Expected Hash but got Nothing
22
187
 
23
188
  ## Contributing
24
189
 
data/hash_schema.gemspec CHANGED
@@ -22,6 +22,8 @@ Gem::Specification.new do |spec|
22
22
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
23
23
  spec.require_paths = ["lib"]
24
24
 
25
+ spec.required_ruby_version = ">= 2.0"
26
+
25
27
  spec.add_development_dependency "bump"
26
28
  spec.add_development_dependency "bundler", "~> 1.6"
27
29
  spec.add_development_dependency "byebug"
@@ -1,3 +1,3 @@
1
1
  module HashSchema
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Po Chen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-03 00:00:00.000000000 Z
11
+ date: 2014-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bump
@@ -112,7 +112,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
112
112
  requirements:
113
113
  - - ">="
114
114
  - !ruby/object:Gem::Version
115
- version: '0'
115
+ version: '2.0'
116
116
  required_rubygems_version: !ruby/object:Gem::Requirement
117
117
  requirements:
118
118
  - - ">="