ec/ecresource/deploymentresource/deployment/v2/version_validator.go (30 lines of code) (raw):
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package v2
import (
"context"
"fmt"
"github.com/blang/semver"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)
var _ validator.String = isVersion{}
type isVersion struct{}
func (r isVersion) Description(ctx context.Context) string {
return "Validates that the specified version is a valid semver."
}
func (r isVersion) MarkdownDescription(ctx context.Context) string {
return "Validates that the specified version is a valid semver."
}
// ValidateString should perform the validation.
func (v isVersion) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) {
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
return
}
version := req.ConfigValue.ValueString()
_, err := semver.Parse(version)
if err == nil {
return
}
resp.Diagnostics.AddAttributeError(
req.Path,
fmt.Sprintf("[%s] is not a valid semver", req.Path),
fmt.Sprintf("The specified version [%s] is not a valid version string: %s", version, err.Error()),
)
}