Skip to content

Commit 5e50023

Browse files
committed
using namespace/major.minor.patch format
1 parent 937c9a3 commit 5e50023

7 files changed

Lines changed: 36 additions & 17 deletions

File tree

lib/entitlements/data/groups/calculated/yaml.rb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,23 @@ def description
3737
# Standard interface: Get the schema version of this group.
3838
#
3939
# Takes no arguments.
40+
# Examples: "entitlements/v1", "entitlements/v1.2.3", "entitlements/1.2.3"
41+
# Format: namespace/major.minor.patch
4042
#
41-
# Returns a String with the schema version (semver), or "1.0.0" if undefined.
43+
# Returns a String with the schema version (k8s-style), or "entitlements/v1" if undefined.
4244
Contract C::None => String
4345
def schema_version
44-
version = parsed_data.fetch("schema_version", "1.0.0").to_s
45-
unless version.match?(/\A\d+\.\d+\.\d+\z/)
46-
raise "Invalid schema version format: #{version} - Expected format is 'MAJOR.MINOR.PATCH' " \
47-
"- Examples: 1.2.3 or 10.0.0"
46+
schema_version = parsed_data.fetch("schema_version", "entitlements/v1").to_s
47+
48+
namespace, version = schema_version.split("/")
49+
50+
unless namespace.match?(/\A[a-zA-Z0-9]+\z/) && version.match?(/\A(v?\d+(\.\d+){0,2})\z/)
51+
raise "Invalid schema version format: #{schema_version} - Expected format is '<namespace>/<version>' " \
52+
"- Examples: entitlements/v1, entitlements/v1.2.3, entitlements/1.2.3"
4853
end
49-
version
54+
55+
# rebuild the schema version string to ensure it's in the correct format and return it
56+
return "#{namespace}/#{version}"
5057
end
5158

5259
# Files can support modifiers that act independently of rules.

spec/unit/entitlements/data/groups/calculated/yaml_spec.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,38 @@
3939
it "returns the version string when one is set" do
4040
filename = fixture("ldap-config/filters/no-filters-with-schema-version.yaml")
4141
subject = described_class.new(filename: filename)
42-
expect(subject.schema_version).to eq("1.2.3")
42+
expect(subject.schema_version).to eq("entitlements/1.2.3")
4343
end
4444

45-
it "returns the version string when one is set without the patch - not valid semver 2" do
45+
it "returns the version string when one is set without the patch" do
4646
filename = fixture("ldap-config/filters/no-filters-with-schema-version-no-patch.yaml")
4747
subject = described_class.new(filename: filename)
48-
expect { subject.schema_version }.to raise_error(RuntimeError, /Invalid schema version format/)
48+
expect(subject.schema_version).to eq("entitlements/1.2")
4949
end
5050

51-
it "returns the version string when one is set (with v prefix - not valid semver 2)" do
51+
it "returns the version string when one is set (with v prefix)" do
5252
filename = fixture("ldap-config/filters/no-filters-with-schema-version-with-v.yaml")
5353
subject = described_class.new(filename: filename)
54-
expect { subject.schema_version }.to raise_error(RuntimeError, /Invalid schema version format/)
54+
expect(subject.schema_version).to eq("entitlements/v1.2.3")
5555
end
5656

5757
it "returns the default version when schema_version is undefined" do
5858
filename = fixture("ldap-config/filters/no-filters-description.yaml")
5959
subject = described_class.new(filename: filename)
60-
expect(subject.schema_version).to eq("1.0.0")
60+
expect(subject.schema_version).to eq("entitlements/v1")
6161
end
6262

63-
it "throws an error when an invalid semver string is provided" do
63+
it "throws an error when an invalid schema_version string is provided" do
6464
filename = fixture("ldap-config/filters/no-filters-with-bad-schema-version.yaml")
6565
subject = described_class.new(filename: filename)
6666
expect { subject.schema_version }.to raise_error(RuntimeError, /Invalid schema version format/)
6767
end
68+
69+
it "throws an error when the version string is missing the namespace" do
70+
filename = fixture("ldap-config/filters/no-filters-with-missing-version-namespace.yaml")
71+
subject = described_class.new(filename: filename)
72+
expect { subject.schema_version }.to raise_error(RuntimeError, /Invalid schema version format/)
73+
end
6874
end
6975

7076
describe "#initialize_filters" do

spec/unit/fixtures/ldap-config/filters/no-filters-with-bad-schema-version.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
description: Yo kittens
2-
schema_version: 1.abc2.3
2+
schema_version: entitlements/1.abc2.3
33
rules:
44
or:
55
- username: russianblue
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
description: Yo kittens
2+
schema_version: 1.2.3
3+
rules:
4+
or:
5+
- username: russianblue
6+
- username: BlackManx

spec/unit/fixtures/ldap-config/filters/no-filters-with-schema-version-no-patch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
description: Yo kittens
2-
schema_version: 1.2
2+
schema_version: entitlements/1.2
33
rules:
44
or:
55
- username: russianblue

spec/unit/fixtures/ldap-config/filters/no-filters-with-schema-version-with-v.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
description: Yo kittens
2-
schema_version: v1.2.3
2+
schema_version: entitlements/v1.2.3
33
rules:
44
or:
55
- username: russianblue

spec/unit/fixtures/ldap-config/filters/no-filters-with-schema-version.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
description: Yo kittens
2-
schema_version: 1.2.3
2+
schema_version: entitlements/1.2.3
33
rules:
44
or:
55
- username: russianblue

0 commit comments

Comments
 (0)