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

Question: What's the recommended approach for nested object validations? #42

Open
ib-jamesp opened this issue May 20, 2020 · 2 comments

Comments

@ib-jamesp
Copy link

ib-jamesp commented May 20, 2020

I have a reasonably complex object graph, and want to be able to split up the validation of these objects into smaller unit testable chunks, yet retain the nested property structure provided when you validate inline such as the example:

try {
    validate(employee) {
        validate(Employee::company).validate {
            validate(Company::city).validate {
                validate(City::name).isNotEmpty()
            }
        }
    }
} catch (ex: ConstraintViolationException) {
    ex.constraintViolations
        .map { "${it.property}: ${it.constraint.name}" }
        .forEach(::println)
}

Is it possible to have the company.city validation in another class, yet called inline?

try {
    validate(employee) {
         //something like...
            companyValidator.doValidation(employee.company)
        
        }
    }
} catch (ex: ConstraintViolationException) {
    ex.constraintViolations
        .map { "${it.property}: ${it.constraint.name}" }
        .forEach(::println)
}
@ib-jamesp
Copy link
Author

I have sort of answered my own question, and it is possible... but is it something you recommend?

 validate(Employee::dependents).validateForEach {
                    dependentValidator.doValidation(it)
                }

@Areks
Copy link

Areks commented Apr 27, 2021

Hi, I agree, each class should know how to validate himself and if it's validation haven't got any complex condition depending on sibling objects call it internal validation rules look like pretty good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants