-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow ValidationPath in validate() and ValidationPath.of (#169)
- Loading branch information
1 parent
2310894
commit 6ec40a4
Showing
9 changed files
with
207 additions
and
92 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
package io.konform.validation | ||
|
||
import io.konform.validation.constraints.const | ||
import io.konform.validation.constraints.containsPattern | ||
import io.konform.validation.constraints.enum | ||
import io.konform.validation.constraints.maxLength | ||
import io.konform.validation.constraints.minItems | ||
import io.konform.validation.constraints.minLength | ||
|
@@ -206,82 +204,6 @@ class ValidationBuilderTest { | |
) | ||
} | ||
|
||
@Test | ||
fun validateLambda() { | ||
val splitDoubleValidation = | ||
Validation<Register> { | ||
validate("getPasswordLambda", { r: Register -> r.password }) { | ||
minLength(1) | ||
maxLength(10) | ||
} | ||
validate("getEmailLambda", { r: Register -> r.email }) { | ||
pattern(".+@.+".toRegex()).hint("must have correct format") | ||
} | ||
} | ||
|
||
splitDoubleValidation shouldBeValid Register(email = "[email protected]", password = "a") | ||
splitDoubleValidation.shouldBeInvalid(Register(email = "[email protected]", password = "")) { | ||
it.shouldContainOnlyError(ValidationError.of("getPasswordLambda", "must have at least 1 characters")) | ||
} | ||
splitDoubleValidation.shouldBeInvalid(Register(email = "[email protected]", password = "aaaaaaaaaaa")) { | ||
it.shouldContainOnlyError(ValidationError.of("getPasswordLambda", "must have at most 10 characters")) | ||
} | ||
splitDoubleValidation.shouldBeInvalid(Register(email = "tester@", password = "")) { | ||
it.shouldContainExactlyErrors( | ||
ValidationError.of("getPasswordLambda", "must have at least 1 characters"), | ||
ValidationError.of("getEmailLambda", "must have correct format"), | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun complexLambdaAccessors() { | ||
data class Token( | ||
val claims: Map<String, String>, | ||
) | ||
|
||
fun ValidationBuilder<Token>.validateClaim( | ||
key: String, | ||
validations: ValidationBuilder<String>.() -> Unit, | ||
) { | ||
required("claim_$key", { data: Token -> data.claims[key] }) { | ||
validations() | ||
} | ||
} | ||
|
||
val accessTokenValidation = | ||
Validation<Token> { | ||
validateClaim("scope") { | ||
const("access") | ||
} | ||
validateClaim("issuer") { | ||
enum("bob", "eve") | ||
} | ||
} | ||
val refreshTokenVerification = | ||
Validation<Token> { | ||
validateClaim("scope") { | ||
const("refresh") | ||
} | ||
validateClaim("issuer") { | ||
enum("bob", "eve") | ||
} | ||
} | ||
|
||
Token(mapOf("scope" to "access", "issuer" to "bob")).let { | ||
assertEquals(Valid(it), accessTokenValidation(it)) | ||
assertEquals(1, countFieldsWithErrors(refreshTokenVerification(it))) | ||
} | ||
Token(mapOf("scope" to "refresh", "issuer" to "eve")).let { | ||
assertEquals(Valid(it), refreshTokenVerification(it)) | ||
assertEquals(1, countFieldsWithErrors(accessTokenValidation(it))) | ||
} | ||
Token(mapOf("issuer" to "alice")).let { | ||
assertEquals(2, countFieldsWithErrors(accessTokenValidation(it))) | ||
assertEquals(2, countFieldsWithErrors(refreshTokenVerification(it))) | ||
} | ||
} | ||
|
||
@Test | ||
fun validateLists() { | ||
data class Data( | ||
|
Oops, something went wrong.