diff --git a/README.md b/README.md index 07637de..2e0f3c3 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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) diff --git a/pkg/schema/schema.go b/pkg/schema/schema.go index abaeddc..a8fafe5 100644 --- a/pkg/schema/schema.go +++ b/pkg/schema/schema.go @@ -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 { @@ -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") @@ -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) } diff --git a/pkg/schema/schema_test.go b/pkg/schema/schema_test.go index 2b9dacc..5a28712 100644 --- a/pkg/schema/schema_test.go +++ b/pkg/schema/schema_test.go @@ -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, },