Skip to content

Commit

Permalink
fixup! Add Workspace name validation (#834)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackheaven committed Oct 2, 2023
1 parent 9a4991f commit d50e616
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ All notable changes to this project will be documented in this file.

### Changed

- `workspaceName` is typed as `IndexName`, which respect ElasticSearch constraints

### Removed

### Fixed
Expand Down
20 changes: 19 additions & 1 deletion src/Monocle/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ import Data.ByteString qualified as BS
import Data.Char (isLetter, isLowerCase)
import Data.Either.Validation (Validation (Failure, Success))
import Data.Map qualified as Map
import Data.Text qualified as T (all, dropWhileEnd, isPrefixOf, null, replace, toUpper, uncons, unpack)
import Data.Text qualified as T
import Data.Text.Encoding qualified as T
import Data.Text.Lazy qualified as TL
import Dhall qualified
Expand Down Expand Up @@ -480,6 +480,24 @@ getIdentByAliasFromIdents alias idents' = case find isMatched idents' of
isMatched :: Ident -> Bool
isMatched Ident {..} = alias `elem` aliases

-- | Create an IndexName with checked constraints
--
-- >>> mkIndexName ""
-- Left "Is empty"
-- >>> mkIndexName $ T.replicate 256 "x"
-- Left "Is longer than 255 bytes"
-- >>> mkIndexName "azerTY"
-- Left "Contains uppercase letter(s)"
-- >>> mkIndexName "hello#world"
-- Left "Includes [\\/*?\"<>| ,#:]"
-- >>> mkIndexName "-test"
-- Left "Starts with [-_+.]"
-- >>> mkIndexName "."
-- Left "Is (.|..)"
-- >>> mkIndexName ".."
-- Left "Is (.|..)"
-- >>> mkIndexName "hello-world_42"
-- Right (IndexName "hello-world_42")
mkIndexName :: Text -> Either Text IndexName
mkIndexName name = do
let check explanation p = if p then Right () else Left explanation
Expand Down
3 changes: 1 addition & 2 deletions src/Monocle/Effects.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ import Effectful.Dispatch.Static.Primitive qualified as EffStatic
import Monocle.Effects.Compat ()

import GHC.IO.Handle (hClose)
import Monocle.Config (ConfigStatus)
import Monocle.Config (ConfigStatus, IndexName, mkIndexName)
import System.Directory
import System.Posix.Temp (mkstemp)
import Test.Tasty
Expand Down Expand Up @@ -99,7 +99,6 @@ import Effectful.Reader.Static qualified as E
import Effectful.Retry as Retry
import Monocle.Client (MonocleClient)
import Monocle.Client.Api (crawlerAddDoc, crawlerCommit, crawlerCommitInfo)
import Monocle.Config (IndexName, mkIndexName)

import Monocle.Protob.Crawler qualified as CrawlerPB

Expand Down

0 comments on commit d50e616

Please sign in to comment.