Skip to content

Commit

Permalink
[#114] Fix EntryPath Schema
Browse files Browse the repository at this point in the history
  • Loading branch information
sancho20021 committed Jul 25, 2022
1 parent fafd1eb commit 93c62d6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
20 changes: 2 additions & 18 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,8 @@
"components": {
"schemas": {
"EntryPath": {
"type": "object",
"required": [
"unEntryPath"
],
"pattern": "(/${pathSegment})+",
"properties": {
"unEntryPath": {
"items": {
"$ref": "#/components/schemas/PathSegment"
},
"type": "array",
"minItems": 1
}
}
"type": "string",
"pattern": "(/[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_]*)+"
},
"Directory": {
"type": "object",
Expand Down Expand Up @@ -75,10 +63,6 @@
}
}
},
"PathSegment": {
"type": "string",
"pattern": "[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_]*"
},
"NewEntry": {
"type": "object",
"required": [
Expand Down
31 changes: 24 additions & 7 deletions lib/Coffer/Path.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import Data.Text qualified as T
import Fmt (Buildable, build, fmt, pretty)
import GHC.Generics (Generic)
import Servant (FromHttpApiData(..), ToHttpApiData(..))
import Text.Interpolation.Nyan

-- $setup
-- >>> import Fmt (pretty, build)
Expand Down Expand Up @@ -71,14 +72,18 @@ instance ToSchema PathSegment where
& Schema.pattern ?~ pathSegmentPattern
& type_ ?~ OpenApiString
where
pathSegmentPattern = "[" <> T.pack pathSegmentAllowedCharacters <> "]*"
pathSegmentPattern = [int|s|
[#{pathSegmentAllowedCharacters}]
|]

mkPathSegment :: Text -> Either Text PathSegment
mkPathSegment segment
| T.null segment =
Left "Path segments must contain at least 1 character"
| T.any (`notElem` pathSegmentAllowedCharacters) segment =
Left $ "Path segments can only contain the following characters: '" <> T.pack pathSegmentAllowedCharacters <> "'"
Left $ [int|s|
Path segments can only contain the following characters: '#{pathSegmentAllowedCharacters}'
|]
| otherwise = Right $ UnsafeMkPathSegment segment

pathSegmentAllowedCharacters :: [Char]
Expand Down Expand Up @@ -139,12 +144,24 @@ newtype EntryPath = EntryPath { unEntryPath :: NonEmpty PathSegment }
instance A.ToJSON EntryPath where
toJSON = String . pretty

instance ToParamSchema EntryPath where
toParamSchema _ =
mempty
& Schema.pattern ?~ entryPathPattern
& type_ ?~ OpenApiString
where
segmentPattern :: Pattern
segmentPattern = [int|s|
[#{pathSegmentAllowedCharacters}]*
|]

entryPathPattern :: Pattern
entryPathPattern = [int|s|
(/#{segmentPattern})+
|]

instance ToSchema EntryPath where
declareNamedSchema proxy = do
namedSchema <- genericDeclareNamedSchema defaultSchemaOptions proxy
pure
$ namedSchema
& schema . Schema.pattern ?~ "(/${pathSegment})+"
declareNamedSchema proxy = pure $ NamedSchema (Just "EntryPath") (toParamSchema proxy)

instance ToHttpApiData EntryPath where
toUrlPiece = fmt . build
Expand Down

0 comments on commit 93c62d6

Please sign in to comment.