This commit is contained in:
48
docs/snippets/swift/plugins/min_length_validator.md
Normal file
48
docs/snippets/swift/plugins/min_length_validator.md
Normal file
@@ -0,0 +1,48 @@
|
||||
```swift title="Swift"
|
||||
import Kreuzberg
|
||||
|
||||
final class MinLengthValidator: Validator {
|
||||
let minLength: Int
|
||||
|
||||
init(minLength: Int = 100) {
|
||||
self.minLength = minLength
|
||||
}
|
||||
|
||||
func name() -> String {
|
||||
"min_length_validator"
|
||||
}
|
||||
|
||||
func version() -> String {
|
||||
"1.0.0"
|
||||
}
|
||||
|
||||
func priority() -> Int32 {
|
||||
100
|
||||
}
|
||||
|
||||
func validate(result: ExtractionResult, config: ExtractionConfig) -> String {
|
||||
// Returns JSON-encoded Result<(), String>
|
||||
let contentLength = result.content().count
|
||||
if contentLength < minLength {
|
||||
let message = "Content too short: \(contentLength) < \(minLength)"
|
||||
return "{\"err\": \"\(message)\"}"
|
||||
}
|
||||
return "{\"ok\": null}"
|
||||
}
|
||||
|
||||
func shouldValidate(result: ExtractionResult, config: ExtractionConfig) -> Bool {
|
||||
true
|
||||
}
|
||||
|
||||
func initialize() -> String {
|
||||
"{\"ok\": null}"
|
||||
}
|
||||
|
||||
func shutdown() -> String {
|
||||
"{\"ok\": null}"
|
||||
}
|
||||
}
|
||||
|
||||
let validator = MinLengthValidator(minLength: 100)
|
||||
try Kreuzberg.registerValidator(validator)
|
||||
```
|
||||
Reference in New Issue
Block a user