You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Attempting to call NonEmptyString("") would result in the following compilation error:
Error:/src/main/scala/examples/Main.scala:9:16NonEmptyString("")
^^^^^^^^^^^^^^^^^^
—— NewtypeError ——————————————————————————————————————————————————————————
NonEmptyString was called with an INVALIDString.
input: ""check: input.nonEmpty
———————————————————————————————————————————————————————————————————————————
importneotype.*typeNonEmptyString=NonEmptyString.TypeobjectNonEmptyStringextendsNewtype[String]:overrideinlinedefvalidate(value: String):Result=if value.nonEmpty thentrueelse"String must not be empty"
importneotype.interop.ziojson.givenimportzio.json.*caseclassPerson(name: NonEmptyString, age: Int) derivesJsonCodecvalparsed="""{"name": "Kit", "age": 30}""".fromJson[Person]
// Right(Person(NonEmptyString("Kit"), 30))valfailed="""{"name": "", "age": 30}""".fromJson[Person]
// Left(".name(String must not be empty)")
By importing neotype.ziojson.given, we automatically generate a JsonCodec for NonEmptyString. Custom
failure messages are also supported (by overriding def failureMessage in the Newtype definition).
Note that import neotype.interop.ziojson.given needs to be in the same file as Person, notNonEmptyString.
The generated JsonCodec is not made available to the entire project, but only to the file where it is imported.