-
Notifications
You must be signed in to change notification settings - Fork 68
/
ValidatedNewtypesSpec.scala
108 lines (86 loc) · 3.11 KB
/
ValidatedNewtypesSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* Copyright 2021-2023 Disney Streaming
*
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://disneystreaming.github.io/TOST-1.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package smithy4s
import smithy4s.schema.Schema.bijection
import smithy4s.schema.Schema.string
import munit.Assertions
class ValidatedNewtypesSpec() extends munit.FunSuite {
val id1 = "id1"
val id2 = "id2"
test("Validated newtypes are consistent") {
expect.same(AccountId.unsafeApply(id1).value, id1)
expect.different(
AccountId.unsafeApply(id1).value,
AccountId.unsafeApply(id2).value
)
expect.different(
implicitly[ShapeTag[AccountId]],
implicitly[ShapeTag[DeviceId]]
)
expect.same(AccountId.unapply(AccountId.unsafeApply(id1)), Some(id1))
}
test("Newtypes have well defined unapply") {
val aid = AccountId.unsafeApply(id1)
aid match {
case AccountId(id) => expect(id == id1)
}
}
test("Validated newtypes unsafeApply throws exception") {
val e = Assertions.intercept[IllegalArgumentException] {
AccountId.unsafeApply("!^%&")
}
expect.same(
e.getMessage(),
"String '!^%&' does not match pattern '[a-zA-Z0-9]+'"
)
}
type DeviceId = DeviceId.Type
object DeviceId extends NewtypeValidated[String] {
val id: ShapeId = ShapeId("foo", "DeviceId")
val hints: Hints = Hints.empty
val underlyingSchema: Schema[String] = string
.withId(id)
.addHints(hints)
.validated(smithy.api.Length(min = Some(1L), max = None))
implicit val schema: Schema[DeviceId] =
bijection(underlyingSchema, asBijectionUnsafe)
val validator: Validator[String, DeviceId] = Validator
.of[String, DeviceId]
.validating(smithy.api.Length(min = Some(1L), max = None))
.build
@inline def apply(a: String): Either[String, DeviceId] =
validator.validate(a)
}
type AccountId = AccountId.Type
object AccountId extends NewtypeValidated[String] {
def id: smithy4s.ShapeId = ShapeId("foo", "AccountId")
val hints: Hints = Hints.empty
val underlyingSchema: Schema[String] = string
.withId(id)
.addHints(hints)
.validated(smithy.api.Length(min = Some(1L), max = None))
.validated(smithy.api.Pattern("[a-zA-Z0-9]+"))
implicit val schema: Schema[AccountId] =
bijection(underlyingSchema, asBijectionUnsafe)
val validator: Validator[String, AccountId] = Validator
.of[String, AccountId]
.validating(smithy.api.Length(min = Some(1L), max = None))
.validating(smithy.api.Pattern("[a-zA-Z0-9]+"))
.build
@inline def apply(a: String): Either[String, AccountId] =
validator.validate(a)
}
}