Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test for multiple validation markers, and a test for using quotes (e.g. self == "foobar")?

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