feat: validator for patch options

This commit is contained in:
Sculas 2022-08-02 22:00:10 +02:00
parent 2431785d0e
commit 4e2e772389
No known key found for this signature in database
GPG key ID: 1530BFF96D1EEB89

View file

@ -55,9 +55,16 @@ sealed class PatchOption<T>(
default: T?, default: T?,
val title: String, val title: String,
val description: String, val description: String,
val required: Boolean val required: Boolean,
val validator: (T?) -> Boolean
) { ) {
var value: T? = default var value: T? = default
set(value) {
if (!validator(value)) {
throw IllegalArgumentException("Illegal value: $value")
}
field = value
}
/** /**
* A [PatchOption] representing a [String]. * A [PatchOption] representing a [String].
@ -68,9 +75,10 @@ sealed class PatchOption<T>(
default: String?, default: String?,
title: String, title: String,
description: String, description: String,
required: Boolean = false required: Boolean = false,
validator: (String?) -> Boolean = { true }
) : PatchOption<String>( ) : PatchOption<String>(
key, default, title, description, required key, default, title, description, required, validator
) )
/** /**
@ -82,9 +90,10 @@ sealed class PatchOption<T>(
default: Boolean?, default: Boolean?,
title: String, title: String,
description: String, description: String,
required: Boolean = false required: Boolean = false,
validator: (Boolean?) -> Boolean = { true }
) : PatchOption<Boolean>( ) : PatchOption<Boolean>(
key, default, title, description, required key, default, title, description, required, validator
) )
/** /**
@ -98,9 +107,12 @@ sealed class PatchOption<T>(
val options: Iterable<E>, val options: Iterable<E>,
title: String, title: String,
description: String, description: String,
required: Boolean = false required: Boolean = false,
validator: (E?) -> Boolean = { true }
) : PatchOption<E>( ) : PatchOption<E>(
key, default, title, description, required key, default, title, description, required, {
(it?.let { it in options } ?: true) && validator(it)
}
) { ) {
init { init {
if (default !in options) { if (default !in options) {
@ -119,9 +131,10 @@ sealed class PatchOption<T>(
options: Iterable<String>, options: Iterable<String>,
title: String, title: String,
description: String, description: String,
required: Boolean = false required: Boolean = false,
validator: (String?) -> Boolean = { true }
) : ListOption<String>( ) : ListOption<String>(
key, default, options, title, description, required key, default, options, title, description, required, validator
) )
/** /**
@ -134,8 +147,9 @@ sealed class PatchOption<T>(
options: Iterable<Int>, options: Iterable<Int>,
title: String, title: String,
description: String, description: String,
required: Boolean = false required: Boolean = false,
validator: (Int?) -> Boolean = { true }
) : ListOption<Int>( ) : ListOption<Int>(
key, default, options, title, description, required key, default, options, title, description, required, validator
) )
} }