govuk_schemas 4.2.0 → 4.3.0

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
  SHA256:
3
- metadata.gz: 8a44876dfefc37456d8e08d8ba63a9bd95c07425f12d44ca51aafdbbd01e513c
4
- data.tar.gz: '0928c1cae2cf6eb2cf31adfb2eb2d4da30942178ac2e5b9bc2e3681aef3bce3b'
3
+ metadata.gz: 3b1010c2e3fc4d2b11f832cb7fc987d93bc31e911847ec6d18231e47a29a605f
4
+ data.tar.gz: 1c7c41a441859658c3f2e53ba4399374227120611150d14ca9a0d0f850e24c15
5
5
  SHA512:
6
- metadata.gz: 14265d1073c7a9b8efe6225d2f7e5508ea91fa4f25e04cb377afe09e9cd3423d98c3fcc319888773301e0f2e7e940b911c159cf83ee184f793f98d756d3909e3
7
- data.tar.gz: b1dd19d78453ac71cae823c987c558f18bcca9f6295908cfb96e6b084a7a85d0543ff72da724fd2c8fa6cc700adbce39b200fcca14ad113333b00a8260b3cc77
6
+ metadata.gz: 44e53cbd69991f6882e7d9a371257bef45fc100c75f945048e8700fdb9c17c9a69727219206e7ce720b5007c5412d7139472d370f856cc46fc1387453432f09b
7
+ data.tar.gz: 8ad00bb818a94cb75c5ae2d0e99ab754a204655a966a11c4acbed2e937c3015794a2847c9c5b6eb8567d2f724893c03a98fe2252c14b2499cc8d9a93b7b2e26b
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 4.3.0
2
+
3
+ * Generate unique items for arrays with the "uniqueItems" property. ([#63](https://github.com/alphagov/govuk_schemas/pull/63))
4
+
1
5
  # 4.2.0
2
6
 
3
7
  * Add support for generating random HH:MM time strings that match a regex. ([#62](https://github.com/alphagov/govuk_schemas/pull/62))
@@ -116,12 +116,27 @@ module GovukSchemas
116
116
  def generate_random_array(props)
117
117
  min = props["minItems"] || 0
118
118
  max = props["maxItems"] || 10
119
+ unique = props["uniqueItems"] == true
119
120
  num_items = @random.rand(min..max)
121
+ items = []
122
+ attempts = 0
123
+ max_attempts = num_items * 100
120
124
 
121
- num_items.times.map do
125
+ until items.length == num_items
122
126
  # sometimes arrays don't have `items` specified, not sure if this is a bug
123
- generate_value(props["items"] || {})
127
+ new_value = generate_value(props["items"] || {})
128
+
129
+ if unique && items.include?(new_value)
130
+ attempts += 1
131
+ raise "Failed to create a unique array item after #{max_attempts} attempts" if attempts >= max_attempts
132
+ next
133
+ end
134
+
135
+ attempts = 0
136
+ items << new_value
124
137
  end
138
+
139
+ items
125
140
  end
126
141
 
127
142
  def generate_random_string(props)
@@ -1,4 +1,4 @@
1
1
  module GovukSchemas
2
2
  # @private
3
- VERSION = "4.2.0".freeze
3
+ VERSION = "4.3.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: 4.2.0
4
+ version: 4.3.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: 2020-12-04 00:00:00.000000000 Z
11
+ date: 2021-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema