govuk_schemas 2.3.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0159a5f0fe899f67971dd67034deba8635b35e8d
4
- data.tar.gz: aac60f618ada4ceaf5dd8393c92b3481f4a73d13
3
+ metadata.gz: c1a7c7e79986243df375449cf4aa6f6253a1122d
4
+ data.tar.gz: 8307b3beac9c9d57491a1353fc9b00d570a1a14c
5
5
  SHA512:
6
- metadata.gz: f523a79a97ea8b5f2b2d0628acf9fe63a39e12bc90e93c86a1500f0c40ada8227096c0e9193e265ed586c77fe005d703764312fe18e3e6af5d73debb2b37ef06
7
- data.tar.gz: 255d1f6196893e54f95eb1bd80ac579251030bc9c248a84eb5d4ee49f04e8cd1620cd29f9a8af3495832c55f5e8f44f6c8ac43ee64cb8e79ba83e1b2533105f1
6
+ metadata.gz: aa4b2a0b936859273872fe296bcd2ccea3aa5fe94efb18044774ab5f1dd1760c6fee1eb20f66f0af65ea55a976db4dc6a7e6394204ed4ea81fe48c3a31635c17
7
+ data.tar.gz: fb542c17251b814ada00097d8dc0a5463b424586d71699f615e8d43027fee00118680d5f850bed8eb5a03e108928091ab55a11d32601065397435f68d55ae645
data/.yardopts ADDED
@@ -0,0 +1,2 @@
1
+ --no-private
2
+ --markup markdown
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 3.0.0
2
+
3
+ * Move to block based customisation of randomly generated payloads. This removes the existing methods: `customise_and_validate` and `merge_and_validate`. In additon `GovukSchemas::RandomExample.for_schema(schema)` now returns the payload hash directly.
4
+
1
5
  # 2.3.0
2
6
 
3
7
  * Allow looking up examples to work with schemas stored in `formats/{format}/{schema_type}/examples/` and `examples/{format}/{schema_type}/` to allow schema examples to move.
data/README.md CHANGED
@@ -10,14 +10,9 @@ In your Gemfile:
10
10
  gem "govuk_schemas", "~> VERSION"
