Skip to content

Commit

Permalink
use common validators
Browse files Browse the repository at this point in the history
  • Loading branch information
nieomylnieja committed Oct 4, 2023
1 parent 0bf8be2 commit ae25dc0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 60 deletions.
26 changes: 4 additions & 22 deletions manifest/v1alpha/project/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,10 @@ import (
)

var validateProject = validation.RulesForStruct[Project](
// Example: my-name
validation.RulesForField(
"metadata.name",
func(p Project) string { return p.Metadata.Name },
).With(
validation.StringRequired(),
validation.StringIsDNSSubdomain()),
// Example: My Name!
validation.RulesForField(
"metadata.displayName",
func(p Project) string { return p.Metadata.DisplayName },
).With(validation.StringLength(0, 63)),
// Example: this:that,hey=lol
validation.RulesForField(
"metadata.labels",
func(p Project) v1alpha.Labels { return p.Metadata.Labels },
).With(v1alpha.ValidationRuleLabels()),
// Example: Very long description
validation.RulesForField(
"spec.description",
func(p Project) string { return p.Spec.Description },
).With(validation.StringDescription()),
v1alpha.FieldRuleMetadataName(func(p Project) string { return p.Metadata.Name }),
v1alpha.FieldRuleMetadataDisplayName(func(p Project) string { return p.Metadata.DisplayName }),
v1alpha.FieldRuleMetadataLabels(func(p Project) v1alpha.Labels { return p.Metadata.Labels }),
v1alpha.FieldRuleSpecDescription(func(p Project) string { return p.Spec.Description }),
).Validate

func validate(p Project) error {
Expand Down
41 changes: 9 additions & 32 deletions manifest/v1alpha/service/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,16 @@ import (
"github.com/nobl9/nobl9-go/validation"
)

var validateService = validation.RulesForStruct[Service](
v1alpha.FieldRuleMetadataName(func(s Service) string { return s.Metadata.Name }),
v1alpha.FieldRuleMetadataDisplayName(func(s Service) string { return s.Metadata.DisplayName }),
v1alpha.FieldRuleMetadataProject(func(s Service) string { return s.Metadata.Project }),
v1alpha.FieldRuleMetadataLabels(func(s Service) v1alpha.Labels { return s.Metadata.Labels }),
v1alpha.FieldRuleSpecDescription(func(s Service) string { return s.Spec.Description }),
).Validate

func validate(s Service) error {
v := validation.RulesForStruct(
validation.RulesForField[string](
"metadata.name",
func() string { return s.Metadata.Name },
).
With(
validation.StringRequired(),
validation.StringIsDNSSubdomain()),
validation.RulesForField[string](
"metadata.displayName",
func() string { return s.Metadata.DisplayName },
).
With(validation.StringLength(0, 63)),
validation.RulesForField[string](
"metadata.project",
func() string { return s.Metadata.Project },
).
With(
validation.StringRequired(),
validation.StringIsDNSSubdomain()),
validation.RulesForField[v1alpha.Labels](
"metadata.labels",
func() v1alpha.Labels { return s.Metadata.Labels },
).
With(v1alpha.ValidationRule()),
validation.RulesForField[string](
"spec.description",
func() string { return s.Spec.Description },
).
With(validation.StringDescription()),
)
if errs := v.Validate(); len(errs) > 0 {
if errs := validateService(s); len(errs) > 0 {
return v1alpha.NewObjectError(s, errs)
}
return nil
Expand Down
12 changes: 6 additions & 6 deletions manifest/v1alpha/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ package v1alpha

import "github.com/nobl9/nobl9-go/validation"

func FieldRuleMetdataName(getter func() string) validation.FieldRules[string] {
func FieldRuleMetadataName[S any](getter func(S) string) validation.FieldRules[string, S] {
return validation.RulesForField[string]("metadata.name", getter).
With(validation.StringRequired(), validation.StringIsDNSSubdomain())
}

func FieldRuleMetadataDisplayName(getter func() string) validation.FieldRules[string] {
func FieldRuleMetadataDisplayName[S any](getter func(S) string) validation.FieldRules[string, S] {
return validation.RulesForField[string]("metadata.displayName", getter).
With(validation.StringLength(0, 63))
}

func FieldRuleMetdataProject(getter func() string) validation.FieldRules[string] {
func FieldRuleMetadataProject[S any](getter func(S) string) validation.FieldRules[string, S] {
return validation.RulesForField[string]("metadata.project", getter).
With(validation.StringRequired(), validation.StringIsDNSSubdomain())
}

func FieldRuleMetadataLabels(getter func() Labels) validation.FieldRules[Labels] {
func FieldRuleMetadataLabels[S any](getter func(S) Labels) validation.FieldRules[Labels, S] {
return validation.RulesForField[Labels]("metadata.labels", getter).
With(ValidationRule())
With(ValidationRuleLabels())
}

func FieldRuleMetadataDescription(getter func() string) validation.FieldRules[string] {
func FieldRuleSpecDescription[S any](getter func(S) string) validation.FieldRules[string, S] {
return validation.RulesForField[string]("spec.description", getter).
With(validation.StringDescription())
}

0 comments on commit ae25dc0

Please sign in to comment.