Skip to content

Commit

Permalink
chore: normalise refs before validating parent nodes
Browse files Browse the repository at this point in the history
This allows parent nodes to not have to handle module == “” separately.
  • Loading branch information
matt2e committed Dec 18, 2024
1 parent e471620 commit 53c8a0b
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions common/schema/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ func ValidateModuleInSchema(schema *Schema, m optional.Option[*Module]) (*Schema
}
indent++
defer func() { indent-- }()

// Validate all children before validating the current node.
if err := next(); err != nil {
return err
}

switch n := n.(type) {
case *Ref:
mdecl := scopes.Resolve(*n)
Expand Down Expand Up @@ -222,7 +228,7 @@ func ValidateModuleInSchema(schema *Schema, m optional.Option[*Module]) (*Schema
*EnumVariant, *Config, *Secret, *Topic, *DatabaseRuntime, *DatabaseRuntimeConnections,
*Data, *Field:
}
return next()
return nil
})
if err != nil {
merr = append(merr, err)
Expand Down Expand Up @@ -267,6 +273,12 @@ func ValidateModule(module *Module) error {
scopes = scopes.PushScope(scoped.Scope())
defer func() { scopes = pop }()
}

// Validate all children before validating the current node.
if err := next(); err != nil {
return err
}

if n, ok := n.(Decl); ok {
tname := typeName(n)
duplKey := tname + ":" + n.GetName()
Expand Down Expand Up @@ -351,17 +363,12 @@ func ValidateModule(module *Module) error {
}
}

case *MetadataRetry:
if n.Catch != nil && n.Catch.Module == "" {
n.Catch.Module = module.Name
}

case Type, Metadata, Value, IngressPathComponent, DatabaseConnector,
*Module, *Schema, *Optional, *TypeParameter, *EnumVariant,
*Config, *Secret, *DatabaseRuntime, *DatabaseRuntimeConnections,
*Database, *Enum:
}
return next()
return nil
})

merr = cleanErrors(merr)
Expand Down Expand Up @@ -908,9 +915,6 @@ func validateQueryParamsPayloadType(n Node, r Type, v *Verb, reqOrResp string) e

func validateVerbSubscriptions(module *Module, v *Verb, md *MetadataSubscriber, scopes Scopes) (merr []error) {
merr = []error{}
if md.Topic.Module == "" {
md.Topic.Module = module.Name
}
topicDecl := scopes.Resolve(*md.Topic)
if topicDecl == nil {
// errors for invalid refs are handled elsewhere
Expand Down Expand Up @@ -959,9 +963,6 @@ func validateRetries(module *Module, retry *MetadataRetry, requestType optional.
merr = append(merr, errorf(retry, "catch can only be defined on verbs"))
return
}
if retry.Catch.Module == "" {
retry.Catch.Module = module.Name
}
catchDecl := scopes.Resolve(*retry.Catch)
if catchDecl == nil {
if retry.Catch.Module != "" && retry.Catch.Module != module.Name && !schema.Ok() {
Expand Down

0 comments on commit 53c8a0b

Please sign in to comment.