govuk_schemas 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,24 +1,46 @@
1
1
  require "govuk_schemas/random"
2
2
  require "govuk_schemas/random_item_generator"
3
- require "active_support/core_ext/hash"
4
3
  require "json-schema"
5
4
  require "json"
6
5
 
7
6
  module GovukSchemas
8
7
  class RandomExample
9
- # TODO: add docs
8
+ # Returns a new `GovukSchemas::RandomExample` object.
9
+ #
10
+ # For example:
11
+ #
12
+ # schema = GovukSchemas::Schema.find("detailed_guide", schema_type: "frontend")
13
+ # GovukSchemas::RandomExample.new(schema).payload
14
+ #
15
+ # @param [Hash] schema A JSON schema.
16
+ # @return [GovukSchemas::RandomExample]
10
17
  def initialize(schema:)
11
18
  @schema = schema
12
19
  @random_generator = RandomItemGenerator.new(schema: schema)
13
20
  end
14
21
 
15
- # TODO: add docs
22
+ # Returns a new `GovukSchemas::RandomExample` object.
23
+ #
24
+ # For example:
25
+ #
26
+ # generator = GovukSchemas::RandomExample.for_schema("detailed_guide", schema_type: "frontend")
27
+ # generator.payload
28
+ # # => {"base_path"=>"/e42dd28e", "title"=>"dolor est...", "publishing_app"=>"elit"...}
29
+ #
30
+ # @return [GovukSchemas::RandomExample]
16
31
  def self.for_schema(schema_name, schema_type:)
17
32
  schema = GovukSchemas::Schema.find(schema_name, schema_type: schema_type)
18
33
  GovukSchemas::RandomExample.new(schema: schema)
19
34
  end
20
35
 
21
- # TODO: add docs
36
+ # Return a hash with a random content item
37
+ #
38
+ # Example:
39
+ #
40
+ # GovukSchemas::RandomExample.for_schema("detailed_guide", schema_type: "frontend").payload
41
+ # # => {"base_path"=>"/e42dd28e", "title"=>"dolor est...", "publishing_app"=>"elit"...}
42
+ #
43
+ # @return [Hash] A content item
22
44
  def payload
23
45
  item = @random_generator.payload
24
46
  errors = validation_errors_for(item)
@@ -30,9 +52,20 @@ module GovukSchemas
30
52
  item
31
53
  end
32
54
 
33
- # TODO: add docs
55
+ # Return a content item merged with a hash. If the resulting content item
56
+ # isn't valid against the schema an error will be raised.
57
+ #
58
+ # Example:
59
+ #
60
+ # random = GovukSchemas::RandomExample.for_schema("detailed_guide", schema_type: "frontend")
61
+ # random.merge_and_validate(base_path: "/foo")
62
+ # # => {"base_path"=>"/e42dd28e", "title"=>"dolor est...", "publishing_app"=>"elit"...}
63
+ #
64
+ # @param [Hash] hash The hash to merge the random content with
65
+ # @return [Hash] A content item
66
+ # @raise [GovukSchemas::InvalidContentGenerated]
34
67
  def merge_and_validate(hash)
35
- item = payload.merge(hash.stringify_keys)
68
+ item = payload.merge(Utils.stringify_keys(hash))
36
69
  errors = validation_errors_for(item)
37
70
 
38
71
  if errors.any?
@@ -7,6 +7,8 @@ module GovukSchemas
7
7
  # The "randomness" here is quote relative, it's particularly tailored to the
8
8
  # GOV.UK content schemas. For example, strings are limited to a couple of
9
9
  # hundred characters to keep the resulting items small.
10
+ #
11
+ # @private
10
12
  class RandomItemGenerator
11
13
  def initialize(schema:)
12
14
  @schema = schema
@@ -19,6 +21,13 @@ module GovukSchemas
19
21
  private
20
22
 
21
23
  def generate_value(props)
24
+ # TODO: #/definitions/nested_headers are recursively nested and can cause
25
+ # infinite loops. We need to add something that detects and prevents the
26
+ # loop. In the meantime return a valid value.
27
+ if props['$ref'] == "#/definitions/nested_headers"
28
+ return [{ "text" => "1", "level" => 1, "id" => "ABC" }]
29
+ end
30
+
22
31
  # JSON schemas can have "pointers". We use this to extract defintions and
23
32
  # reduce duplication. To make the schema easily parsable we inline the
24
33
  # reference here.
@@ -39,9 +48,13 @@ module GovukSchemas
39
48
  type = Array(type).sample
40
49
 
41
50
  if props['anyOf']
42
- # TODO: this should generate values for 1 or more values, instead of just one
43
51
  generate_value(props['anyOf'].sample)
44
52
  elsif props['oneOf']
53
+ # FIXME: Generating valid data for a `oneOf` schema is quite interesting.
54
+ # According to the JSON Schema spec a `oneOf` schema is only valid if
55
+ # the data is valid against *only one* of the clauses. To do this
56
+ # properly, we'd have to verify that the data generated below doesn't
57
+ # validate against the other schemas in `props['oneOf']`.
45
58
  generate_value(props['oneOf'].sample)
46
59
  elsif props['allOf']
