From f688d4a1476d29f4c276bb61a9e64f61e91307f8 Mon Sep 17 00:00:00 2001 From: Marcus Kimpenhaus Date: Fri, 29 May 2026 20:40:25 +0200 Subject: [PATCH] fix: enable validation rules to be placed on classes should fix #1122 --- .../operator/building-blocks/entities.mdx | 2 +- .../Attributes/ValidationRuleAttribute.cs | 2 +- src/KubeOps.Transpiler/Crds.cs | 38 +++++--- .../Crds.Mlc.ValidationRule.Test.cs | 87 +++++++++++++++++++ 4 files changed, 116 insertions(+), 13 deletions(-) diff --git a/docs/docs/operator/building-blocks/entities.mdx b/docs/docs/operator/building-blocks/entities.mdx index 76ad692be..2f0c27214 100644 --- a/docs/docs/operator/building-blocks/entities.mdx +++ b/docs/docs/operator/building-blocks/entities.mdx @@ -137,7 +137,7 @@ public class EntitySpec - `[RangeMinimum]` and `[RangeMaximum]`: Defines numeric value ranges - `[MultipleOf]`: Specifies that a number must be a multiple of a given value - `[Items]`: Defines minimum and maximum items for arrays -- `[ValidationRule]`: Defines custom validation rules using CEL expressions +- `[ValidationRule]`: Defines custom validation rules using CEL expressions. Can be applied to properties and to class types — in both cases it emits `x-kubernetes-validations` on the corresponding schema node. When applied to both a class and a property of that type, the rules are merged (class rules first, then property rules) ### Documentation Attributes diff --git a/src/KubeOps.Abstractions/Entities/Attributes/ValidationRuleAttribute.cs b/src/KubeOps.Abstractions/Entities/Attributes/ValidationRuleAttribute.cs index 67f515f88..e9dfa6f12 100644 --- a/src/KubeOps.Abstractions/Entities/Attributes/ValidationRuleAttribute.cs +++ b/src/KubeOps.Abstractions/Entities/Attributes/ValidationRuleAttribute.cs @@ -37,7 +37,7 @@ namespace KubeOps.Abstractions.Entities.Attributes; /// /// reason provides a machine-readable validation failure reason that is returned to the caller when a request fails this validation rule. /// -[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)] +[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, AllowMultiple = true)] public sealed class ValidationRuleAttribute( string rule, string? fieldPath = null, diff --git a/src/KubeOps.Transpiler/Crds.cs b/src/KubeOps.Transpiler/Crds.cs index 1231581e6..acf440d2c 100644 --- a/src/KubeOps.Transpiler/Crds.cs +++ b/src/KubeOps.Transpiler/Crds.cs @@ -111,6 +111,8 @@ public static V1CustomResourceDefinition Transpile(this MetadataLoadContext cont { Count: > 0 } list => list, _ => null, }, + XKubernetesValidations = context.MapValidationRules( + type.GetCustomAttributesData()), }, }; @@ -309,18 +311,12 @@ private static V1JSONSchemaProps Map(this MetadataLoadContext context, PropertyI props.Properties = null; } - if (prop.GetCustomAttributesData().ToArray() is { Length: > 0 } validations) + var propValidations = context.MapValidationRules(prop.GetCustomAttributesData()); + if (propValidations != null) { - props.XKubernetesValidations = validations - .Select(validation => new V1ValidationRule - { - Rule = validation.GetCustomAttributeCtorArg(context, 0), - FieldPath = validation.GetCustomAttributeCtorArg(context, 1), - Message = validation.GetCustomAttributeCtorArg(context, 2), - MessageExpression = validation.GetCustomAttributeCtorArg(context, 3), - Reason = validation.GetCustomAttributeCtorArg(context, 4), - }) - .ToList(); + props.XKubernetesValidations = props.XKubernetesValidations is { } existing + ? [.. existing, .. propValidations] + : propValidations; } return props; @@ -411,6 +407,24 @@ static Type GetRootBaseType(Type type) }; } + private static List? MapValidationRules( + this MetadataLoadContext context, + IEnumerable attributesData) + { + var rules = attributesData + .Select(v => new V1ValidationRule + { + Rule = v.GetCustomAttributeCtorArg(context, 0), + FieldPath = v.GetCustomAttributeCtorArg(context, 1), + Message = v.GetCustomAttributeCtorArg(context, 2), + MessageExpression = v.GetCustomAttributeCtorArg(context, 3), + Reason = v.GetCustomAttributeCtorArg(context, 4), + }) + .ToList(); + + return rules.Count > 0 ? rules : null; + } + private static IList GetEnumNames(this MetadataLoadContext context, Type type) { #if NET9_0_OR_GREATER @@ -484,6 +498,8 @@ private static V1JSONSchemaProps MapObjectType(this MetadataLoadContext context, }, XKubernetesPreserveUnknownFields = type.GetCustomAttributeData() != null ? true : null, + XKubernetesValidations = context.MapValidationRules( + type.GetCustomAttributesData()), }; } } diff --git a/test/KubeOps.Transpiler.Test/Crds.Mlc.ValidationRule.Test.cs b/test/KubeOps.Transpiler.Test/Crds.Mlc.ValidationRule.Test.cs index 8615e74b9..3823f02c9 100644 --- a/test/KubeOps.Transpiler.Test/Crds.Mlc.ValidationRule.Test.cs +++ b/test/KubeOps.Transpiler.Test/Crds.Mlc.ValidationRule.Test.cs @@ -64,6 +64,53 @@ public void Should_Omit_Validations() specProperties.XKubernetesValidations.Should().BeNull(); } + [Fact] + public void Should_Set_Validations_On_Root_Class() + { + var crd = _mlc.Transpile(typeof(RootClassValidateAttrEntity)); + + var schema = crd.Spec.Versions[0].Schema.OpenAPIV3Schema; + schema.XKubernetesValidations.Should().HaveCount(1); + schema.XKubernetesValidations[0].Rule.Should().Be(Rule1); + schema.XKubernetesValidations[0].Message.Should().Be(Message1); + schema.XKubernetesValidations[0].MessageExpression.Should().BeNull(); + schema.XKubernetesValidations[0].FieldPath.Should().BeNull(); + schema.XKubernetesValidations[0].Reason.Should().BeNull(); + } + + [Fact] + public void Should_Set_Multiple_Validations_On_Root_Class() + { + var crd = _mlc.Transpile(typeof(MultiRootClassValidateAttrEntity)); + + var schema = crd.Spec.Versions[0].Schema.OpenAPIV3Schema; + schema.XKubernetesValidations.Should().HaveCount(2); + schema.XKubernetesValidations[0].Rule.Should().Be(Rule1); + schema.XKubernetesValidations[1].Rule.Should().Be(Rule2); + } + + [Fact] + public void Should_Set_Validations_On_Nested_Class() + { + var crd = _mlc.Transpile(typeof(NestedClassValidateAttrEntity)); + + var nestedSchema = crd.Spec.Versions[0].Schema.OpenAPIV3Schema.Properties["nested"]; + nestedSchema.XKubernetesValidations.Should().HaveCount(1); + nestedSchema.XKubernetesValidations[0].Rule.Should().Be(Rule1); + nestedSchema.XKubernetesValidations[0].Message.Should().Be(Message1); + } + + [Fact] + public void Should_Merge_Class_And_Property_Validations() + { + var crd = _mlc.Transpile(typeof(ClassAndPropertyValidateAttrEntity)); + + var nestedSchema = crd.Spec.Versions[0].Schema.OpenAPIV3Schema.Properties["nested"]; + nestedSchema.XKubernetesValidations.Should().HaveCount(2); + nestedSchema.XKubernetesValidations[0].Rule.Should().Be(Rule1); + nestedSchema.XKubernetesValidations[1].Rule.Should().Be(Rule2); + } + [Fact] public void Should_Set_ValidationFields() { @@ -86,6 +133,46 @@ public sealed class NoValidateAttrEntity : CustomKubernetesEntity public string Property { get; set; } = null!; } + [KubernetesEntity(Group = "testing.dev", ApiVersion = "v1", Kind = "TestEntity")] + [ValidationRule(Rule1, message: Message1)] + public sealed class RootClassValidateAttrEntity : CustomKubernetesEntity + { + public string Property { get; set; } = null!; + } + + [KubernetesEntity(Group = "testing.dev", ApiVersion = "v1", Kind = "TestEntity")] + [ValidationRule(Rule1, message: Message1)] + [ValidationRule(Rule2, message: Message2)] + public sealed class MultiRootClassValidateAttrEntity : CustomKubernetesEntity + { + public string Property { get; set; } = null!; + } + + [KubernetesEntity(Group = "testing.dev", ApiVersion = "v1", Kind = "TestEntity")] + public sealed class NestedClassValidateAttrEntity : CustomKubernetesEntity + { + public NestedObject Nested { get; set; } = new(); + + [ValidationRule(Rule1, message: Message1)] + public sealed class NestedObject + { + public string Name { get; set; } = null!; + } + } + + [KubernetesEntity(Group = "testing.dev", ApiVersion = "v1", Kind = "TestEntity")] + public sealed class ClassAndPropertyValidateAttrEntity : CustomKubernetesEntity + { + [ValidationRule(Rule2, message: Message2)] + public NestedWithClassRule Nested { get; set; } = new(); + + [ValidationRule(Rule1, message: Message1)] + public sealed class NestedWithClassRule + { + public string Name { get; set; } = null!; + } + } + [KubernetesEntity(Group = "testing.dev", ApiVersion = "v1", Kind = "TestEntity")] public sealed class SingleValidateAttrEntity : CustomKubernetesEntity {