Skip to content

Commit

Permalink
Merge pull request #71 from dadav/feature_min_max_length
Browse files Browse the repository at this point in the history
feat: Add MinLength and MaxLength
  • Loading branch information
dadav authored Sep 18, 2024
2 parents 7da61f8 + fae8c59 commit f033847
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ foo: bar
| [`not`](#not) | A schema that must not be matched. | Takes an `object` |
| [`if/then/else`](#ifthenelse) | `if` the given schema applies, `then` also apply the given schema or `else` the other schema| Takes an `object` |
| `$ref` | Accepts a URL to a valid `jsonschema`. Extend the schema for the current key | Takes an URL |
| [`minLength`](#minlength) | Minimum string length. | Takes an `integer`. Must be smaller or equal than `maxLength` (if used) |
| [`maxLength`](#maxlength) | Maximum string length. | Takes an `integer`. Must be greater or equal than `minLength` (if used) |

## Validation & completion

Expand Down Expand Up @@ -686,6 +688,28 @@ Conditional schema settings with `if`/`then`/`else`
unknown: foo
```

#### `minLength`

The value must be an integer greater or equal to zero and defines the minimum length of a string value.

```yaml
# @schema
# minLength: 1
# @schema
namespace: foo
```

#### `maxLength`

The value must be an integer greater than zero and defines the maximum length of a string value.

```yaml
# @schema
# maxLength: 3
# @schema
namespace: foo
```

## License

[MIT](https://github.com/dadav/helm-schema/blob/main/LICENSE)
11 changes: 9 additions & 2 deletions pkg/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ type Schema struct {
WriteOnly bool `yaml:"writeOnly,omitempty" json:"writeOnly,omitempty"`
Required BoolOrArrayOfString `yaml:"required,omitempty" json:"required,omitempty"`
CustomAnnotations map[string]interface{} `yaml:"-" json:",omitempty"`
MinLength *int `yaml:"minLength,omitempty" json:"minLength,omitempty"`
MaxLength *int `yaml:"maxLength,omitempty" json:"maxLength,omitempty"`
}

func NewSchema(schemaType string) *Schema {
Expand Down Expand Up @@ -355,11 +357,16 @@ func (s Schema) Validate() error {
return fmt.Errorf("cant use pattern if type is %s. Use type=string", s.Type)
}

// // Check if type=string if format!=""
// Check if type=string if format!=""
if s.Format != "" && !s.Type.IsEmpty() && !s.Type.Matches("string") {
return fmt.Errorf("cant use format if type is %s. Use type=string", s.Type)
}

// Check if type=string if maxLength or minLength is used
if s.MaxLength != nil && s.MinLength != nil && *s.MinLength > *s.MaxLength {
return errors.New("cant use MinLength > MaxLength")
}

// Cant use Format and Pattern together
if s.Format != "" && s.Pattern != "" {
return errors.New("cant use format and pattern option at the same time")
Expand All @@ -372,7 +379,7 @@ func (s Schema) Validate() error {
}
}

// // If type and items are used, type must be array
// If type and items are used, type must be array
if s.Items != nil && !s.Type.IsEmpty() && !s.Type.Matches("array") {
return fmt.Errorf("cant use items if type is %s. Use type=array", s.Type)
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,22 @@ func TestValidate(t *testing.T) {
comment: `
# @schema
# $ref: https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/v1.29.2/affinity-v1.json
# @schema`,
expectedValid: true,
},
{
comment: `
# @schema
# minLength: 1
# maxLength: 0
# @schema`,
expectedValid: false,
},
{
comment: `
# @schema
# minLength: 1
# maxLength: 2
# @schema`,
expectedValid: true,
},
Expand Down

0 comments on commit f033847

Please sign in to comment.