func CheckStringOption()

in lib/check/check.go [42:96]


func CheckStringOption(opt, optName string, descriptors map[string]*cpb.Descriptor) error {
	desc, ok := descriptors[optName]
	if !ok {
		return fmt.Errorf("internal error: option descriptor %q not defined", optName)
	}
	if len(opt) == 0 {
		return nil
	}
	if len(desc.Regexp) > 0 {
		re, err := regexp.Compile(desc.Regexp)
		if err != nil {
			return fmt.Errorf("internal error: option descriptor %q regexp %q does not compile", optName, desc.Regexp)
		}
		if !re.Match([]byte(opt)) {
			return fmt.Errorf("option %q: value %q does not match regular expression %q", optName, opt, desc.Regexp)
		}
	}
	if len(desc.EnumValues) > 0 {
		found := false
		for _, v := range desc.EnumValues {
			if v == opt {
				found = true
				break
			}
		}
		if !found {
			return fmt.Errorf("option %q: value %q is not a valid, must be one of: %v", optName, opt, desc.EnumValues)
		}
	}
	if len(desc.Min) > 0 || len(desc.Max) > 0 {
		if desc.Type == "string:duration" {
			val, min, max, err := OptDuration(optName, opt, desc.Min, desc.Max)
			if err != nil {
				return err
			}
			if (min.IsPresent() && val < min.Get()) || (max.IsPresent() && val > max.Get()) {
				return fmt.Errorf("option %q: value %q is not within range (duration range is %s to %s)", optName, opt, desc.Min, desc.Max)
			}
		} else {
			min, err := optional.NewIntFromString(desc.Min)
			if err != nil {
				return err
			}
			max, err := optional.NewIntFromString(desc.Max)
			if err != nil {
				return err
			}

			if (min.IsPresent() && len(opt) < min.Get()) || (max.IsPresent() && len(opt) > max.Get()) {
				return fmt.Errorf("option %q: value %q is too short or too long (range is %s to %s)", optName, opt, desc.Min, desc.Max)
			}
		}
	}
	return nil
}