Skip to content

Commit

Permalink
Support type aliasing to basic types
Browse files Browse the repository at this point in the history
Previously, validation schema generation was skipped on a type alias:

```
type A = B
```

Whereas, a new type was fine:

```
type A B
```

This commit implements support for generating validation schemas for
type aliases to basic types, i.e. string, int, etc.

Signed-off-by: Chris Tarazi <tarazichris@gmail.com>
  • Loading branch information
christarazi committed Aug 14, 2020
1 parent 7bb5dee commit d944deb
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pkg/crd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,21 @@ func localNamedToSchema(ctx *schemaContext, ident *ast.Ident) *apiext.JSONSchema
if err != nil {
ctx.pkg.AddError(loader.ErrFromNode(err, ident))
}

// Check for type aliasing. If the type is not an alias, then handle it
// as a basic type.
if basicInfo.Name() == ident.Name {
return &apiext.JSONSchemaProps{
Type: typ,
Format: fmt,
}
}

// Type alias to a basic type.
ctx.requestSchema("", ident.Name)
link := TypeRefLink("", ident.Name)
return &apiext.JSONSchemaProps{
Type: typ,
Format: fmt,
Ref: &link,
}
}
// NB(directxman12): if there are dot imports, this might be an external reference,
Expand Down

0 comments on commit d944deb

Please sign in to comment.