Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added function validation #56

Closed
wants to merge 6 commits into from

Conversation

jusexton
Copy link

@jusexton jusexton commented Nov 28, 2020

Fixes #50

Proposed Changes

Allow function calls to be validated like property values.

New Abstractions

Existing code is coupled to the idea of only validating property values obtained from KProperty1. A new abstract class ValueValidator was added to decouple the Validator from where the value is obtained. The ValueValidator does everything the Property class does except implementors of the class must define how a value is extracted from the given instance.

Two implementations of the class were added:

  • Property which is responsible for extracting properties to be validated
  • Function which is responsible for invoking methods validating their return value

Also, additional DSL factory methods were added to the validator context to allow the creation of the new Function ValueValidator

Missing Functionality

Currently functions can not be validated with given arguments. This seems like it could be addressed but for some reason I was not able to get it working.

Minimal Testing

Very little testing was done. If this approach is appreciated, I don't mind adding additional test cases.

Examples

data class Employee(val email: String? = null) {
    init {
        validate(this) {
            validate(Employee::hasEmail).isTrue()
            validate(Employee::email).isEmail()
        }
    }

    fun hasEmail() = email != null
}

… from an object instance. This decouples the engine from only validating property values.

Using this new abstraction, two new ValueValidators were added. The Property ValueValidator and the brand new Function ValueValidator. Added additional factory functions for creating the Function ValueValidator.
Transitioned all extension DSL functions to make use of this abstraction instead of being coupled to Property values.
@jusexton jusexton closed this Jun 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Validator for value returned from a function
1 participant