-
Notifications
You must be signed in to change notification settings - Fork 18
/
Project.scala
250 lines (213 loc) · 9.91 KB
/
Project.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* Copyright © 2021 - 2023 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
* SPDX-License-Identifier: Apache-2.0
*/
package dsp.valueobjects
import zio.json._
import zio.prelude.Validation
import scala.util.matching.Regex
import dsp.errors.ValidationException
object Project {
// A regex sub-pattern for project IDs, which must consist of 4 hexadecimal digits.
private val ProjectIDPattern: String = """\p{XDigit}{4,4}"""
// A regex for matching a string containing the project ID.
private val ProjectIDRegex: Regex = ("^" + ProjectIDPattern + "$").r
/**
* Regex which matches string that:
* - is 3-20 characters long,
* - contains small and capital letters, numbers, special characters: `-` and `_`,
* - cannot start with number nor allowed special characters.
*/
private val shortnameRegex: Regex = "^[a-zA-Z][a-zA-Z0-9_-]{2,19}$".r
object ErrorMessages {
val ShortcodeMissing = "Shortcode cannot be empty."
val ShortcodeInvalid = (v: String) => s"Invalid project shortcode: $v"
val ShortnameMissing = "Shortname cannot be empty."
val ShortnameInvalid = (v: String) => s"Shortname is invalid: $v"
val NameMissing = "Name cannot be empty."
val NameInvalid = "Name must be 3 to 256 characters long."
val ProjectDescriptionMissing = "Description cannot be empty."
val ProjectDescriptionInvalid = "Description must be 3 to 40960 characters long."
val KeywordsMissing = "Keywords cannot be empty."
val KeywordsInvalid = "Keywords must be 3 to 64 characters long."
val LogoMissing = "Logo cannot be empty."
}
/**
* Project Shortcode value object.
*/
sealed abstract case class Shortcode private (value: String)
object Shortcode { self =>
implicit val decoder: JsonDecoder[Shortcode] = JsonDecoder[String].mapOrFail { value =>
Shortcode.make(value).toEitherWith(e => e.head.getMessage)
}
implicit val encoder: JsonEncoder[Shortcode] =
JsonEncoder[String].contramap((shortcode: Shortcode) => shortcode.value)
def unsafeFrom(str: String): Shortcode = make(str).fold(e => throw e.head, identity)
def make(value: String): Validation[ValidationException, Shortcode] =
if (value.isEmpty) Validation.fail(ValidationException(ErrorMessages.ShortcodeMissing))
else
ProjectIDRegex.matches(value.toUpperCase) match {
case false => Validation.fail(ValidationException(ErrorMessages.ShortcodeInvalid(value)))
case true => Validation.succeed(new Shortcode(value.toUpperCase) {})
}
}
/**
* Project Shortname value object.
*/
sealed abstract case class Shortname private (value: String)
object Shortname { self =>
implicit val decoder: JsonDecoder[Shortname] = JsonDecoder[String].mapOrFail { value =>
Shortname.make(value).toEitherWith(e => e.head.getMessage)
}
implicit val encoder: JsonEncoder[Shortname] =
JsonEncoder[String].contramap((shortname: Shortname) => shortname.value)
def unsafeFrom(str: String): Shortname = make(str).fold(e => throw e.head, identity)
def make(value: String): Validation[ValidationException, Shortname] =
if (value.isEmpty) Validation.fail(ValidationException(ErrorMessages.ShortnameMissing))
else
Validation
.fromOption(validateAndEscape(value))
.mapError(_ => ValidationException(ErrorMessages.ShortnameInvalid(value)))
.map(new Shortname(_) {})
private def validateAndEscape(shortname: String): Option[String] = {
val defaultSharedOntologiesProject = "DefaultSharedOntologiesProject"
if (shortname == defaultSharedOntologiesProject) {
Some(defaultSharedOntologiesProject)
} else {
shortnameRegex
.findFirstIn(shortname)
.flatMap(Iri.toSparqlEncodedString)
}
}
}
/**
* Project Name value object.
* (Formerly `Longname`)
*/
sealed abstract case class Name private (value: String)
object Name { self =>
implicit val decoder: JsonDecoder[Name] = JsonDecoder[String].mapOrFail { value =>
Name.make(value).toEitherWith(e => e.head.getMessage)
}
implicit val encoder: JsonEncoder[Name] =
JsonEncoder[String].contramap((name: Name) => name.value)
private def isLengthCorrect(name: String): Boolean = name.length > 2 && name.length < 257
def make(value: String): Validation[ValidationException, Name] =
if (value.isEmpty) Validation.fail(ValidationException(ErrorMessages.NameMissing))
else if (!isLengthCorrect(value)) Validation.fail(ValidationException(ErrorMessages.NameInvalid))
else Validation.succeed(new Name(value) {})
def make(value: Option[String]): Validation[ValidationException, Option[Name]] =
value match {
case None => Validation.succeed(None)
case Some(v) => self.make(v).map(Some(_))
}
}
/**
* Description value object.
*/
sealed abstract case class Description private (value: Seq[V2.StringLiteralV2])
object Description { self =>
implicit val decoder: JsonDecoder[Description] = JsonDecoder[Seq[V2.StringLiteralV2]].mapOrFail { value =>
Description.make(value).toEitherWith(e => e.head.getMessage)
}
implicit val encoder: JsonEncoder[Description] =
JsonEncoder[Seq[V2.StringLiteralV2]].contramap((description: Description) => description.value)
private def isLengthCorrect(descriptionToCheck: Seq[V2.StringLiteralV2]): Boolean = {
val checked = descriptionToCheck.filter(d => d.value.length > 2 && d.value.length < 40961)
descriptionToCheck == checked
}
def make(value: Seq[V2.StringLiteralV2]): Validation[ValidationException, Description] =
if (value.isEmpty) Validation.fail(ValidationException(ErrorMessages.ProjectDescriptionMissing))
else if (!isLengthCorrect(value)) Validation.fail(ValidationException(ErrorMessages.ProjectDescriptionInvalid))
else Validation.succeed(new Description(value) {})
def make(value: Option[Seq[V2.StringLiteralV2]]): Validation[ValidationException, Option[Description]] =
value match {
case Some(v) => self.make(v).map(Some(_))
case None => Validation.succeed(None)
}
}
/**
* Project Keywords value object.
*/
sealed abstract case class Keywords private (value: Seq[String])
object Keywords { self =>
implicit val decoder: JsonDecoder[Keywords] = JsonDecoder[Seq[String]].mapOrFail { value =>
Keywords.make(value).toEitherWith(e => e.head.getMessage)
}
implicit val encoder: JsonEncoder[Keywords] =
JsonEncoder[Seq[String]].contramap((keywords: Keywords) => keywords.value)
private def isLengthCorrect(keywordsToCheck: Seq[String]): Boolean = {
val checked = keywordsToCheck.filter(k => k.length > 2 && k.length < 65)
keywordsToCheck == checked
}
def make(value: Seq[String]): Validation[ValidationException, Keywords] =
if (value.isEmpty) Validation.fail(ValidationException(ErrorMessages.KeywordsMissing))
else if (!isLengthCorrect(value)) Validation.fail(ValidationException(ErrorMessages.KeywordsInvalid))
else Validation.succeed(new Keywords(value) {})
def make(value: Option[Seq[String]]): Validation[ValidationException, Option[Keywords]] =
value match {
case Some(v) => self.make(v).map(Some(_))
case None => Validation.succeed(None)
}
}
/**
* Project Logo value object.
*/
sealed abstract case class Logo private (value: String)
object Logo { self =>
implicit val decoder: JsonDecoder[Logo] = JsonDecoder[String].mapOrFail { value =>
Logo.make(value).toEitherWith(e => e.head.getMessage)
}
implicit val encoder: JsonEncoder[Logo] =
JsonEncoder[String].contramap((logo: Logo) => logo.value)
def make(value: String): Validation[ValidationException, Logo] =
if (value.isEmpty)
Validation.fail(ValidationException(ErrorMessages.LogoMissing))
else {
Validation.succeed(new Logo(value) {})
}
def make(value: Option[String]): Validation[ValidationException, Option[Logo]] =
value match {
case Some(v) => self.make(v).map(Some(_))
case None => Validation.succeed(None)
}
}
/**
* ProjectSelfjoin value object.
*/
sealed abstract case class ProjectSelfJoin private (value: Boolean)
object ProjectSelfJoin { self =>
implicit val decoder: JsonDecoder[ProjectSelfJoin] = JsonDecoder[Boolean].mapOrFail { value =>
ProjectSelfJoin.make(value).toEitherWith(e => e.head.getMessage)
}
implicit val encoder: JsonEncoder[ProjectSelfJoin] =
JsonEncoder[Boolean].contramap((selfJoin: ProjectSelfJoin) => selfJoin.value)
def make(value: Boolean): Validation[ValidationException, ProjectSelfJoin] =
Validation.succeed(new ProjectSelfJoin(value) {})
def make(value: Option[Boolean]): Validation[ValidationException, Option[ProjectSelfJoin]] =
value match {
case Some(v) => self.make(v).map(Some(_))
case None => Validation.succeed(None)
}
}
/**
* ProjectStatus value object.
*/
sealed abstract case class ProjectStatus private (value: Boolean)
object ProjectStatus { self =>
val deleted = new ProjectStatus(false) {}
val active = new ProjectStatus(true) {}
implicit val decoder: JsonDecoder[ProjectStatus] = JsonDecoder[Boolean].mapOrFail { value =>
ProjectStatus.make(value).toEitherWith(e => e.head.getMessage)
}
implicit val encoder: JsonEncoder[ProjectStatus] =
JsonEncoder[Boolean].contramap((status: ProjectStatus) => status.value)
def make(value: Boolean): Validation[ValidationException, ProjectStatus] =
Validation.succeed(new ProjectStatus(value) {})
def make(value: Option[Boolean]): Validation[ValidationException, Option[ProjectStatus]] =
value match {
case Some(v) => self.make(v).map(Some(_))
case None => Validation.succeed(None)
}
}
}