-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validation for @default attribute on userEntity #2311
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,21 +7,17 @@ module Wasp.AppSpec.Entity | |
getPslModelBody, | ||
getIdField, | ||
getIdBlockAttribute, | ||
isFieldUnique, | ||
-- only for testing: | ||
doesFieldHaveAttribute, | ||
) | ||
where | ||
|
||
import Data.Aeson (FromJSON (parseJSON)) | ||
import Data.Data (Data) | ||
import Data.List (find) | ||
import Wasp.AppSpec.Core.IsDecl (IsDecl) | ||
import Wasp.AppSpec.Entity.Field (Field) | ||
import qualified Wasp.AppSpec.Entity.Field as Field | ||
import qualified Wasp.Psl.Ast.Attribute as Psl.Attribute | ||
import qualified Wasp.Psl.Ast.Model as Psl.Model | ||
import Wasp.Psl.Util (doesPslFieldHaveAttribute, findIdBlockAttribute, findIdField) | ||
import Wasp.Psl.Util (findIdBlockAttribute, findIdField) | ||
|
||
data Entity = Entity | ||
{ fields :: ![Field], | ||
|
@@ -54,18 +50,5 @@ 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a weirdly specific function which I tried to rewrite but in the end it was easier just to use the lower level helper |
||
doesFieldHaveAttribute fieldName attrName entity = | ||
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] | ||
where | ||
isField Psl.Model.Field {_name = name} = name == fieldName | ||
|
||
getIdBlockAttribute :: Entity -> Maybe Psl.Attribute.Attribute | ||
getIdBlockAttribute = findIdBlockAttribute . getPslModelBody |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ import Wasp.Generator.Crud (crudDeclarationToOperationsList) | |
import Wasp.Node.Version (oldestWaspSupportedNodeVersion) | ||
import qualified Wasp.Node.Version as V | ||
import qualified Wasp.Psl.Ast.Model as Psl.Model | ||
import qualified Wasp.Psl.Util as Psl.Util | ||
import Wasp.Psl.Valid (getValidDbSystemFromPrismaSchema) | ||
import qualified Wasp.SemanticVersion as SV | ||
import qualified Wasp.SemanticVersion.VersionBound as SVB | ||
|
@@ -128,14 +129,18 @@ 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 | ||
] | ||
case Entity.getIdField userEntity of | ||
Nothing -> [userEntityMissingIdFieldError] | ||
Just idField -> | ||
if Psl.Util.doesPslFieldHaveAttribute "default" idField | ||
then [] | ||
else [userEntityIdFieldMissingDefaultAttrError] | ||
Comment on lines
+132
to
+137
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! Might be tempting to use Either here as a monad? Btw there is even this thing: https://hackage.haskell.org/package/validation . |
||
where | ||
idFieldType = Entity.getIdField userEntity | ||
|
||
(userEntityName, userEntity) = AS.resolveRef spec (Auth.userEntity auth) | ||
|
||
userEntityMissingIdFieldError = GenericValidationError $ "Entity '" ++ userEntityName ++ "' (referenced by app.auth.userEntity) must have an ID field (specified with the '@id' attribute)" | ||
userEntityIdFieldMissingDefaultAttrError = GenericValidationError $ "Entity '" ++ userEntityName ++ "' (referenced by app.auth.userEntity) must have an ID field (specified with the '@id' attribute) with a default value" | ||
|
||
validateAppAuthIsSetIfAnyPageRequiresAuth :: AppSpec -> [ValidationError] | ||
validateAppAuthIsSetIfAnyPageRequiresAuth spec = | ||
[ GenericValidationError | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was unused.