11
11
  ```
12
12
 
13
- ## Limitations
14
-
15
- - The gem doesn't support `patternProperties` yet. On GOV.UK we [use this in the expanded frontend links](https://github.com/alphagov/govuk-content-schemas/blob/bdd97d18c7a9318e66f332f0748a410fddab1141/formats/frontend_links_definition.json#L67-L71).
16
- - It's complicated to generate random data for `oneOf` properties. According to the JSON Schema spec a `oneOf` schema is only valid if the data is valid against *only one* of the clauses. To do this properly, we'd have to make sure that the data generated below doesn't validate against the other schemas properties.
17
-
18
13
  ## Usage
19
14
 
20
- [Read the documentation!](https://alphagov.github.io/govuk_schemas/frames.html)
15
+ [Read the documentation!](http://www.rubydoc.info/github/alphagov/govuk_schemas)
21
16
 
22
17
  ## Running the test suite
23
18
 
@@ -33,10 +28,6 @@ To run a Yard server locally to preview documentation, run:
33
28
 
34
29
  $ bundle exec yard server --reload
35
30
 
36
- To rebuild the documentation, run:
37
-
38
- $ bundle exec rake yard
39
-
40
31
  ## License
41
32
 
42
33
  The gem is available as open source under the terms of the [MIT License](LICENSE.md).
data/Rakefile CHANGED
@@ -1,11 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rspec/core/rake_task"
3
- require "yard"
4
3
 
5
4
  RSpec::Core::RakeTask.new(:spec)
6
5
 
7
6
  task default: [:spec]
8
-
9
- YARD::Rake::YardocTask.new do |t|
10
- t.options = ['--no-private', '--markup', 'markdown', '--output-dir', 'docs']
11
- end
@@ -4,6 +4,18 @@ require "json-schema"
4
4
  require "json"
5
5
 
6
6
  module GovukSchemas
7
+ # Generate random content based on a schema.
8
+ #
9
+ # ## Limitations
10
+ #
11
+ # - The gem doesn't support `patternProperties` yet. On GOV.UK we [use this in
12
+ # the expanded frontend
13
+ # links](https://github.com/alphagov/govuk-content-schemas/blob/bdd97d18c7a9318e66f332f0748a410fddab1141/formats/frontend_links_definition.json#L67-L71).
14
+ # - It's complicated to generate random data for `oneOf` properties. According
15
+ # to the JSON Schema spec a `oneOf` schema is only valid if the data is valid
16
+ # against *only one* of the clauses. To do this properly, we'd have to make
17
+ # sure that the data generated below doesn't validate against the other
18
+ # schemas properties.
7
19
  class RandomExample
8
20
  # Returns a new `GovukSchemas::RandomExample` object.
9
21
  #
@@ -21,112 +33,101 @@ module GovukSchemas
21
33
 
22
34
  # Returns a new `GovukSchemas::RandomExample` object.
23
35
  #
24
- # For example:
36
+ # Example without block:
37
+ #
38
+ # GovukSchemas::RandomExample.for_schema(frontend_schema: "detailed_guide")
39
+ # # => {"base_path"=>"/e42dd28e", "title"=>"dolor est...", "publishing_app"=>"elit"...}
25
40
  #
26
- # generator = GovukSchemas::RandomExample.for_schema(frontend_schema: "detailed_guide")
27
- # generator.payload
28
- # # => {"base_path"=>"/e42dd28e", "title"=>"dolor est...", "publishing_app"=>"elit"...}
41
+ # Example with block:
42
+ #
43
+ # GovukSchemas::RandomExample.for_schema(frontend_schema: "detailed_guide") do |payload|
44
+ # payload.merge('base_path' => "Test base path")
45
+ # end
46
+ # # => {"base_path"=>"Test base path", "title"=>"dolor est...", "publishing_app"=>"elit"...}
29
47
  #
30
48
  # @param schema_key_value [Hash]
49
+ # @param [Block] the base payload is passed inton the block, with the block result then becoming
50
+ # the new payload. The new payload is then validated. (optional)
31
51
  # @return [GovukSchemas::RandomExample]
32
- def self.for_schema(schema_key_value)
52
+ # @param [Block] the base payload is passed inton the block, with the block result then becoming
53
+ # the new payload. The new payload is then validated. (optional)
54
+ def self.for_schema(schema_key_value, &block)
33
55
  schema = GovukSchemas::Schema.find(schema_key_value)
34
- GovukSchemas::RandomExample.new(schema: schema)
56
+ GovukSchemas::RandomExample.new(schema: schema).payload(&block)
35
57
  end
36
58
 
37
59
  # Return a content item merged with a hash and with the excluded fields removed.
38
60
  # If the resulting content item isn't valid against the schema an error will be raised.
39
61
  #
40
- # Example:
62
+ # Example without block:
41
63
  #
42
- # random = GovukSchemas::RandomExample.for_schema("detailed_guide", schema_type: "frontend")
43
- # random.customise_and_validate({base_path: "/foo"}, ["withdrawn_notice"])
64
+ # generator.payload
44
65
  # # => {"base_path"=>"/e42dd28e", "title"=>"dolor est...", "publishing_app"=>"elit"...}
45
66
  #
46
- # @param [Hash] hash The hash to merge the random content with
47
- # @param [Array] array The array containing fields to exclude
67
+ # Example with block:
68
+ #
69
+ # generator.payload do |payload|
70
+ # payload.merge('base_path' => "Test base path")
71
+ # end
72
+ # # => {"base_path"=>"Test base path", "title"=>"dolor est...", "publishing_app"=>"elit"...}
73
+ #
74
+ # @param [Block] the base payload is passed inton the block, with the block result then becoming
75
+ # the new payload. The new payload is then validated. (optional)
48
76
  # @return [Hash] A content item
49
77
  # @raise [GovukSchemas::InvalidContentGenerated]
50
- def customise_and_validate(user_defined_values = {}, fields_to_exclude = [])
51
- random_payload = @random_generator.payload
52
- item_merged_with_user_input = random_payload.merge(Utils.stringify_keys(user_defined_values))
53
- item_merged_with_user_input.reject! { |k| fields_to_exclude.include?(k) }
54
- errors = validation_errors_for(item_merged_with_user_input)
55
- if errors.any?
56
-
57
- errors_on_random_payload = validation_errors_for(random_payload)
58
- if errors_on_random_payload.any?
59
- # The original item was invalid when it was generated, so it's not
60
- # the users fault.
61
- raise InvalidContentGenerated, error_message(random_payload, errors)
62
- else
63
- # The random item was valid, but it was merged with something invalid.
64
- raise InvalidContentGenerated, error_message_custom(item_merged_with_user_input, user_defined_values, errors)
65
- end
78
+ def payload
79
+ payload = @random_generator.payload
80
+ # ensure the base payload is valid
81
+ errors = validation_errors_for(payload)
82
+ raise InvalidContentGenerated, error_message(payload, errors) if errors.any?
83
+
84
+ if block_given?
85
+ payload = yield(payload)
86
+ # check the payload again after customisation
87
+ errors = validation_errors_for(payload)
88
+ raise InvalidContentGenerated, error_message(payload, errors, true) if errors.any?
66
89
  end
67
90
 
68
- item_merged_with_user_input
91
+ payload
69
92
  end
70
93
 
71
- # Return a hash with a random content item
72
- #
73
- # Example:
74
- #
75
- # GovukSchemas::RandomExample.for_schema("detailed_guide", schema_type: "frontend").payload
76
- # # => {"base_path"=>"/e42dd28e", "title"=>"dolor est...", "publishing_app"=>"elit"...}
77
- #
78
- # @return [Hash] A content item
79
- # Support backwards compatibility
80
- alias :payload :customise_and_validate
81
- alias :merge_and_validate :customise_and_validate
82
-
83
94
  private
84
95
 
85
96
  def validation_errors_for(item)
86
97
  JSON::Validator.fully_validate(@schema, item, errors_as_objects: true)
87
98
  end
88
99
 
89
- def error_message(item, errors)
90
- <<err
91
- An invalid content item was generated.
92
-
93
- This probably means there's a bug in the generator that causes it to output
94
- invalid values. Below you'll find the generated payload, the validation errors
95
- and the schema that was used.
96
-
97
- Validation errors:
98
- --------------------------
99
-
100
- #{JSON.pretty_generate(errors)}
101
-
102
- Generated payload:
103
- --------------------------
104
-
105
- #{JSON.pretty_generate([item])}
106
- err
107
- end
108
-
109
- def error_message_custom(item, custom_values, errors)
110
- error_messages = errors.map { |e| "- #{e[:message]}\n" }.join
100
+ def error_message(item, errors, customised = false)
101
+ details = <<~ERR
102
+ Validation errors:
103
+ --------------------------
111
104
 
112
- <<err
113
- The content item you are trying to generate is invalid against the schema.
105
+ #{JSON.pretty_generate(errors)}
114
106
 
115
- Validation errors:
116
- --------------------------
107
+ Generated payload:
108
+ --------------------------
117
109
 
118
- #{error_messages}
110
+ #{JSON.pretty_generate([item])}
111
+ ERR
119
112
 
120
- Custom values provided:
121
- --------------------------
113
+ if customised
114
+ <<~ERR
115
+ The content item you are trying to generate is invalid against the schema.
116
+ The item was valid before being customised.
122
117
 
123
- #{JSON.pretty_generate(custom_values)}
118
+ #{details}
119
+ ERR
120
+ else
121
+ <<~ERR
122
+ An invalid content item was generated.
124
123
 
125
- Generated payload:
126
- --------------------------
124
+ This probably means there's a bug in the generator that causes it to output
125
+ invalid values. Below you'll find the generated payload, the validation errors
126
+ and the schema that was used.
127
127
 
128
- #{JSON.pretty_generate([item])}
129
- err
128
+ #{details}
129
+ ERR
130
+ end
130
131
  end
131
132
  end
132
133
  end
@@ -25,6 +25,7 @@ module GovukSchemas
25
25
  end
26
26
  end
27
27
 
28
+ # @private
28
29
  class ValidationErrorMessage
29
30
  attr_reader :schema_name, :type, :payload
30
31
 
@@ -1,4 +1,4 @@
1
1
  module GovukSchemas
2
2
  # @private
3
- VERSION = "2.3.0".freeze
3
+ VERSION = "3.0.0".freeze
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: govuk_schemas
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GOV.UK Dev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-09-25 00:00:00.000000000 Z
11
+ date: 2017-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema
@@ -105,6 +105,7 @@ files:
105
105
  - ".rspec"
106
106
  - ".rubocop.yml"
107
107
  - ".ruby-version"
108
+ - ".yardopts"
108
109
  - CHANGELOG.md
109
110
  - Gemfile
110
111
  - Jenkinsfile
@@ -112,23 +113,6 @@ files:
112
113
  - README.md
113
114
  - Rakefile
114
115
  - bin/console
115
- - docs/GovukSchemas.html
116
- - docs/GovukSchemas/RandomExample.html
117
- - docs/GovukSchemas/Schema.html
118
- - docs/_index.html
119
- - docs/class_list.html
120
- - docs/css/common.css
121
- - docs/css/full_list.css
122
- - docs/css/style.css
123
- - docs/file.README.html
124
- - docs/file_list.html
125
- - docs/frames.html
126
- - docs/index.html
127
- - docs/js/app.js
128
- - docs/js/full_list.js
129
- - docs/js/jquery.js
130
- - docs/method_list.html
131
- - docs/top-level-namespace.html
132
116
  - govuk_schemas.gemspec
133
117
  - lib/govuk_schemas.rb
134
118
  - lib/govuk_schemas/example.rb
@@ -1,120 +0,0 @@
1
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
- <head>
5
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6
- <title>
7
- Module: GovukSchemas
8
-
9
- &mdash; Documentation by YARD 0.8.7.6
10
-
11
- </title>
12
-
13
- <link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />
14
-
15
- <link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />
16
-
17
- <script type="text/javascript" charset="utf-8">
18
- hasFrames = window.top.frames.main ? true : false;
19
- relpath = '';
20
- framesUrl = "frames.html#!GovukSchemas.html";
21
- </script>
22
-
23
-
24
- <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
25
-
26
- <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
27
-
28
-
29
- </head>
30
- <body>
31
- <div id="header">
32
- <div id="menu">
33
-
34
- <a href="_index.html">Index (G)</a> &raquo;
35
-
36
-
37
- <span class="title">GovukSchemas</span>
38
-
39
-
40
- <div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
41
- </div>
42
-
43
- <div id="search">
44
-
45
- <a class="full_list_link" id="class_list_link"
46
- href="class_list.html">
47
- Class List
48
- </a>
49
-
50
- <a class="full_list_link" id="method_list_link"
51
- href="method_list.html">
52
- Method List
53
- </a>
54
-
55
- <a class="full_list_link" id="file_list_link"
56
- href="file_list.html">
57
- File List
58
- </a>
59
-
60
- </div>
61
- <div class="clear"></div>
62
- </div>
63
-
64
- <iframe id="search_frame"></iframe>
65
-
66
- <div id="content"><h1>Module: GovukSchemas
67
-
68
-
69
-
70
- </h1>
71
-
72
- <dl class="box">
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
- <dt class="r1 last">Defined in:</dt>
82
- <dd class="r1 last">lib/govuk_schemas.rb<span class="defines">,<br />
83
- lib/govuk_schemas/utils.rb,<br /> lib/govuk_schemas/schema.rb,<br /> lib/govuk_schemas/random.rb,<br /> lib/govuk_schemas/version.rb,<br /> lib/govuk_schemas/random_example.rb,<br /> lib/govuk_schemas/random_item_generator.rb</span>
84
- </dd>
85
-
86
- </dl>
87
- <div class="clear"></div>
88
-
89
- <h2>Defined Under Namespace</h2>
90
- <p class="children">
91
-
92
-
93
-
94
-
95
- <strong class="classes">Classes:</strong> <span class='object_link'><a href="GovukSchemas/RandomExample.html" title="GovukSchemas::RandomExample (class)">RandomExample</a></span>, <span class='object_link'><a href="GovukSchemas/Schema.html" title="GovukSchemas::Schema (class)">Schema</a></span>
96
-
97
-
98
- </p>
99
-
100
- <h2>Constant Summary</h2>
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
- </div>
112
-
113
- <div id="footer">
114
- Generated on Thu Nov 3 17:31:15 2016 by
115
- <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
116
- 0.8.7.6 (ruby-2.3.1).
117
- </div>
118
-
119
- </body>
120
- </html>
@@ -1,594 +0,0 @@
1
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
- <head>
5
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6
- <title>
7
- Class: GovukSchemas::RandomExample
8
-
9
- &mdash; Documentation by YARD 0.8.7.6
10
-
11
- </title>
12
-
13
- <link rel="stylesheet" href="../css/style.css" type="text/css" charset="utf-8" />
14
-
15
- <link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8" />
16
-
17
- <script type="text/javascript" charset="utf-8">
18
- hasFrames = window.top.frames.main ? true : false;
19
- relpath = '../';
20
- framesUrl = "../frames.html#!GovukSchemas/RandomExample.html";
21
- </script>
22
-
23
-
24
- <script type="text/javascript" charset="utf-8" src="../js/jquery.js"></script>
25
-
26
- <script type="text/javascript" charset="utf-8" src="../js/app.js"></script>
27
-
28
-
29
- </head>
30
- <body>
31
- <div id="header">
32
- <div id="menu">
33
-
34
- <a href="../_index.html">Index (R)</a> &raquo;
35
- <span class='title'><span class='object_link'><a href="../GovukSchemas.html" title="GovukSchemas (module)">GovukSchemas</a></span></span>
36
- &raquo;
37
- <span class="title">RandomExample</span>
38
-
39
-
40
- <div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
41
- </div>
42
-
43
- <div id="search">
44
-
45
- <a class="full_list_link" id="class_list_link"
46
- href="../class_list.html">
47
- Class List
48
- </a>
49
-
50
- <a class="full_list_link" id="method_list_link"
51
- href="../method_list.html">
52
- Method List
53
- </a>
54
-
55
- <a class="full_list_link" id="file_list_link"
56
- href="../file_list.html">
57
- File List
58
- </a>
59
-
60
- </div>
61
- <div class="clear"></div>
62
- </div>
63
-
64
- <iframe id="search_frame"></iframe>
65
-
66
- <div id="content"><h1>Class: GovukSchemas::RandomExample
67
-
68
-
69
-
70
- </h1>
71
-
72
- <dl class="box">
73
-
74
- <dt class="r1">Inherits:</dt>
75
- <dd class="r1">
76
- <span class="inheritName">Object</span>
77
-
78
- <ul class="fullTree">
79
- <li>Object</li>
80
-
81
- <li class="next">GovukSchemas::RandomExample</li>
82
-
83
- </ul>
84
- <a href="#" class="inheritanceTree">show all</a>
85
-
86
- </dd>
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
- <dt class="r2 last">Defined in:</dt>
97
- <dd class="r2 last">lib/govuk_schemas/random_example.rb</dd>
98
-
99
- </dl>
100
- <div class="clear"></div>
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
- <h2>
111
- Class Method Summary
112
- <small>(<a href="#" class="summary_toggle">collapse</a>)</small>
113
- </h2>
114
-
115
- <ul class="summary">
116
-
117
- <li class="public ">
118
- <span class="summary_signature">
119
-
120
- <a href="#for_schema-class_method" title="for_schema (class method)">+ (GovukSchemas::RandomExample) <strong>for_schema</strong>(schema_key_value) </a>
121
-
122
-
123
-
124
- </span>
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
- <span class="summary_desc"><div class='inline'>
135
- <p>Returns a new <code>GovukSchemas::RandomExample</code> object.</p>
136
- </div></span>
137
-
138
- </li>
139
-
140
-
141
- </ul>
142
-
143
- <h2>
144
- Instance Method Summary
145
- <small>(<a href="#" class="summary_toggle">collapse</a>)</small>
146
- </h2>
147
-
148
- <ul class="summary">
149
-
150
- <li class="public ">
151
- <span class="summary_signature">
152
-
153
- <a href="#initialize-instance_method" title="#initialize (instance method)">- (GovukSchemas::RandomExample) <strong>initialize</strong>(schema:) </a>
154
-
155
-
156
-
157
- </span>
158
-
159
-
160
- <span class="note title constructor">constructor</span>
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
- <span class="summary_desc"><div class='inline'>
170
- <p>Returns a new <code>GovukSchemas::RandomExample</code> object.</p>
171
- </div></span>
172
-
173
- </li>
174
-
175
-
176
- <li class="public ">
177
- <span class="summary_signature">
178
-
179
- <a href="#merge_and_validate-instance_method" title="#merge_and_validate (instance method)">- (Hash) <strong>merge_and_validate</strong>(hash) </a>
180
-
181
-
182
-
183
- </span>
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
- <span class="summary_desc"><div class='inline'>
194
- <p>Return a content item merged with a hash.</p>
195
- </div></span>
196
-
197
- </li>
198
-
199
-
200
- <li class="public ">
201
- <span class="summary_signature">
202
-
203
- <a href="#payload-instance_method" title="#payload (instance method)">- (Hash) <strong>payload</strong> </a>
204
-
205
-
206
-
207
- </span>
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
-
216
-
217
- <span class="summary_desc"><div class='inline'>
218
- <p>Return a hash with a random content item.</p>
219
- </div></span>
220
-
221
- </li>
222
-
223
-
224
- </ul>
225
-
226
-
227
- <div id="constructor_details" class="method_details_list">
228
- <h2>Constructor Details</h2>
229
-
230
- <div class="method_details first">
231
- <h3 class="signature first" id="initialize-instance_method">
232
-
233
- - (<tt><span class='object_link'><a href="" title="GovukSchemas::RandomExample (class)">GovukSchemas::RandomExample</a></span></tt>) <strong>initialize</strong>(schema:)
234
-
235
-
236
-
237
-
238
-
239
- </h3><div class="docstring">
240
- <div class="discussion">
241
-
242
- <p>Returns a new <code>GovukSchemas::RandomExample</code> object.</p>
243
-
244
- <p>For example:</p>
245
-
246
- <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_schema'>schema</span> <span class='op'>=</span> <span class='const'>GovukSchemas</span><span class='op'>::</span><span class='const'>Schema</span><span class='period'>.</span><span class='id identifier rubyid_find'>find</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>detailed_guide</span><span class='tstring_end'>&quot;</span></span><span class='comma'>,</span> <span class='label'>schema_type:</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>frontend</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span>
247
- <span class='const'>GovukSchemas</span><span class='op'>::</span><span class='const'>RandomExample</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='id identifier rubyid_schema'>schema</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_payload'>payload</span>
248
- </code></pre>
249
-
250
-
251
- </div>
252
- </div>
253
- <div class="tags">
254
- <p class="tag_title">Parameters:</p>
255
- <ul class="param">
256
-
257
- <li>
258
-
259
- <span class='name'>schema</span>
260
-
261
-
262
- <span class='type'>(<tt>Hash</tt>)</span>
263
-
264
-
265
-
266
- &mdash;
267
- <div class='inline'>
268
- <p>A JSON schema.</p>
269
- </div>
270
-
271
- </li>
272
-
273
- </ul>
274
-
275
-
276
- </div><table class="source_code">
277
- <tr>
278
- <td>
279
- <pre class="lines">
280
-
281
-
282
- 17
283
- 18
284
- 19
285
- 20</pre>
286
- </td>
287
- <td>
288
- <pre class="code"><span class="info file"># File 'lib/govuk_schemas/random_example.rb', line 17</span>
289
-
290
- <span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='label'>schema:</span><span class='rparen'>)</span>
291
- <span class='ivar'>@schema</span> <span class='op'>=</span> <span class='id identifier rubyid_schema'>schema</span>
292
- <span class='ivar'>@random_generator</span> <span class='op'>=</span> <span class='const'>RandomItemGenerator</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='label'>schema:</span> <span class='id identifier rubyid_schema'>schema</span><span class='rparen'>)</span>
293
- <span class='kw'>end</span></pre>
294
- </td>
295
- </tr>
296
- </table>
297
- </div>
298
-
299
- </div>
300
-
301
-
302
- <div id="class_method_details" class="method_details_list">
303
- <h2>Class Method Details</h2>
304
-
305
-
306
- <div class="method_details first">
307
- <h3 class="signature first" id="for_schema-class_method">
308
-
309
- + (<tt><span class='object_link'><a href="" title="GovukSchemas::RandomExample (class)">GovukSchemas::RandomExample</a></span></tt>) <strong>for_schema</strong>(schema_key_value)
310
-
311
-
312
-
313
-
314
-
315
- </h3><div class="docstring">
316
- <div class="discussion">
317
-
318
- <p>Returns a new <code>GovukSchemas::RandomExample</code> object.</p>
319
-
320
- <p>For example:</p>
321
-
322
- <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_generator'>generator</span> <span class='op'>=</span> <span class='const'>GovukSchemas</span><span class='op'>::</span><span class='const'>RandomExample</span><span class='period'>.</span><span class='id identifier rubyid_for_schema'>for_schema</span><span class='lparen'>(</span><span class='label'>frontend_schema:</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>detailed_guide</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span>
323
- <span class='id identifier rubyid_generator'>generator</span><span class='period'>.</span><span class='id identifier rubyid_payload'>payload</span>
324
- <span class='comment'># =&gt; {&quot;base_path&quot;=&gt;&quot;/e42dd28e&quot;, &quot;title&quot;=&gt;&quot;dolor est...&quot;, &quot;publishing_app&quot;=&gt;&quot;elit&quot;...}
325
- </span></code></pre>
326
-
327
-
328
- </div>
329
- </div>
330
- <div class="tags">
331
- <p class="tag_title">Parameters:</p>
332
- <ul class="param">
333
-
334
- <li>
335
-
336
- <span class='name'>schema_key_value</span>
337
-
338
-
339
- <span class='type'>(<tt>Hash</tt>)</span>
340
-
341
-
342
-
343
- </li>
344
-
345
- </ul>
346
-
347
- <p class="tag_title">Returns:</p>
348
- <ul class="return">
349
-
350
- <li>
351
-
352
-
353
- <span class='type'>(<tt><span class='object_link'><a href="" title="GovukSchemas::RandomExample (class)">GovukSchemas::RandomExample</a></span></tt>)</span>
354
-
355
-
356
-
357
- </li>
358
-
359
- </ul>
360
-
361
- </div><table class="source_code">
362
- <tr>
363
- <td>
364
- <pre class="lines">
365
-
366
-
367
- 32
368
- 33
369
- 34
370
- 35</pre>
371
- </td>
372
- <td>
373
- <pre class="code"><span class="info file"># File 'lib/govuk_schemas/random_example.rb', line 32</span>
374
-
375
- <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_for_schema'>for_schema</span><span class='lparen'>(</span><span class='id identifier rubyid_schema_key_value'>schema_key_value</span><span class='rparen'>)</span>
376
- <span class='id identifier rubyid_schema'>schema</span> <span class='op'>=</span> <span class='const'>GovukSchemas</span><span class='op'>::</span><span class='const'>Schema</span><span class='period'>.</span><span class='id identifier rubyid_find'>find</span><span class='lparen'>(</span><span class='id identifier rubyid_schema_key_value'>schema_key_value</span><span class='rparen'>)</span>
377
- <span class='const'>GovukSchemas</span><span class='op'>::</span><span class='const'>RandomExample</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='label'>schema:</span> <span class='id identifier rubyid_schema'>schema</span><span class='rparen'>)</span>
378
- <span class='kw'>end</span></pre>
379
- </td>
380
- </tr>
381
- </table>
382
- </div>
383
-
384
- </div>
385
-
386
- <div id="instance_method_details" class="method_details_list">
387
- <h2>Instance Method Details</h2>
388
-
389
-
390
- <div class="method_details first">
391
- <h3 class="signature first" id="merge_and_validate-instance_method">
392
-
393
- - (<tt>Hash</tt>) <strong>merge_and_validate</strong>(hash)
394
-
395
-
396
-
397
-
398
-
399
- </h3><div class="docstring">
400
- <div class="discussion">
401
-
402
- <p>Return a content item merged with a hash. If the resulting content item
403
- isn&#39;t valid against the schema an error will be raised.</p>
404
-
405
- <p>Example:</p>
406
-
407
- <pre class="code ruby"><code class="ruby"> <span class='id identifier rubyid_random'>random</span> <span class='op'>=</span> <span class='const'>GovukSchemas</span><span class='op'>::</span><span class='const'>RandomExample</span><span class='period'>.</span><span class='id identifier rubyid_for_schema'>for_schema</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>detailed_guide</span><span class='tstring_end'>&quot;</span></span><span class='comma'>,</span> <span class='label'>schema_type:</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>frontend</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span>
408
- <span class='id identifier rubyid_random'>random</span><span class='period'>.</span><span class='id identifier rubyid_merge_and_validate'>merge_and_validate</span><span class='lparen'>(</span><span class='label'>base_path:</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>/foo</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span>
409
- <span class='comment'># =&gt; {&quot;base_path&quot;=&gt;&quot;/e42dd28e&quot;, &quot;title&quot;=&gt;&quot;dolor est...&quot;, &quot;publishing_app&quot;=&gt;&quot;elit&quot;...}
410
- </span></code></pre>
411
-
412
-
413
- </div>
414
- </div>
415
- <div class="tags">
416
- <p class="tag_title">Parameters:</p>
417
- <ul class="param">
418
-
419
- <li>
420
-
421
- <span class='name'>hash</span>
422
-
423
-
424
- <span class='type'>(<tt>Hash</tt>)</span>
425
-
426
-
427
-
428
- &mdash;
429
- <div class='inline'>
430
- <p>The hash to merge the random content with</p>
431
- </div>
432
-
433
- </li>
434
-
435
- </ul>
436
-
437
- <p class="tag_title">Returns:</p>
438
- <ul class="return">
439
-
440
- <li>
441
-
442
-
443
- <span class='type'>(<tt>Hash</tt>)</span>
444
-
445
-
446
-
447
- &mdash;
448
- <div class='inline'>
449
- <p>A content item</p>
450
- </div>
451
-
452
- </li>
453
-
454
- </ul>
455
- <p class="tag_title">Raises:</p>
456
- <ul class="raise">
457
-
458
- <li>
459
-
460
-
461
- <span class='type'>(<tt><span class='object_link'>GovukSchemas::InvalidContentGenerated</span></tt>)</span>
462
-
463
-
464
-
465
- </li>
466
-
467
- </ul>
468
-
469
- </div><table class="source_code">
470
- <tr>
471
- <td>
472
- <pre class="lines">
473
-
474
-
475
- 68
476
- 69
477
- 70
478
- 71
479
- 72
480
- 73
481
- 74
482
- 75
483
- 76
484
- 77</pre>
485
- </td>
486
- <td>
487
- <pre class="code"><span class="info file"># File 'lib/govuk_schemas/random_example.rb', line 68</span>
488
-
489
- <span class='kw'>def</span> <span class='id identifier rubyid_merge_and_validate'>merge_and_validate</span><span class='lparen'>(</span><span class='id identifier rubyid_hash'>hash</span><span class='rparen'>)</span>
490
- <span class='id identifier rubyid_item'>item</span> <span class='op'>=</span> <span class='id identifier rubyid_payload'>payload</span><span class='period'>.</span><span class='id identifier rubyid_merge'>merge</span><span class='lparen'>(</span><span class='const'>Utils</span><span class='period'>.</span><span class='id identifier rubyid_stringify_keys'>stringify_keys</span><span class='lparen'>(</span><span class='id identifier rubyid_hash'>hash</span><span class='rparen'>)</span><span class='rparen'>)</span>
491
- <span class='id identifier rubyid_errors'>errors</span> <span class='op'>=</span> <span class='id identifier rubyid_validation_errors_for'>validation_errors_for</span><span class='lparen'>(</span><span class='id identifier rubyid_item'>item</span><span class='rparen'>)</span>
492
-
493
- <span class='kw'>if</span> <span class='id identifier rubyid_errors'>errors</span><span class='period'>.</span><span class='id identifier rubyid_any?'>any?</span>
494
- <span class='id identifier rubyid_raise'>raise</span> <span class='const'>InvalidContentGenerated</span><span class='comma'>,</span> <span class='id identifier rubyid_error_message_custom'>error_message_custom</span><span class='lparen'>(</span><span class='id identifier rubyid_item'>item</span><span class='comma'>,</span> <span class='id identifier rubyid_hash'>hash</span><span class='comma'>,</span> <span class='id identifier rubyid_errors'>errors</span><span class='rparen'>)</span>
495
- <span class='kw'>end</span>
496
-
497
- <span class='id identifier rubyid_item'>item</span>
498
- <span class='kw'>end</span></pre>
499
- </td>
500
- </tr>
501
- </table>
502
- </div>
503
-
504
- <div class="method_details ">
505
- <h3 class="signature " id="payload-instance_method">
506
-
507
- - (<tt>Hash</tt>) <strong>payload</strong>
508
-
509
-
510
-
511
-
512
-
513
- </h3><div class="docstring">
514
- <div class="discussion">
515
-
516
- <p>Return a hash with a random content item</p>
517
-
518
- <p>Example:</p>
519
-
520
- <pre class="code ruby"><code class="ruby"><span class='const'>GovukSchemas</span><span class='op'>::</span><span class='const'>RandomExample</span><span class='period'>.</span><span class='id identifier rubyid_for_schema'>for_schema</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>detailed_guide</span><span class='tstring_end'>&quot;</span></span><span class='comma'>,</span> <span class='label'>schema_type:</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>frontend</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_payload'>payload</span>
521
- <span class='comment'># =&gt; {&quot;base_path&quot;=&gt;&quot;/e42dd28e&quot;, &quot;title&quot;=&gt;&quot;dolor est...&quot;, &quot;publishing_app&quot;=&gt;&quot;elit&quot;...}
522
- </span></code></pre>
523
-
524
-
525
- </div>
526
- </div>
527
- <div class="tags">
528
-
529
- <p class="tag_title">Returns:</p>
530
- <ul class="return">
531
-
532
- <li>
533
-
534
-
535
- <span class='type'>(<tt>Hash</tt>)</span>
536
-
537
-
538
-
539
- &mdash;
540
- <div class='inline'>
541
- <p>A content item</p>
542
- </div>
543
-
544
- </li>
545
-
546
- </ul>
547
-
548
- </div><table class="source_code">
549
- <tr>
550
- <td>
551
- <pre class="lines">
552
-
553
-
554
- 45
555
- 46
556
- 47
557
- 48
558
- 49
559
- 50
560
- 51
561
- 52
562
- 53
563
- 54</pre>
564
- </td>
565
- <td>
566
- <pre class="code"><span class="info file"># File 'lib/govuk_schemas/random_example.rb', line 45</span>
567
-
568
- <span class='kw'>def</span> <span class='id identifier rubyid_payload'>payload</span>
569
- <span class='id identifier rubyid_item'>item</span> <span class='op'>=</span> <span class='ivar'>@random_generator</span><span class='period'>.</span><span class='id identifier rubyid_payload'>payload</span>
570
- <span class='id identifier rubyid_errors'>errors</span> <span class='op'>=</span> <span class='id identifier rubyid_validation_errors_for'>validation_errors_for</span><span class='lparen'>(</span><span class='id identifier rubyid_item'>item</span><span class='rparen'>)</span>
571
-
572
- <span class='kw'>if</span> <span class='id identifier rubyid_errors'>errors</span><span class='period'>.</span><span class='id identifier rubyid_any?'>any?</span>
573
- <span class='id identifier rubyid_raise'>raise</span> <span class='const'>InvalidContentGenerated</span><span class='comma'>,</span> <span class='id identifier rubyid_error_message'>error_message</span><span class='lparen'>(</span><span class='id identifier rubyid_item'>item</span><span class='comma'>,</span> <span class='id identifier rubyid_errors'>errors</span><span class='rparen'>)</span>
574
- <span class='kw'>end</span>
575
-
576
- <span class='id identifier rubyid_item'>item</span>
577
- <span class='kw'>end</span></pre>
578
- </td>
579
- </tr>
580
- </table>
581
- </div>
582
-
583
- </div>
584
-
585
- </div>
586
-
587
- <div id="footer">
588
- Generated on Thu Nov 3 17:31:15 2016 by
589
- <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
590
- 0.8.7.6 (ruby-2.3.1).
591
- </div>
592
-
593
- </body>
594
- </html>