1 /*
2  *  Make.org Core API
3  *  Copyright (C) 2018 Make.org
4  *
5  * This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  *
18  */
19 
20 package org.make.api.technical.directives
21 
22 import akka.http.scaladsl.server.{Directive0, Directives}
23 import cats.data.Validated.{Invalid, Valid}
24 import org.make.core.{ValidationFailedError, Validator}
25 
26 trait ValidationDirectives {
27   self: Directives =>
28 
29   def validate[T](value: T, validator: Validator[T]): Directive0 = {
30     mapInnerRoute { inner =>
31       validator.validate(value) match {
32         case Invalid(errors) => failWith(ValidationFailedError(errors))
33         case Valid(_)        => inner
34       }
35     }
36   }
37 
38 }
Line Stmt Id Pos Tree Symbol Tests Code
30 35906 1072 - 1260 Apply akka.http.scaladsl.server.directives.BasicDirectives.mapInnerRoute org.make.api.technical.directives.validationdirectivestest ValidationDirectives.this.mapInnerRoute(((inner: akka.http.scaladsl.server.Route) => validator.validate(value) match { case (e: cats.data.NonEmptyChain[org.make.core.ValidationError]): cats.data.Validated.Invalid[cats.data.NonEmptyChain[org.make.core.ValidationError]]((errors @ _)) => ValidationDirectives.this.failWith(org.make.core.ValidationFailedError.apply(errors)) case (a: T): cats.data.Validated.Valid[T](_) => inner }))
31 35575 1103 - 1128 Apply org.make.core.Validator.validate org.make.api.technical.directives.validationdirectivestest validator.validate(value)
32 44772 1169 - 1208 Apply akka.http.scaladsl.server.directives.RouteDirectives.failWith org.make.api.technical.directives.validationdirectivestest ValidationDirectives.this.failWith(org.make.core.ValidationFailedError.apply(errors))
32 31730 1178 - 1207 Apply org.make.core.ValidationFailedError.apply org.make.api.technical.directives.validationdirectivestest org.make.core.ValidationFailedError.apply(errors)