string-variants-0.3.1.0: Constrained text newtypes
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.StringVariants.NullableNonEmptyText

Synopsis

Documentation

newtype NullableNonEmptyText n Source #

Newtype wrapper around Maybe NonEmptyText that converts empty string to Nothing.

NullableNonEmptyText n is used in API types to represent optional text fields when you do not want an empty string to fail to parse. Like NonEmptyText, the payload Text is guaranteed to be non-empty, within the character limit, and stripped of whitespace. Unlike NonEmptyText, it will successfully parse empty strings as nullNonEmptyText.

Since Aeson version 2.2, fields of this type maybe be missing, null, or empty without failing to parse. Avoid using Maybe (NullableNonEmptyText n) in API types, since it creates unnecessary edge cases that complicate the code.

NB: When using a version of Aeson prior to 2.2, you must use Maybe (NullableNonEmptyText n) if you want to allow missing or null fields to parse.

  data Person = Person
    { name :: NonEmptyText 50
    , catchphrase :: NullableNonEmptyText 500
    }
  

With this type definition, these four JSON objects below are valid and parse as Person Daniel nullNonEmptyText.

{"name": "Daniel"}
{"name": "Daniel", catchphrase: null}
{"name": "Daniel", catchphrase: ""}
{"name": "Daniel", catchphrase: " "}

These two JSON objects parses as Person Daniel (mkNullableNonEmptyText "Yabba-Dabba Do!")

{"name": "Daniel", catchphrase: "Yabba-Dabba Do!"}
{"name": "Daniel", catchphrase: "    Yabba-Dabba Do!   "}

Use nullableNonEmptyTextToMaybeNonEmptyText to extract Maybe (NonEmptyText n) from NullableNonEmptyText n.

Instances

Instances details
Lift (NullableNonEmptyText n :: Type) Source # 
Instance details

Defined in Data.StringVariants.NullableNonEmptyText

Methods

lift :: Quote m => NullableNonEmptyText n -> m Exp #

liftTyped :: forall (m :: Type -> Type). Quote m => NullableNonEmptyText n -> Code m (NullableNonEmptyText n) #

(KnownNat n, 1 <= n) => FromJSON (NullableNonEmptyText n) Source # 
Instance details

Defined in Data.StringVariants.NullableNonEmptyText

ToJSON (NullableNonEmptyText n) Source # 
Instance details

Defined in Data.StringVariants.NullableNonEmptyText

Generic (NullableNonEmptyText n) Source # 
Instance details

Defined in Data.StringVariants.NullableNonEmptyText

Associated Types

type Rep (NullableNonEmptyText n) :: Type -> Type #

Read (NullableNonEmptyText n) Source # 
Instance details

Defined in Data.StringVariants.NullableNonEmptyText

Show (NullableNonEmptyText n) Source # 
Instance details

Defined in Data.StringVariants.NullableNonEmptyText

Eq (NullableNonEmptyText n) Source # 
Instance details

Defined in Data.StringVariants.NullableNonEmptyText

type Rep (NullableNonEmptyText n) Source # 
Instance details

Defined in Data.StringVariants.NullableNonEmptyText

type Rep (NullableNonEmptyText n) = D1 ('MetaData "NullableNonEmptyText" "Data.StringVariants.NullableNonEmptyText" "string-variants-0.3.1.0-7oZ1CmOM9DaEt259oVGHqF" 'True) (C1 ('MetaCons "NullableNonEmptyText" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Maybe (NonEmptyText n)))))

Constructing

literalNullableNonEmptyText :: forall (s :: Symbol) (n :: Nat). IsNullableNonEmptyText n s => NullableNonEmptyText n Source #

This requires the text to be non-empty. For the empty text just use the constructor `NullableNonEmptyText Nothing`

Conversion

Functions