-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9a8e69f
commit a4c26c6
Showing
5 changed files
with
177 additions
and
89 deletions.
There are no files selected for viewing
37 changes: 3 additions & 34 deletions
37
zio-quickstart-schema/src/main/scala/dev/zio/quickstart/Person.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,15 @@ | ||
package dev.zio.quickstart | ||
|
||
import zio._ | ||
import zio.schema._ | ||
import zio.schema.annotation.validate | ||
import zio.schema.validation.Validation | ||
import zio.stream.ZStream | ||
|
||
// one can choose the detailed way of validation or use annotations | ||
|
||
case class Person( | ||
name: String, | ||
case class Person(name: String, | ||
@validate(Validation.greaterThan(18)) | ||
age: Int) | ||
|
||
object Person { | ||
implicit val schema : Schema[Person] = DeriveSchema.gen | ||
} | ||
|
||
|
||
// one can choose the detailed way of validation or use annotations | ||
|
||
//case class Person(name: String, age: Int) | ||
|
||
// object Person { | ||
// implicit val schema: Schema[Person] = CaseClass2( | ||
// id0 = TypeId.fromTypeName("Person"), | ||
// field01 = Schema.Field( | ||
// name0 = "name", | ||
// schema0 = Schema[String], | ||
// validation0 = Validation.minLength(15), | ||
// get0 = (p: Person) => p.name, | ||
// set0 = { (p: Person, s: String) => p.copy(name = s) } | ||
// ), | ||
// field02 = Schema.Field( | ||
// name0 = "age", | ||
// schema0 = Schema[Int], | ||
// validation0 = Validation.greaterThan(18), | ||
// get0 = (p: Person) => p.age, | ||
// set0 = { (p: Person, age: Int) => p.copy(age = age) } | ||
// ), | ||
// construct0 = (name, age) => Person(name, age), | ||
// ) | ||
|
||
// } | ||
|
||
implicit val schema: Schema[Person] = DeriveSchema.gen | ||
} |
30 changes: 30 additions & 0 deletions
30
zio-quickstart-schema/src/main/scala/dev/zio/quickstart/PersonWithEmail.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package dev.zio.quickstart | ||
|
||
import zio._ | ||
import zio.schema.Schema.CaseClass1 | ||
import zio.schema._ | ||
import zio.schema.annotation.validate | ||
import zio.schema.validation._ | ||
import zio.stream.ZStream | ||
|
||
case class PersonWithEmail(email: String) | ||
|
||
object PersonWithEmail { | ||
|
||
val localPart = Regex.letter.atLeast(3) | ||
val tld = (Regex.literal("org") | Regex.literal("net") | Regex.literal("com")) // Matches top-level domains (2 or more letters) | ||
val regexValidator = localPart ~ Regex.digit.atLeast(1) ~ Regex.literal("@") ~ Regex.letter.atLeast(3) ~ Regex.literal(".") ~ tld | ||
|
||
implicit val schema: Schema[PersonWithEmail] = CaseClass1( | ||
id0 = TypeId.fromTypeName("PersonWithEmail"), | ||
field0 = Schema.Field( | ||
name0 = "email", | ||
schema0 = Schema[String], | ||
validation0 = Validation.regex(regexValidator), | ||
get0 = (p: PersonWithEmail) => p.email, | ||
set0 = { (p: PersonWithEmail, s: String) => p.copy(email = s) } | ||
), | ||
defaultConstruct0 = email => PersonWithEmail(email), | ||
) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters