Skip to content

Commit

Permalink
Update tsconfig.json validation messages (#2322)
Browse files Browse the repository at this point in the history
  • Loading branch information
infomiho authored Oct 28, 2024
1 parent 73e5532 commit c350cbe
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions waspc/src/Wasp/Generator/ExternalConfig/TsConfig.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Wasp.Generator.ExternalConfig.TsConfig
)
where

import Data.List (intercalate)
import qualified Wasp.ExternalConfig.TsConfig as T
import Wasp.Generator.ExternalConfig.Common (ErrorMsg)

Expand All @@ -21,7 +22,15 @@ instance IsJavascriptValue Bool where
showAsJsValue True = "true"
showAsJsValue False = "false"

type FieldName = String
-- | Represents a fully qualified field name in a JSON object.
-- For example, for the field "module" in the "compilerOptions" object,
-- the fully qualified field name would be "compilerOptions.module".
data FullyQualifiedFieldName = FieldName FieldPath

type FieldPath = [String]

instance Show FullyQualifiedFieldName where
show (FieldName fieldPath) = intercalate "." fieldPath

validateSrcTsConfig :: T.TsConfig -> [ErrorMsg]
validateSrcTsConfig tsConfig =
Expand All @@ -38,30 +47,31 @@ validateSrcTsConfig tsConfig =
validateRequiredFieldInCompilerOptions "outDir" ".wasp/phantom" T.outDir
]
where
validateRequiredFieldInCompilerOptions fieldName expectedValue getFieldValue = case getFieldValue compilerOptionsFields of
Just actualValue -> validateFieldValue ("compilerOptions." ++ fieldName) expectedValue actualValue
validateRequiredFieldInCompilerOptions fieldName expectedValue getFieldValue = case fieldValue of
Just actualValue -> validateFieldValue fullyQualifiedFieldName expectedValue actualValue
Nothing -> [missingFieldErrorMessage]
where
fieldValue = getFieldValue $ T.compilerOptions tsConfig
fullyQualifiedFieldName = FieldName ["compilerOptions", fieldName]

missingFieldErrorMessage =
unwords
[ "The",
show fieldName,
"field is missing in tsconfig.json. Expected value:",
"\"" ++ show fullyQualifiedFieldName ++ "\"",
"field is missing in tsconfig.json, expected value:",
showAsJsValue expectedValue ++ "."
]

compilerOptionsFields = T.compilerOptions tsConfig

validateFieldValue :: (Eq value, IsJavascriptValue value) => FieldName -> value -> value -> [String]
validateFieldValue fieldName expectedValue actualValue =
validateFieldValue :: (Eq value, IsJavascriptValue value) => FullyQualifiedFieldName -> value -> value -> [String]
validateFieldValue fullyQualifiedFieldName expectedValue actualValue =
if actualValue == expectedValue
then []
else [invalidValueErrorMessage]
where
invalidValueErrorMessage =
unwords
[ "Invalid value for the",
show fieldName,
"field in tsconfig.json file, expected value:",
"\"" ++ show fullyQualifiedFieldName ++ "\"",
"field in tsconfig.json, expected value:",
showAsJsValue expectedValue ++ "."
]

0 comments on commit c350cbe

Please sign in to comment.