47
60
  props['allOf'].each_with_object({}) do |subschema, hash|
@@ -3,7 +3,7 @@ module GovukSchemas
3
3
  # Find a schema by name
4
4
  #
5
5
  # @param schema_name [String] Name of the schema/format
6
- # @param schema_type [String] The type: frontend, backend or links
6
+ # @param schema_type [String] The type: frontend, publisher, notification or links
7
7
  def self.find(schema_name, schema_type:)
8
8
  schema_type = "publisher_v2" if schema_type == "publisher"
9
9
  file_path = "#{GovukSchemas::CONTENT_SCHEMA_DIR}/dist/formats/#{schema_name}/#{schema_type}/schema.json"
@@ -11,11 +11,21 @@ module GovukSchemas
11
11
  end
12
12
 
13
13
  # Return all schemas in a hash, keyed by schema name
14
- def self.all
15
- Dir.glob("#{GovukSchemas::CONTENT_SCHEMA_DIR}/dist/**/*.json").reduce({}) do |hash, file_path|
14
+ #
15
+ # @param schema_type [String] The type: frontend, publisher, notification or links
16
+ def self.all(schema_type: '*')
17
+ schema_type = "publisher_v2" if schema_type == "publisher"
18
+ Dir.glob("#{GovukSchemas::CONTENT_SCHEMA_DIR}/dist/formats/*/#{schema_type}/*.json").reduce({}) do |hash, file_path|
16
19
  hash[file_path] = JSON.parse(File.read(file_path))
17
20
  hash
18
21
  end
19
22
  end
23
+
24
+ # Return a random schema of a certain type
25
+ #
26
+ # @param schema_type [String] The type: frontend, publisher, notification or links
27
+ def self.random_schema(schema_type:)
28
+ all(schema_type: schema_type).values.sample
29
+ end
20
30
  end
21
31
  end
@@ -0,0 +1,16 @@
1
+ module GovukSchemas
2
+ # @private
3
+ module Utils
4
+ def self.stringify_keys(hash)
5
+ new_hash = {}
6
+ hash.each do |k, v|
7
+ new_hash[k.to_s] = v
8
+ end
9
+ new_hash
10
+ end
11
+
12
+ def self.parameterize(string)
13
+ string.gsub(/[^a-z0-9\-_]+/i, '-')
14
+ end
15
+ end
16
+ end
@@ -1,3 +1,4 @@
1
1
  module GovukSchemas
2
- VERSION = "0.1.0".freeze
2
+ # @private
3
+ VERSION = "0.2.0".freeze
3
4
  end
metadata CHANGED
@@ -1,43 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: govuk_schemas
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.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: 2016-09-20 00:00:00.000000000 Z
11
+ date: 2016-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: activesupport
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
17
+ - - "~>"
32
18
  - !ruby/object:Gem::Version
33
- version: '0'
19
+ version: 2.5.0
34
20
  type: :runtime
35
21
  prerelease: false
36
22
  version_requirements: !ruby/object:Gem::Requirement
37
23
  requirements:
38
- - - ">="
24
+ - - "~>"
39
25
  - !ruby/object:Gem::Version
40
- version: '0'
26
+ version: 2.5.0
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: rake
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -108,6 +94,20 @@ dependencies:
108
94
  - - "~>"
109
95
  - !ruby/object:Gem::Version
110
96
  version: 1.2.1
97
+ - !ruby/object:Gem::Dependency
98
+ name: yard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.8'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.8'
111
111
  description: Gem to generate test data based on GOV.UK content schemas
112
112
  email:
113
113
  - govuk-dev@digital.cabinet-office.gov.uk
@@ -119,11 +119,29 @@ files:
119
119
  - ".rspec"
120
120
  - ".rubocop.yml"
121
121
  - ".ruby-version"
122
+ - CHANGELOG.md
122
123
  - Gemfile
123
124
  - LICENSE.md
124
125
  - README.md
125
126
  - Rakefile
126
127
  - bin/console
128
+ - docs/GovukSchemas.html
129
+ - docs/GovukSchemas/RandomExample.html
130
+ - docs/GovukSchemas/Schema.html
131
+ - docs/_index.html
132
+ - docs/class_list.html
133
+ - docs/css/common.css
134
+ - docs/css/full_list.css
135
+ - docs/css/style.css
136
+ - docs/file.README.html
137
+ - docs/file_list.html
138
+ - docs/frames.html
139
+ - docs/index.html
140
+ - docs/js/app.js
141
+ - docs/js/full_list.js
142
+ - docs/js/jquery.js
143
+ - docs/method_list.html
144
+ - docs/top-level-namespace.html
127
145
  - govuk_schemas.gemspec
128
146
  - jenkins-schema.sh
129
147
  - jenkins.sh
@@ -132,6 +150,7 @@ files:
132
150
  - lib/govuk_schemas/random_example.rb
133
151
  - lib/govuk_schemas/random_item_generator.rb
134
152
  - lib/govuk_schemas/schema.rb
153
+ - lib/govuk_schemas/utils.rb
135
154
  - lib/govuk_schemas/version.rb
136
155
  homepage: https://github.com/alphagov/govuk_schemas_gem
137
156
  licenses: