| 1 |
package valuevalidation |
| 2 |
|
| 3 |
import ( |
| 4 |
"k8s.io/kube-openapi/pkg/common" |
| 5 |
"k8s.io/kube-openapi/pkg/validation/spec" |
| 6 |
) |
| 7 |
|
| 8 |
// Dummy type to test the openapi-gen API rule checker. |
| 9 |
// The API rule violations are in format of: |
| 10 |
// -> +k8s:validation:[validation rule]=[value] |
| 11 |
|
| 12 |
// +k8s:validation:maxProperties=5 |
| 13 |
// +k8s:validation:minProperties=1 |
| 14 |
// +k8s:openapi-gen=true |
| 15 |
// +k8s:validation:cel[0]:rule="self == oldSelf" |
| 16 |
// +k8s:validation:cel[0]:message="foo" |
| 17 |
type Foo struct { |
| 18 |
// +k8s:validation:maxLength=5 |
| 19 |
// +k8s:validation:minLength=1 |
| 20 |
// +k8s:validation:pattern="^a.*b$" |
| 21 |
// +optional |
| 22 |
StringValue string `json:"StringValue,omitempty"` |
| 23 |
|
| 24 |
// +k8s:validation:maximum=5.0 |
| 25 |
// +k8s:validation:minimum=1.0 |
| 26 |
// +k8s:validation:exclusiveMinimum=true |
| 27 |
// +k8s:validation:exclusiveMaximum=true |
| 28 |
// +k8s:validation:multipleOf=2.0 |
| 29 |
// +optional |
| 30 |
NumberValue float64 `json:"NumberValue,omitempty"` |
| 31 |
|
| 32 |
// +k8s:validation:maxItems=5 |
| 33 |
// +k8s:validation:minItems=1 |
| 34 |
// +k8s:validation:uniqueItems=true |
| 35 |
ArrayValue []string |
| 36 |
|
| 37 |
// +k8s:validation:minProperties=1 |
| 38 |
// +k8s:validation:maxProperties=5 |
| 39 |
MapValue map[string]string |
| 40 |
|
| 41 |
// +k8s:validation:cel[0]:rule="self.length() > 0" |
| 42 |
// +k8s:validation:cel[0]:message="string message" |
| 43 |
// +k8s:validation:cel[1]:rule="self.length() % 2 == 0" |
| 44 |
// +k8s:validation:cel[1]:messageExpression="self + ' hello'" |
| 45 |
// +optional |
| 46 |
CELField string `json:"celField"` |
| 47 |
} |
| 48 |
|
| 49 |
// This one has an open API v3 definition |
| 50 |
// +k8s:validation:maxProperties=5 |
| 51 |
// +k8s:openapi-gen=true |
| 52 |
// +k8s:validation:cel[0]:rule="self == oldSelf" |
| 53 |
// +k8s:validation:cel[0]:message="foo2" |
| 54 |
type Foo2 struct{} |
| 55 |
|
| 56 |
func (Foo2) OpenAPIV3Definition() common.OpenAPIDefinition { |
| 57 |
return common.OpenAPIDefinition{ |
| 58 |
Schema: spec.Schema{ |
| 59 |
SchemaProps: spec.SchemaProps{ |
| 60 |
Type: []string{"object"}, |
| 61 |
}, |
| 62 |
}, |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func (Foo2) OpenAPISchemaType() []string { |
| 67 |
return []string{"test-type"} |
| 68 |
} |
| 69 |
|
| 70 |
func (Foo2) OpenAPISchemaFormat() string { |
| 71 |
return "test-format" |
| 72 |
} |
| 73 |
|
| 74 |
// This one has a OneOf |
| 75 |
// +k8s:openapi-gen=true |
| 76 |
// +k8s:validation:maxProperties=5 |
| 77 |
// +k8s:openapi-gen=true |
| 78 |
// +k8s:validation:cel[0]:rule="self == oldSelf" |
| 79 |
// +k8s:validation:cel[0]:message="foo3" |
| 80 |
type Foo3 struct{} |
| 81 |
|
| 82 |
func (Foo3) OpenAPIV3OneOfTypes() []string { |
| 83 |
return []string{"number", "string"} |
| 84 |
} |
| 85 |
func (Foo3) OpenAPISchemaType() []string { |
| 86 |
return []string{"string"} |
| 87 |
} |
| 88 |
func (Foo3) OpenAPISchemaFormat() string { |
| 89 |
return "string" |
| 90 |
} |
| 91 |
|
| 92 |
// this one should ignore marker comments |
| 93 |
// +k8s:openapi-gen=true |
| 94 |
// +k8s:validation:maximum=6 |
| 95 |
// +k8s:validation:cel[0]:rule="self == oldSelf" |
| 96 |
// +k8s:validation:cel[0]:message="foo4" |
| 97 |
type Foo4 struct{} |
| 98 |
|
| 99 |
func (Foo4) OpenAPIDefinition() common.OpenAPIDefinition { |
| 100 |
return common.OpenAPIDefinition{ |
| 101 |
Schema: spec.Schema{ |
| 102 |
SchemaProps: spec.SchemaProps{ |
| 103 |
Type: []string{"integer"}, |
| 104 |
}, |
| 105 |
}, |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
// +k8s:openapi-gen=true |
| 110 |
// +k8s:validation:maxProperties=5 |
| 111 |
// +k8s:validation:minProperties=1 |
| 112 |
// +k8s:validation:cel[0]:rule="self == oldSelf" |
| 113 |
// +k8s:validation:cel[0]:message="foo5" |
| 114 |
type Foo5 struct{} |
| 115 |
|
| 116 |
func (Foo5) OpenAPIV3Definition() common.OpenAPIDefinition { |
| 117 |
return common.OpenAPIDefinition{ |
| 118 |
Schema: spec.Schema{ |
| 119 |
SchemaProps: spec.SchemaProps{ |
| 120 |
Type: []string{"object"}, |
| 121 |
}, |
| 122 |
}, |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
func (Foo5) OpenAPISchemaType() []string { |
| 127 |
return []string{"test-type"} |
| 128 |
} |
| 129 |
|
| 130 |
func (Foo5) OpenAPISchemaFormat() string { |
| 131 |
return "test-format" |
| 132 |
} |