func()

in internal/azure/types/array_type.go [67:106]


func (t *ArrayType) Validate(body attr.Value, path string) []error {
	if t == nil || body == nil || body.IsNull() || body.IsUnknown() {
		return []error{}
	}
	errors := make([]error, 0)
	var itemType *TypeBase
	if t.ItemType != nil {
		itemType = t.ItemType.Type
	}
	// check body type
	var items []attr.Value
	switch v := body.(type) {
	case types.List:
		items = v.Elements()
	case types.Tuple:
		items = v.Elements()
	case types.Set:
		items = v.Elements()
	case types.Dynamic:
		return t.Validate(v.UnderlyingValue(), path)
	default:
		errors = append(errors, utils.ErrorMismatch(path, "array", fmt.Sprintf("%T", body)))
	}

	// check the length
	if t.MinLength != nil && len(items) < *t.MinLength {
		errors = append(errors, utils.ErrorCommon(path, fmt.Sprintf("array length is less than %d", *t.MinLength)))
	}

	if t.MaxLength != nil && len(items) > *t.MaxLength {
		errors = append(errors, utils.ErrorCommon(path, fmt.Sprintf("array length is greater than %d", *t.MaxLength)))
	}

	for index, value := range items {
		if itemType != nil {
			errors = append(errors, (*itemType).Validate(value, path+"."+strconv.Itoa(index))...)
		}
	}
	return errors
}