func ValidateBlock()

in internal/langserver/handlers/validate/validate.go [47:111]


func ValidateBlock(src []byte, block *hclsyntax.Block) hcl.Diagnostics {
	if block == nil {
		return nil
	}
	schemaValidationAttr := parser.AttributeWithName(block, "schema_validation_enabled")
	if schemaValidationAttr != nil {
		if enabled := parser.ToLiteralBoolean(schemaValidationAttr.Expr); enabled != nil && !*enabled {
			return nil
		}
	}

	typeValue := parser.ExtractAzureResourceType(block)
	if typeValue == nil {
		return nil
	}

	bodyDef := tfschema.BodyDefinitionFromBlock(block)
	if bodyDef == nil {
		return nil
	}

	var attribute *hclsyntax.Attribute
	var hclNode *parser.HclNode
	if bodyAttribute := parser.AttributeWithName(block, "body"); bodyAttribute != nil {
		attribute = bodyAttribute
		hclNode = parser.JsonEncodeExpressionToHclNode(src, attribute.Expr)
		if hclNode == nil {
			tokens, _ := hclsyntax.LexExpression(src[attribute.Expr.Range().Start.Byte:attribute.Expr.Range().End.Byte], "", attribute.Expr.Range().Start)
			hclNode = parser.BuildHclNode(tokens)
		}
	}
	if attribute == nil || hclNode == nil {
		return nil
	}

	if dummy, ok := hclNode.Children["dummy"]; ok {
		dummy.KeyRange = attribute.NameRange
		if nameAttribute := parser.AttributeWithName(block, "name"); nameAttribute != nil {
			if dummy.Children == nil {
				dummy.Children = make(map[string]*parser.HclNode)
			}
			dummy.Children["name"] = &parser.HclNode{
				Value:      parser.ToLiteral(nameAttribute.Expr),
				Key:        "name",
				KeyRange:   nameAttribute.NameRange,
				ValueRange: nameAttribute.Expr.Range(),
			}
		}
		diags := Validate(dummy, bodyDef.AsTypeBase())
		// update resource doesn't need to check on required properties
		if block.Labels[0] == "azapi_update_resource" {
			res := hcl.Diagnostics{}
			for _, diag := range diags {
				// TODO: don't hardcode here
				if !strings.HasSuffix(diag.Summary, " is required, but no definition was found") {
					res = append(res, diag)
				}
			}
			return res
		} else {
			return diags
		}
	}
	return nil
}