|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2025 Orange |
| 3 | + * SPDX-License-Identifier: Mozilla Public License 2.0 |
| 4 | + * |
| 5 | + * This software is distributed under the MPL-2.0 license. |
| 6 | + * the text of which is available at https://www.mozilla.org/en-US/MPL/2.0/ |
| 7 | + * or see the "LICENSE" file for more details. |
| 8 | + */ |
| 9 | + |
| 10 | +package stringvalidator |
| 11 | + |
| 12 | +import ( |
| 13 | + "context" |
| 14 | + "fmt" |
| 15 | + |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 17 | + |
| 18 | + casesTypes "github.com/orange-cloudavenue/terraform-plugin-framework-validators/stringvalidator/cases" |
| 19 | +) |
| 20 | + |
| 21 | +var _ validator.String = casesValidator{} |
| 22 | + |
| 23 | +const ( |
| 24 | + CasesDisallowUpper CasesValidatorType = "disallow_upper" |
| 25 | + CasesDisallowNumber CasesValidatorType = "disallow_number" |
| 26 | + CasesDisallowSpace CasesValidatorType = "disallow_space" |
| 27 | + CasesDisallowLower CasesValidatorType = "disallow_lower" |
| 28 | +) |
| 29 | + |
| 30 | +var casesTypesFunc = map[CasesValidatorType]func() validator.String{ |
| 31 | + CasesDisallowUpper: casesTypes.DisallowUpper, |
| 32 | + CasesDisallowNumber: casesTypes.DisallowNumber, |
| 33 | + CasesDisallowSpace: casesTypes.DisallowSpace, |
| 34 | + CasesDisallowLower: casesTypes.DisallowLower, |
| 35 | +} |
| 36 | + |
| 37 | +type CasesValidatorType string |
| 38 | + |
| 39 | +type casesValidator struct { |
| 40 | + CasesTypes []CasesValidatorType |
| 41 | +} |
| 42 | + |
| 43 | +// Description describes the validation in plain text formatting. |
| 44 | +func (validatorCase casesValidator) Description(_ context.Context) string { |
| 45 | + description := "" |
| 46 | + |
| 47 | + if len(validatorCase.CasesTypes) == 0 { |
| 48 | + description += "invalid configuration" |
| 49 | + } |
| 50 | + |
| 51 | + switch { |
| 52 | + case len(validatorCase.CasesTypes) > 1: |
| 53 | + description += "The value must respect the following rules : " |
| 54 | + case len(validatorCase.CasesTypes) == 1: |
| 55 | + description += "The value must respect the following rule : " |
| 56 | + } |
| 57 | + |
| 58 | + for i, caseType := range validatorCase.CasesTypes { |
| 59 | + if i == len(validatorCase.CasesTypes)-1 { |
| 60 | + description += casesTypesFunc[caseType]().Description(context.Background()) |
| 61 | + } else { |
| 62 | + description += fmt.Sprintf("%s, ", casesTypesFunc[caseType]().Description(context.Background())) |
| 63 | + } |
| 64 | + } |
| 65 | + return description |
| 66 | +} |
| 67 | + |
| 68 | +// MarkdownDescription describes the validation in Markdown formatting. |
| 69 | +func (validatorCase casesValidator) MarkdownDescription(ctx context.Context) string { |
| 70 | + return validatorCase.Description(ctx) |
| 71 | +} |
| 72 | + |
| 73 | +// Validate performs the validation. |
| 74 | +func (validatorCase casesValidator) ValidateString( |
| 75 | + ctx context.Context, |
| 76 | + request validator.StringRequest, |
| 77 | + response *validator.StringResponse, |
| 78 | +) { |
| 79 | + if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() { |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + if len(validatorCase.CasesTypes) == 0 { |
| 84 | + response.Diagnostics.AddError( |
| 85 | + fmt.Sprintf("Invalid configuration for attribute %s", request.Path), |
| 86 | + "Set at least one case type", |
| 87 | + ) |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + for _, caseType := range validatorCase.CasesTypes { |
| 92 | + if _, ok := casesTypesFunc[caseType]; !ok { |
| 93 | + response.Diagnostics.AddError( |
| 94 | + "Invalid case type", |
| 95 | + fmt.Sprintf("invalid case type: %s", caseType), |
| 96 | + ) |
| 97 | + continue |
| 98 | + } |
| 99 | + |
| 100 | + resp := new(validator.StringResponse) |
| 101 | + casesTypesFunc[caseType]().ValidateString(ctx, request, resp) |
| 102 | + |
| 103 | + if resp.Diagnostics.HasError() { |
| 104 | + response.Diagnostics.Append(resp.Diagnostics...) |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// Cases returns a new string validator that checks if the string matches any of the specified case types. |
| 110 | +// |
| 111 | +// Parameters: |
| 112 | +// - casesTypes: A slice of CasesValidatorType that specifies the types of cases to validate against. |
| 113 | +// |
| 114 | +// Returns: |
| 115 | +// - validator.String: A string validator that validates the string against the specified case types. |
| 116 | +func Cases(casesTypes []CasesValidatorType) validator.String { |
| 117 | + return &casesValidator{ |
| 118 | + CasesTypes: casesTypes, |
| 119 | + } |
| 120 | +} |
0 commit comments