|
| 1 | +/* |
| 2 | +Copyright © 2023-present, Meta Platforms, Inc. and affiliates |
| 3 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +of this software and associated documentation files (the "Software"), to deal |
| 5 | +in the Software without restriction, including without limitation the rights |
| 6 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +copies of the Software, and to permit persons to whom the Software is |
| 8 | +furnished to do so, subject to the following conditions: |
| 9 | +The above copyright notice and this permission notice shall be included in |
| 10 | +all copies or substantial portions of the Software. |
| 11 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 12 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 13 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 14 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 15 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 16 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 17 | +THE SOFTWARE. |
| 18 | +*/ |
| 19 | + |
| 20 | +package validation |
| 21 | + |
| 22 | +import ( |
| 23 | + "fmt" |
| 24 | + "regexp" |
| 25 | + "strings" |
| 26 | + |
| 27 | + "github.com/facebookincubator/ttpforge/pkg/args" |
| 28 | +) |
| 29 | + |
| 30 | +var ( |
| 31 | + namingPattern = regexp.MustCompile(`^[a-z][a-z0-9_]*$`) |
| 32 | +) |
| 33 | + |
| 34 | +// ValidateArgs validates argument definitions |
| 35 | +func ValidateArgs(ttpMap map[string]any, result *Result) { |
| 36 | + argsVal, ok := ttpMap["args"] |
| 37 | + if !ok { |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + argsList, isList := argsVal.([]any) |
| 42 | + if !isList { |
| 43 | + result.AddError("'args' must be a list") |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + seenArgs := make(map[string]bool) |
| 48 | + for i, arg := range argsList { |
| 49 | + argMap, isMap := arg.(map[string]any) |
| 50 | + if !isMap { |
| 51 | + result.AddError(fmt.Sprintf("Argument %d must be a dictionary", i+1)) |
| 52 | + continue |
| 53 | + } |
| 54 | + |
| 55 | + nameVal, hasName := argMap["name"] |
| 56 | + if !hasName { |
| 57 | + result.AddError(fmt.Sprintf("Argument %d missing 'name' field", i+1)) |
| 58 | + continue |
| 59 | + } |
| 60 | + |
| 61 | + argName := fmt.Sprintf("%v", nameVal) |
| 62 | + |
| 63 | + if seenArgs[argName] { |
| 64 | + result.AddError(fmt.Sprintf("Duplicate argument name: %s", argName)) |
| 65 | + } |
| 66 | + seenArgs[argName] = true |
| 67 | + |
| 68 | + if !namingPattern.MatchString(argName) { |
| 69 | + result.AddWarning(fmt.Sprintf("Argument name should be lowercase with underscores: %s", argName)) |
| 70 | + } |
| 71 | + |
| 72 | + if argType, ok := argMap["type"]; ok { |
| 73 | + typeStr := fmt.Sprintf("%v", argType) |
| 74 | + validTypes := args.GetValidArgTypes() |
| 75 | + validTypesMap := make(map[string]bool) |
| 76 | + for _, t := range validTypes { |
| 77 | + validTypesMap[t] = true |
| 78 | + } |
| 79 | + if !validTypesMap[typeStr] { |
| 80 | + result.AddError(fmt.Sprintf("Invalid argument type: %s (valid: %s)", typeStr, strings.Join(validTypes, ", "))) |
| 81 | + } |
| 82 | + } else { |
| 83 | + result.AddInfo(fmt.Sprintf("Argument '%s' has no type specified (defaults to string)", argName)) |
| 84 | + } |
| 85 | + |
| 86 | + if _, ok := argMap["description"]; !ok { |
| 87 | + result.AddWarning(fmt.Sprintf("Argument '%s' should have a description", argName)) |
| 88 | + } |
| 89 | + |
| 90 | + if _, ok := argMap["default"]; !ok { |
| 91 | + result.AddInfo(fmt.Sprintf("Argument '%s' has no default value", argName)) |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments