diff --git a/manifest/v1alpha/slo/test_data/expected_error.txt b/manifest/v1alpha/slo/test_data/expected_error.txt index 326049c17..b35cf4989 100644 --- a/manifest/v1alpha/slo/test_data/expected_error.txt +++ b/manifest/v1alpha/slo/test_data/expected_error.txt @@ -1,14 +1,12 @@ -Validation for Service 'MY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICE' in project 'MY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECT' has failed for the following fields: - - 'metadata.name' with value 'MY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICE...': +Validation for SLO 'MY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLO' in project 'MY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECT' has failed for the following fields: + - 'spec.description' with value 'llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll...': + - length must be between 0 and 1050 + - '.metadata.name' with value 'MY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY S...': - length must be between 1 and 63 - a DNS-1123 compliant name must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name', or '123-abc', regex used for validation is '^[a-z0-9]([-a-z0-9]*[a-z0-9])?$') - - 'metadata.displayName' with value 'my-servicemy-servicemy-servicemy-servicemy-servicemy-servicemy-servicemy-servicemy-servicemy-service': - - length must be between 0 and 63 - - 'metadata.project' with value 'MY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECT...': + - '.metadata.project' with value 'MY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECT...': - length must be between 1 and 63 - a DNS-1123 compliant name must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name', or '123-abc', regex used for validation is '^[a-z0-9]([-a-z0-9]*[a-z0-9])?$') - - 'metadata.labels' with value '{"L O L":["dip","dip"]}': + - '.metadata.labels' with value '{"L O L":["dip","dip"]}': - label key 'L O L' does not match the regex: ^\p{L}([_\-0-9\p{L}]*[0-9\p{L}])?$ - - 'spec.description' with value 'llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll...': - - length must be between 0 and 1050 -Manifest source: /home/me/service.yaml \ No newline at end of file +Manifest source: /home/me/slo.yaml \ No newline at end of file diff --git a/manifest/v1alpha/slo/validation.go b/manifest/v1alpha/slo/validation.go index bc0f252e0..a9d317232 100644 --- a/manifest/v1alpha/slo/validation.go +++ b/manifest/v1alpha/slo/validation.go @@ -6,10 +6,15 @@ import ( ) var sloValidation = validation.New[SLO]( - v1alpha.FieldRuleMetadataName(func(s SLO) string { return s.Metadata.Name }), - v1alpha.FieldRuleMetadataDisplayName(func(s SLO) string { return s.Metadata.DisplayName }), - v1alpha.FieldRuleMetadataLabels(func(s SLO) v1alpha.Labels { return s.Metadata.Labels }), v1alpha.FieldRuleSpecDescription(func(s SLO) string { return s.Spec.Description }), + validation.RulesFor(func(s SLO) Metadata { return s.Metadata }).Include(sloMetadataValidation), +) + +var sloMetadataValidation = validation.New[Metadata]( + v1alpha.FieldRuleMetadataName(func(m Metadata) string { return m.Name }), + v1alpha.FieldRuleMetadataDisplayName(func(m Metadata) string { return m.DisplayName }), + v1alpha.FieldRuleMetadataProject(func(m Metadata) string { return m.Project }), + v1alpha.FieldRuleMetadataLabels(func(m Metadata) v1alpha.Labels { return m.Labels }), ) func validate(s SLO) error { diff --git a/manifest/v1alpha/slo/validation_test.go b/manifest/v1alpha/slo/validation_test.go index 3ba206ea3..4ada49cfb 100644 --- a/manifest/v1alpha/slo/validation_test.go +++ b/manifest/v1alpha/slo/validation_test.go @@ -16,10 +16,11 @@ var expectedError string func TestValidate_AllErrors(t *testing.T) { err := validate(SLO{ - Kind: manifest.KindProject, + Kind: manifest.KindSLO, Metadata: Metadata{ - Name: strings.Repeat("MY PROJECT", 20), - DisplayName: strings.Repeat("my-project", 10), + Name: strings.Repeat("MY SLO", 20), + DisplayName: strings.Repeat("my-slo", 10), + Project: strings.Repeat("MY PROJECT", 20), Labels: v1alpha.Labels{ "L O L": []string{"dip", "dip"}, }, @@ -27,7 +28,7 @@ func TestValidate_AllErrors(t *testing.T) { Spec: Spec{ Description: strings.Repeat("l", 2000), }, - ManifestSource: "/home/me/project.yaml", + ManifestSource: "/home/me/slo.yaml", }) assert.Equal(t, expectedError, err.Error()) }