-
Notifications
You must be signed in to change notification settings - Fork 6
/
DValidationSpec.scala
232 lines (188 loc) · 6.7 KB
/
DValidationSpec.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package net.atinu.dvalidation
import net.atinu.dvalidation.util.ValidationSuite
class DValidationSpec extends ValidationSuite {
import net.atinu.dvalidation.Path._
import net.atinu.dvalidation.Validator._
test("String is blank") {
notBlank("") should beInvalidWithError(new IsEmptyStringError())
}
test("String with whitespace is blank") {
notBlank(" ") should beInvalidWithError(new IsEmptyStringError())
}
test("String with whitespace is not blank") {
notBlank(" ", trimWhitespace = false) should beValidResult(" ")
}
test("String is not blank") {
notBlank("s") should beValidResult("s")
}
test("validate any value") {
ensure(1)("error.dvalidation.isequal", 1)(_ == 1) should beValidResult(1)
}
test("validate any value 2") {
ensure(1)("error.dvalidation.isequal", 2)(_ == 2) should beInvalidWithError(new CustomValidationError(1, "error.dvalidation.isequal", Seq("2")))
}
test("List is empty") {
hasElements(List.empty[String]) should beInvalidWithError(new IsEmptySeqError())
}
test("List is not empty") {
val a: DValidation[List[Int]] = hasElements(List(1, 2))
a should beValidResult(List(1, 2))
}
test("Option is empty") {
isSome(None.asInstanceOf[Option[String]]) should beInvalidWithError(new IsNoneError())
}
test("Option is not empty") {
val a: DValidation[Option[Int]] = isSome(Some(1))
a should beValidResult(Some(1))
}
test("Validate is equal") {
isEqual(2, 2) should beValidResult(2)
(2 is_== 2) should beValidResult(2)
}
test("validate is equal - error") {
isEqual(2, 3) should beInvalidWithError(new IsNotEqualError(2, 3))
}
test("Validate is equal strict") {
import scalaz.std.anyVal._
isEqualStrict(2, 2) should beValidResult(2)
(2 is_=== 2) should beValidResult(2)
}
test("validate is equal strict - error") {
import scalaz.std.anyVal._
isEqualStrict(2, 3) should beInvalidWithError(new IsNotEqualError(2, 3))
(2 is_=== 3) should beInvalidWithError(new IsNotEqualError(2, 3))
}
test("Validate a > b") {
isGreaterThan(3, 2) should beValidResult(3)
3 is_> 2 should beValidResult(3)
}
test("Validate a > b - error") {
isGreaterThan(2, 3) should beInvalidWithError(new IsNotGreaterThenError(3, 2, false))
}
test("Validate a >= b") {
isGreaterThan(3, 3, isInclusive = true) should beValidResult(3)
(3 is_>= 3) should beValidResult(3)
}
test("Validate a >= b - error") {
isGreaterThan(2, 3, isInclusive = true) should beInvalidWithError(new IsNotGreaterThenError(3, 2, true))
}
test("Validate a < b") {
isSmallerThan(2, 3) should beValidResult(2)
2 is_< 3 should beValidResult(2)
}
test("Validate a < b - error") {
isSmallerThan(3, 2) should beInvalidWithError(new IsNotLowerThenError(2, 3, false))
}
test("Validate a <= b") {
isSmallerThan(3, 3, isInclusive = true) should beValidResult(3)
(3 is_<= 3) should beValidResult(3)
}
test("Validate a <= b - error") {
isSmallerThan(3, 2, isInclusive = true) should beInvalidWithError(new IsNotLowerThenError(2, 3, true))
}
test("Validate a < b < c") {
isInRange(4, min = 1, max = 5) should beValidResult(4)
}
test("Validate a < b < c - error high") {
isInRange(6, min = 1, max = 5) should beInvalidWithError(new IsNotLowerThenError(5, 6, false))
}
test("Validate a < b < c - error low") {
isInRange(0, min = 1, max = 5) should beInvalidWithError(new IsNotGreaterThenError(1, 0, false))
}
test("isInRange should only work on valid ranges") {
intercept[IllegalArgumentException] {
isInRange(0, min = 8, max = 7)
}
intercept[IllegalArgumentException] {
isInRange(0, min = 8, max = 8)
}
}
test("Validate if not Monoid zero") {
import scalaz.std.anyVal._
notZero(1) should beValidResult(1)
}
test("Validate if not Monoid zero - error") {
import scalaz.std.anyVal._
notZero(0) should beInvalidWithError(new IsZeroError(0))
}
test("do a custom validation") {
val a = if ("a" == "a") "a".valid else invalid("a", "error.notA")
a should beValid
}
test("do a custom validation 2") {
val a = Validator.validate("a")(_ == "a")(error = new CustomValidationError("a", "error.notA"))
a should beValid
}
case class VTest(a: Int, b: String, c: Option[String])
test("validate case class") {
val vtest = VTest(1, "sdf", Some("a"))
val res: DValidation[VTest] = vtest.validateWith(
ensure(vtest.a)("should be 1", 1)(_ == 1),
notBlank(vtest.b)
)
res should beValidResult(vtest)
}
test("validate case class 2") {
val vtest = VTest(1, "", Some("a"))
val res = vtest.validateWith(
ensure(vtest.a)("validation.equal", 2)(_ == 2),
notBlank(vtest.b)
)
res should beInvalidWithErrors(
new CustomValidationError(1, "validation.equal", Seq("2")),
new IsEmptyStringError()
)
}
case class VTestNested(value: Int, nest: VTest)
test("validate case class for attribute") {
val vtest = VTest(1, "", None)
val validateWith = vtest.validateWith(
(vtest.a is_== 2) forAttribute 'a,
notBlank(vtest.b) forAttribute 'b,
isSome(vtest.c) forAttribute 'c
)
validateWith should beInvalidWithErrors(
new IsNotEqualError(1, 2).nestAttribute('a),
new IsEmptyStringError("/b".asPath),
new IsNoneError("/c".asPath)
)
val vTestNested = VTestNested(5, vtest)
val resNest = vTestNested.validateWith(
isEqual(vTestNested.value, 1).forAttribute('value),
validateWith.forAttribute('nest)
)
resNest should beInvalidWithErrors(
new IsNotEqualError(5, 1)
.nestAttribute('value),
new IsNotEqualError(1, 2)
.nestAttribute('a).nestAttribute('nest),
new IsEmptyStringError("/nest/b".asPath),
new IsNoneError("/nest/c".asPath)
)
}
case class VTestSeq(value: Int, tests: Seq[VTest])
test("validate a seq") {
val vtest = VTest(1, "", None)
val vtest2 = VTest(2, "", Some("d"))
val vtseq = VTestSeq(1, Seq(vtest, vtest2))
val vtestValidator: DValidator[VTest] = Validator.template[VTest] { value =>
value.validateWith(
isEqual(value.a, 2) forAttribute 'a,
notBlank(value.b) forAttribute 'b,
isSome(value.c) forAttribute 'c
)
}
val res = vtseq.validateWith(
isEqual(vtseq.value, 2) forAttribute 'value
).withValidations(
validSequence(vtseq.tests, vtestValidator) forAttribute 'tests
)
res should beInvalidWithErrors(
new IsNotEqualError(1, 2).nestAttribute('value),
new IsNotEqualError(1, 2).nest("/tests/[0]/a".asPath),
new IsEmptyStringError("/tests/[0]/b".asPath),
new IsNoneError("/tests/[0]/c".asPath),
new IsEmptyStringError("/tests/[1]/b".asPath)
)
}
}