govuk_schemas 2.0.0 → 2.1.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: 5b6cda522703d0896cf14a762388caa66fbca5c0
4
- data.tar.gz: ca5ca470c2662ba182b283584c58ede459465720
3
+ metadata.gz: ac6896c57c2532adcb41b53a7299bc0199aee11e
4
+ data.tar.gz: 8aae891ff22402cff6c858b33490edaaaf326a7b
5
5
  SHA512:
6
- metadata.gz: 761d1a395fcd5c9051f162d68398d9ff88861493cf0947eb50292d333561ca591d37261b7668c52403c7bce3cc8b4b346f659285350291753a41f1a5d01560a0
7
- data.tar.gz: 8a11fcaed55550bec45183185789c09a0394db87d2ab9b6656b2b84c29b20c3044002487043ec20aca08af479d7012694a1a3aa89a5f3ad049fc924a2e9f4916
6
+ metadata.gz: fb53487240a352345f504c932aee0b4895ae5aa8fb71aa91709563983fa3a6c0a06767009f8798dc126952655d865e4cff39ab1a242cba294e05d0b19ed97ad1
7
+ data.tar.gz: dd87037cb8ea2887b1397ecf55ccdbe45654067b4d834c3a854c7cab36eb9cc54260d64b0e43a31d6abdc302e47bdcfcbe507952722aaac6fbfa8ec979b77fb9
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 2.1.0
2
+
3
+ * Add `Schema.schema_names` to return all schema names
4
+ * Add functionality to work with examples
5
+
1
6
  # 2.0.0
2
7
 
3
8
  * Change GovukSchemas::Schema.find to take { links_schema: 'detailded_guide' }
@@ -0,0 +1,25 @@
1
+ module GovukSchemas
2
+ class Example
3
+ # Find all examples for a schema
4
+ #
5
+ # @param schema_name [String] like "detailed_guide", "policy" or "publication"
6
+ # @return [Array] array of example content items
7
+ def self.find_all(schema_name)
8
+ Dir.glob("#{GovukSchemas::CONTENT_SCHEMA_DIR}/formats/#{schema_name}/frontend/examples/*.json").map do |filename|
9
+ json = File.read(filename)
10
+ JSON.parse(json)
11
+ end
12
+ end
13
+
14
+ # Find an example by name
15
+ #
16
+ # @param schema_name [String] like "detailed_guide", "policy" or "publication"
17
+ # @param example_name [String] the name of the example JSON file
18
+ # @return [Hash] the example content item
19
+ def self.find(schema_name, example_name:)
20
+ path = "/formats/#{schema_name}/frontend/examples/#{example_name}.json"
21
+ json = File.read("#{GovukSchemas::CONTENT_SCHEMA_DIR}#{path}")
22
+ JSON.parse(json)
23
+ end
24
+ end
25
+ end
@@ -65,15 +65,25 @@ module GovukSchemas
65
65
  # @param [Hash] hash The hash to merge the random content with
66
66
  # @return [Hash] A content item
67
67
  # @raise [GovukSchemas::InvalidContentGenerated]
68
- def merge_and_validate(hash)
69
- item = payload.merge(Utils.stringify_keys(hash))
70
- errors = validation_errors_for(item)
68
+ def merge_and_validate(user_defined_values)
69
+ random_payload = @random_generator.payload
70
+ item_merged_with_user_input = random_payload.merge(Utils.stringify_keys(user_defined_values))
71
71
 
72
+ errors = validation_errors_for(item_merged_with_user_input)
72
73
  if errors.any?
73
- raise InvalidContentGenerated, error_message_custom(item, hash, errors)
74
+
75
+ errors_on_random_payload = validation_errors_for(random_payload)
76
+ if errors_on_random_payload.any?
77
+ # The original item was invalid when it was generated, so it's not
78
+ # the users fault.
79
+ raise InvalidContentGenerated, error_message(random_payload, errors)
80
+ else
81
+ # The random item was valid, but it was merged with something invalid.
82
+ raise InvalidContentGenerated, error_message_custom(item_merged_with_user_input, user_defined_values, errors)
83
+ end
74
84
  end
75
85
 
76
- item
86
+ item_merged_with_user_input
77
87
  end
78
88
 
79
89
  private
@@ -35,6 +35,15 @@ module GovukSchemas
35
35
  all(schema_type: schema_type).values.sample
36
36
  end
37
37
 
38
+ # Return all schema names
39
+ #
40
+ # @return [Array] all the schema names
41
+ def self.schema_names
42
+ Dir.glob("#{GovukSchemas::CONTENT_SCHEMA_DIR}/dist/formats/*").map do |directory|
43
+ File.basename(directory)
44
+ end
45
+ end
46
+
38
47
  # @private
39
48
  def self.location_for_schema_name(schema)
40
49
  type, schema_name = schema.to_a.flatten
@@ -1,4 +1,4 @@
1
1
  module GovukSchemas
2
2
  # @private
3
- VERSION = "2.0.0".freeze
3
+ VERSION = "2.1.0".freeze
4
4
  end
data/lib/govuk_schemas.rb CHANGED
@@ -2,6 +2,7 @@ require "govuk_schemas/version"
2
2
  require "govuk_schemas/schema"
3
3
  require "govuk_schemas/utils"
4
4
  require "govuk_schemas/random_example"
5
+ require "govuk_schemas/example"
5
6
 
6
7
  module GovukSchemas
7
8
  # @private
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.0.0
4
+ version: 2.1.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-11-04 00:00:00.000000000 Z
11
+ date: 2016-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema
@@ -146,6 +146,7 @@ files:
146
146
  - jenkins-schema.sh
147
147
  - jenkins.sh
148
148
  - lib/govuk_schemas.rb
149
+ - lib/govuk_schemas/example.rb
149
150
  - lib/govuk_schemas/random.rb
150
151
  - lib/govuk_schemas/random_example.rb
151
152
  - lib/govuk_schemas/random_item_generator.rb