Skip to content

Commit

Permalink
Validation for @default attribute on userEntity
Browse files Browse the repository at this point in the history
  • Loading branch information
infomiho committed Sep 27, 2024
1 parent 3b69284 commit 614b96f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
13 changes: 4 additions & 9 deletions waspc/src/Wasp/AppSpec/Entity.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ module Wasp.AppSpec.Entity
getPslModelBody,
getIdField,
getIdBlockAttribute,
isFieldUnique,
-- only for testing:
doesFieldHaveAttribute,
)
where
Expand Down Expand Up @@ -54,18 +52,15 @@ getPslModelBody = pslModelBody
getIdField :: Entity -> Maybe Psl.Model.Field
getIdField = findIdField . getPslModelBody

isFieldUnique :: String -> Entity -> Maybe Bool
isFieldUnique fieldName = doesFieldHaveAttribute fieldName "unique"

doesFieldHaveAttribute :: String -> String -> Entity -> Maybe Bool
doesFieldHaveAttribute fieldName attrName entity =
doesFieldHaveAttribute :: Entity -> String -> String -> Maybe Bool
doesFieldHaveAttribute entity attrName fieldName =
doesPslFieldHaveAttribute attrName <$> findPslFieldByName fieldName entity

findPslFieldByName :: String -> Entity -> Maybe Psl.Model.Field
findPslFieldByName fieldName Entity {pslModelBody = Psl.Model.Body elements} =
find isField [field | (Psl.Model.ElementField field) <- elements]
find isTargetField [field | (Psl.Model.ElementField field) <- elements]
where
isField Psl.Model.Field {_name = name} = name == fieldName
isTargetField Psl.Model.Field {_name = name} = name == fieldName

getIdBlockAttribute :: Entity -> Maybe Psl.Attribute.Attribute
getIdBlockAttribute = findIdBlockAttribute . getPslModelBody
15 changes: 12 additions & 3 deletions waspc/src/Wasp/AppSpec/Valid.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import qualified Wasp.AppSpec.App.Wasp as Wasp
import Wasp.AppSpec.Core.Decl (takeDecls)
import Wasp.AppSpec.Core.IsDecl (IsDecl)
import qualified Wasp.AppSpec.Crud as AS.Crud
import Wasp.AppSpec.Entity (doesFieldHaveAttribute)
import qualified Wasp.AppSpec.Entity as Entity
import qualified Wasp.AppSpec.Entity.Field as Entity.Field
import qualified Wasp.AppSpec.Operation as AS.Operation
Expand Down Expand Up @@ -128,12 +129,20 @@ validateUserEntity spec =
case App.auth (snd $ getApp spec) of
Nothing -> []
Just auth ->
[ GenericValidationError $ "Entity '" ++ userEntityName ++ "' (referenced by app.auth.userEntity) must have an ID field (specified with the '@id' attribute)"
| isNothing idFieldType
]
concat
[ [ GenericValidationError $ "Entity '" ++ userEntityName ++ "' (referenced by app.auth.userEntity) must have an ID field (specified with the '@id' attribute)"
| isNothing idFieldType
],
[ GenericValidationError $ "Entity '" ++ userEntityName ++ "' (referenced by app.auth.userEntity) must have an ID field (specified with the '@id' attribute) with a default value"
| doesIdFieldHaveDefaultAttribute /= Just True
]
]
where
idFieldType = Entity.getIdField userEntity

doesIdFieldHaveDefaultAttribute =
idFieldType >>= doesFieldHaveAttribute userEntity "default" . Psl.Model._name

(userEntityName, userEntity) = AS.resolveRef spec (Auth.userEntity auth)

validateAppAuthIsSetIfAnyPageRequiresAuth :: AppSpec -> [ValidationError]
Expand Down
14 changes: 3 additions & 11 deletions waspc/test/AppSpec/EntityTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,13 @@ spec_AppSpecEntityTest = do
it "Returns Nothing if primary field doesn't exist" $ do
getIdField entityWithoutIdField `shouldBe` Nothing

describe "isFieldUnique" $ do
it "Returns Nothing if the field doesn't exist on the entity" $ do
Entity.isFieldUnique "nonExistingField" entityWithoutIdField `shouldBe` Nothing
it "Returns Just False if the field exists on the entity but isn't unique" $ do
Entity.isFieldUnique "description" entityWithIdField `shouldBe` Just False
it "Returns Just True if the field exists and is unique" $ do
Entity.isFieldUnique "id" entityWithIdField `shouldBe` Just True

describe "doesFieldHaveAttribute" $ do
it "Returns Nothing if the field doesn't exist on the entity" $ do
Entity.doesFieldHaveAttribute "nonExistingField" "unique" entityWithoutIdField `shouldBe` Nothing
Entity.doesFieldHaveAttribute entityWithoutIdField "unique" "nonExistingField" `shouldBe` Nothing
it "Returns Just False if the field exists on the entity but doesn't have the required attribute" $ do
Entity.doesFieldHaveAttribute "description" "id" entityWithIdField `shouldBe` Just False
Entity.doesFieldHaveAttribute entityWithIdField "id" "description" `shouldBe` Just False
it "Returns Just True if the field exists on the entity and has the required attribute" $ do
Entity.doesFieldHaveAttribute "id" "id" entityWithIdField `shouldBe` Just True
Entity.doesFieldHaveAttribute entityWithIdField "id" "id" `shouldBe` Just True
where
entityWithIdField =
Entity.makeEntity $
Expand Down

0 comments on commit 614b96f

Please sign in to comment.