Skip to content

Commit

Permalink
Add documentation and another test case
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmesa-jt committed Jun 13, 2024
1 parent c08b884 commit 4106fe3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
34 changes: 30 additions & 4 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The following annotations are supported:
* [Type](#type)
* [Multiple types](#multiple-types)
* [Enum](#enum)
* [ItemEnum](#itemEnum)
* [Strings](#strings)
* [maxLength](#maxlength)
* [minLength](#minlength)
Expand Down Expand Up @@ -120,7 +121,7 @@ Always returns array of strings. Special case is `null` where instead of string,
```yaml
service: ClusterIP # @schema enum:[ClusterIP, LoadBalancer, null]
```
```json
"service": {
"enum": [
Expand All @@ -132,6 +133,31 @@ service: ClusterIP # @schema enum:[ClusterIP, LoadBalancer, null]
}
```

### ItemEnum

This is a special key that apply [enum](#enum) on items of an array.

```yaml
port: [80, 443] # @schema itemEnum:[80, 8080, 443, 8443]
```
```json
"port": {
"items": {
"type": "number",
"enum": [
"80",
"443",
"8080",
"8443"
]
},
"type": [
"array"
]
}
```

## Strings

### maxLength
Expand Down Expand Up @@ -193,7 +219,7 @@ Number greater than `0`. [section 6.2.1](https://json-schema.org/draft/2020-12/j
```yaml
replicas: 2 # @schema multipleOf:2
```
```json
"replicas": {
"multipleOf": 2,
Expand All @@ -208,7 +234,7 @@ Number. [section 6.2.2](https://json-schema.org/draft/2020-12/json-schema-valida
```yaml
replicas: 2 # @schema maximum:10
```
```json
"replicas": {
"maximum": 10,
Expand All @@ -223,7 +249,7 @@ Number. [section 6.2.4](https://json-schema.org/draft/2020-12/json-schema-valida
```yaml
replicas: 5 # @schema minimum:2
```
```json
"replicas": {
"minimum": 2,
Expand Down
13 changes: 10 additions & 3 deletions pkg/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,17 @@ func TestProcessComment(t *testing.T) {
expectedRequired: false,
},
{
name: "Set array item enum",
name: "Set array only item enum",
schema: &Schema{},
comment: "# @schema minItems:1;maxItems:10;uniqueItems:true;item:boolean;itemEnum:[\"one\",\"two\"]",
expectedSchema: &Schema{MinItems: uint64Ptr(1), MaxItems: uint64Ptr(10), UniqueItems: true, Items: &Schema{Type: "boolean", Enum: []any{"one", "two"}}},
comment: "# @schema itemEnum:[1,2]",
expectedSchema: &Schema{Items: &Schema{Enum: []any{"1", "2"}}},
expectedRequired: false,
},
{
name: "Set array item type and enum",
schema: &Schema{},
comment: "# @schema minItems:1;maxItems:10;uniqueItems:true;item:string;itemEnum:[\"one\",\"two\"]",
expectedSchema: &Schema{MinItems: uint64Ptr(1), MaxItems: uint64Ptr(10), UniqueItems: true, Items: &Schema{Type: "string", Enum: []any{"one", "two"}}},
expectedRequired: false,
},
{
Expand Down

0 comments on commit 4106fe3

Please sign in to comment.