func()

in internal/azure/types/object_type.go [103:161]


func (t *ObjectType) Validate(body attr.Value, path string) []error {
	if t == nil || body == nil || body.IsNull() || body.IsUnknown() {
		return []error{}
	}

	errors := make([]error, 0)
	// check body type
	var bodyMap map[string]attr.Value
	switch v := body.(type) {
	case types.Object:
		bodyMap = v.Attributes()
	case types.Map:
		bodyMap = v.Elements()
	case types.Dynamic:
		return t.Validate(v.UnderlyingValue(), path)
	default:
		errors = append(errors, utils.ErrorMismatch(path, "object", fmt.Sprintf("%T", body)))
		return errors
	}
	// check properties defined in body, but not in schema
	for key, value := range bodyMap {
		if def, ok := t.Properties[key]; ok {
			if def.IsReadOnly() {
				errors = append(errors, utils.ErrorShouldNotDefineReadOnly(path+"."+key))
				continue
			}
			var valueDefType *TypeBase
			if def.Type != nil && def.Type.Type != nil {
				valueDefType = def.Type.Type
				errors = append(errors, (*valueDefType).Validate(value, path+"."+key)...)
			}
			continue
		}
		if t.AdditionalProperties != nil && t.AdditionalProperties.Type != nil {
			errors = append(errors, (*t.AdditionalProperties.Type).Validate(value, path+"."+key)...)
		} else {
			options := make([]string, 0)
			for key := range t.Properties {
				options = append(options, path+"."+key)
			}
			errors = append(errors, utils.ErrorShouldNotDefine(path+"."+key, options))
		}
	}

	// check properties required in schema, but not in body
	for key, value := range t.Properties {
		if !value.IsRequired() {
			continue
		}
		if _, ok := bodyMap[key]; !ok {
			// skip name in body
			if path == "" && key == "name" {
				continue
			}
			errors = append(errors, utils.ErrorShouldDefine(path+"."+key))
		}
	}
	return errors
}