Skip to content

Commit

Permalink
Add validation marker
Browse files Browse the repository at this point in the history
  • Loading branch information
MahmoudShakour committed Mar 5, 2025
1 parent 1031b6d commit c36aad1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/simpleschema/markers.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ const (
MarkerTypeMinimum MarkerType = "minimum"
// MarkerTypeMaximum represents the `maximum` marker.
MarkerTypeMaximum MarkerType = "maximum"
// MarkerTypeValidation represents the `validation` marker.
MarkerTypeValidation MarkerType = "validation"
)

func markerTypeFromString(s string) (MarkerType, error) {
switch MarkerType(s) {
case MarkerTypeRequired, MarkerTypeDefault, MarkerTypeDescription,
MarkerTypeMinimum, MarkerTypeMaximum:
MarkerTypeMinimum, MarkerTypeMaximum, MarkerTypeValidation:
return MarkerType(s), nil
default:
return "", fmt.Errorf("unknown marker type: %s", s)
Expand Down
9 changes: 9 additions & 0 deletions pkg/simpleschema/markers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ func TestParseMarkers(t *testing.T) {
},
wantErr: false,
},
{
name: "validation marker with one value",
input: "validation=\"oldself=self\" required=true",
want: []*Marker{
{MarkerType: MarkerTypeValidation, Key: "validation", Value: "oldself=self"},
{MarkerType: MarkerTypeRequired, Key: "required", Value: "true"},
},
wantErr: false,
},
}

for _, tt := range tests {
Expand Down
7 changes: 7 additions & 0 deletions pkg/simpleschema/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,13 @@ func (tf *transformer) applyMarkers(schema *extv1.JSONSchemaProps, markers []*Ma
if val, err := strconv.ParseFloat(marker.Value, 64); err == nil {
schema.Maximum = &val
}
case MarkerTypeValidation:
newRule := extv1.ValidationRule{
Rule: marker.Value,
}
if parentSchema != nil {
parentSchema.XValidations = append(parentSchema.XValidations, newRule)
}
}
}
}
Expand Down

0 comments on commit c36aad1

Please sign in to comment.