Skip to content

Commit

Permalink
fix: default cannot be used on computed fields (#1708)
Browse files Browse the repository at this point in the history
  • Loading branch information
davenewza committed Jan 30, 2025
1 parent d306165 commit 41afa3d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 2 additions & 0 deletions schema/testdata/errors/attribute_computed.keel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ model Item {
password Password @computed("password")
//expect-error:23:42:AttributeNotAllowedError:@computed cannot be used on field of type Secret
secret Secret @computed("secret")
//expect-error:28:39:AttributeExpressionError:@default cannot be used with computed fields
withDefault Number @default(1) @computed(1 + 1)
}
actions {
get getItem(id) {
Expand Down
2 changes: 1 addition & 1 deletion schema/testdata/errors/enum_default_no_expression.keel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
model Post {
fields {
//expect-error:23:31:AttributeArgumentError:default requires an expression
//expect-error:23:31:AttributeArgumentError:@default requires an expression
type PostType @default
}
}
Expand Down
15 changes: 14 additions & 1 deletion schema/validation/default_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,25 @@ func DefaultAttributeExpressionRules(asts []*parser.AST, errs *errorhandling.Val
return
}

for _, attr := range field.Attributes {
if attr.Name.Value == parser.AttributeComputed {
errs.AppendError(errorhandling.NewValidationErrorWithDetails(
errorhandling.AttributeExpressionError,
errorhandling.ErrorDetails{
Message: "@default cannot be used with computed fields",
Hint: "Either remove the @default attribute or remove the @computed attribute",
},
a,
))
}
}

typesWithZeroValue := []string{"Text", "Number", "Boolean", "ID", "Timestamp"}
if len(a.Arguments) == 0 && !lo.Contains(typesWithZeroValue, field.Type.Value) {
errs.AppendError(errorhandling.NewValidationErrorWithDetails(
errorhandling.AttributeArgumentError,
errorhandling.ErrorDetails{
Message: "default requires an expression",
Message: "@default requires an expression",
Hint: "Try @default(MyDefaultValue) instead",
},
a,
Expand Down

0 comments on commit 41afa3d

Please sign in to comment.