git.iko.soy/rowtype-yoga/purescript-yoga-json
on main help sign in
Add instances for `NonEmptyString` (#13)
commit0a674d1da52a8d3cfa0b3756fa5ddd0f4788c3f3 authorMark Eibes <mark.eibes@gmail.com> dateTue 25 Oct 2022, 14:53 (4 years ago) parente7ffcc2
3 files changed+23−2
spago.dhall+2−1 src/Yoga/JSON.purs+11 test/BasicsSpec.purs+10−1
spago.dhall+2−1
@@ -4,7 +4,6 @@
44, dependencies =
55 [ "arrays"
66 , "bifunctors"
7− , "js-bigints"
87 , "control"
98 , "datetime"
109 , "effect"
@@ -15,6 +14,7 @@
1514 , "foreign-object"
1615 , "identity"
1716 , "integers"
17+ , "js-bigints"
1818 , "js-date"
1919 , "lists"
2020 , "maybe"
@@ -25,6 +25,7 @@
2525 , "partial"
2626 , "prelude"
2727 , "record"
28+ , "strings"
2829 , "transformers"
2930 , "tuples"
3031 , "typelevel-prelude"
src/Yoga/JSON.purs+11
@@ -55,6 +55,8 @@ import Data.Maybe (Maybe(..), fromMaybe, fromMaybe', maybe)
5555import Data.Newtype (class Newtype)
5656import Data.Nullable (Nullable, toMaybe, toNullable)
5757import Data.Number as Number
58+import Data.String.NonEmpty.Internal (NonEmptyString)
59+import Data.String.NonEmpty.Internal as NonEmptyString
5860import Data.Symbol (class IsSymbol, reflectSymbol)
5961import Data.Traversable (traverse)
6062import Data.Tuple (Tuple(..))
@@ -212,6 +214,12 @@ instance ReadForeign BigInt where
212214instance ReadForeign String where
213215 readImpl = readString
214216
217+instance ReadForeign NonEmptyString where
218+ readImpl = readString >=>
219+ NonEmptyString.fromString
220+ >>> note (pure $ ForeignError "String must not be empty")
221+ >>> except
222+
215223instance ReadForeign Boolean where
216224 readImpl = readBoolean
217225
@@ -402,6 +410,9 @@ instance WriteForeign Foreign where
402410instance WriteForeign String where
403411 writeImpl = unsafeToForeign
404412
413+instance WriteForeign NonEmptyString where
414+ writeImpl = unsafeToForeign
415+
405416instance WriteForeign Int where
406417 writeImpl = unsafeToForeign
407418
test/BasicsSpec.purs+10−1
@@ -12,11 +12,13 @@ import Data.Map as Map
1212import Data.Maybe (Maybe(..), fromJust, fromMaybe')
1313import Data.Newtype (class Newtype, un)
1414import Data.Nullable as Nullable
15+import Data.String.NonEmpty (NonEmptyString, nes)
1516import Data.Tuple (Tuple(..))
1617import Data.Tuple.Nested ((/\))
1718import Data.Variant (Variant, inj)
1819import Effect.Class (liftEffect)
1920import Effect.Now (nowDateTime)
21+import Foreign (ForeignError(..))
2022import Foreign.Object as Object
2123import Js.BigInt.BigInt (BigInt)
2224import Js.BigInt.BigInt as BigInt
@@ -133,13 +135,20 @@ spec = describe "En- and decoding" $ do
133135 roundtrips (UntaggedVariant (erwin "e") ∷ UntaggedVariant (Erwin ()))
134136 let bareVariant = erwin "e"
135137 let res = writeJSON (UntaggedVariant bareVariant ∷ UntaggedVariant ExampleVariant)
136− let expected = """"e""""
138+ let expected = "\"e\""
137139 res `shouldEqual` expected
138140 let
139141 parsed ∷ _ (UntaggedVariant ExampleVariant)
140142 parsed = readJSON expected
141143 (un UntaggedVariant <$> parsed) `shouldEqual` Right bareVariant
142144
145+ describe "works on non empty strings" do
146+ it "roundtrips NonEmptyString" do
147+ roundtrips (nes (Proxy :: Proxy "Non-Empty"))
148+ it "fails to decode empty strings" do
149+ let (result :: (Either _ NonEmptyString)) = readJSON (show "")
150+ result `shouldEqual` Left (pure $ ForeignError "String must not be empty")
151+
143152type ExampleVariant = ("erwin" ∷ String, "jackie" ∷ Int)
144153type ExampleTaggedVariant t v = TaggedVariant t v ExampleVariant
145154