{"id":16099303,"url":"https://github.com/viartemev/ktor-validation-feature","last_synced_at":"2025-10-05T12:58:13.441Z","repository":{"id":82550297,"uuid":"229941978","full_name":"viartemev/ktor-validation-feature","owner":"viartemev","description":"Ktor validation feature","archived":false,"fork":false,"pushed_at":"2019-12-25T12:37:29.000Z","size":71,"stargazers_count":5,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-02T13:21:42.531Z","etag":null,"topics":["kotlin","ktor","ktor-feature","ktor-framework","ktor-server","validation"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/viartemev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-12-24T12:55:51.000Z","updated_at":"2024-02-20T12:26:17.000Z","dependencies_parsed_at":null,"dependency_job_id":"78c3f1a7-f8f1-4ced-9783-adeee7a0911a","html_url":"https://github.com/viartemev/ktor-validation-feature","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/viartemev/ktor-validation-feature","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/viartemev%2Fktor-validation-feature","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/viartemev%2Fktor-validation-feature/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/viartemev%2Fktor-validation-feature/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/viartemev%2Fktor-validation-feature/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/viartemev","download_url":"https://codeload.github.com/viartemev/ktor-validation-feature/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/viartemev%2Fktor-validation-feature/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278457467,"owners_count":25989956,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-05T02:00:06.059Z","response_time":54,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["kotlin","ktor","ktor-feature","ktor-framework","ktor-server","validation"],"created_at":"2024-10-09T18:26:45.727Z","updated_at":"2025-10-05T12:58:13.414Z","avatar_url":"https://github.com/viartemev.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Ktor validation feature\n[![Build Status](https://travis-ci.com/viartemev/ktor-validation-feature.svg?branch=master)](https://travis-ci.com/viartemev/ktor-validation-feature)\n\nValidation is an important part of each API. This feature provides a simple way for incoming request validation.\n\nLet's imagine we have request object which must be validated:\n```kotlin\ndata class Message(val id: Long, val message: String)\n```\nFirst of all, we need to implement validation logic implementing `Validator` interface:\n```kotlin\nobject MessageValidator : Validator\u003cMessage\u003e {\n\n    override fun supports(clazz: KClass\u003c*\u003e): Boolean = Message::class == clazz\n\n    override fun validate(obj: Message): ValidationResult {\n        val errors = mutableMapOf\u003cPropertyPath, List\u003cValidationError\u003e\u003e()\n        if (obj.message.isBlank()) errors[\"message\"] = listOf(\"message must be not blank\")\n        return ValidationResult.Invalid(errors.toMap())\n    }\n}\n```\nThen we need to configure the feature. There are 2 options:\n1. If object validation fails (object is not valid), then we get `RequestValidationException`, which we need handle.\nOne of the options is StatusPage feature.\n```kotlin\ninstall(ValidationFeature) {\n    validators = listOf(MessageValidator) //add MessageValidator to feature\n}\n\n//StatusPages feature is optional, otherwise you should care about exception handling by yourself\ninstall(StatusPages) {\n    exception\u003cRequestValidationException\u003e { cause -\u003e \n        call.respond(HttpStatusCode.BadRequest, cause.validationResult) \n    }\n}\n\n// Some logic is here\npost(\"/messages\") {\n    val message = call.receive\u003cMessage\u003e()\n    call.respond(message)\n}\n```\n2. If you don't want to throw the exception, you should set `throwExceptionIfInvalid` to `false`. \nIn this case, you could get validation result by calling: `call.receiveValidated\u003cT\u003e()` which returns `Pair\u003cT, ValidationResult\u003e`.\n`ValidationResult` is sealed class, can be:\n    - `NotValidated` - in case validators weren't provided\n    - `Valid` - in case validation finished successfully\n    - `Invalid` - in case validation failed\n```kotlin\ninstall(ValidationFeature) {\n    validators = listOf(MessageValidator) //add MessageValidator to feature\n    throwExceptionIfInvalid = false //by default, it is true\n}\n\npost(\"/messages\") {\n    val (msg, validatedResult) = call.receiveValidated\u003cMessage\u003e()\n    when (validatedResult) {\n        is ValidationResult.NotValidated -\u003e call.respond(msg.message)\n        is ValidationResult.Valid -\u003e call.respond(msg)\n        is ValidationResult.Invalid -\u003e call.respond(HttpStatusCode.BadRequest, validatedResult.errors)    \n    }\n}\n```\n\n### Improvements\nAdd validated function for Routers\n```kotlin\npost(\"/\") = validated {\n    val (msg, validatedResult) = call.receiveValidated\u003cMessage\u003e()\n    when (validatedResult) {\n        is ValidationResult.NotValidated -\u003e call.respond(msg.message)\n        is ValidationResult.Valid -\u003e call.respond(msg)\n        is ValidationResult.Invalid -\u003e call.respond(HttpStatusCode.BadRequest, validatedResult.errors)\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fviartemev%2Fktor-validation-feature","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fviartemev%2Fktor-validation-feature","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fviartemev%2Fktor-validation-feature/lists"}