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

Follow the 'required' argument for nested validations #2382

Merged
merged 6 commits into from Jun 24, 2020
Merged

Follow the 'required' argument for nested validations #2382

merged 6 commits into from Jun 24, 2020

Conversation

seeppp
Copy link
Contributor

@seeppp seeppp commented Jun 3, 2020

Validations now follows required argument while validating nested validations.

If we add a validation step of a nested object, which is optional, the required flag would be set false.

validations.add("optionalNestedObject", required: false, B.validations)

Therefore, we are now able to have an object like this:

struct A: Codable {
    var name: String
    var optionalNestedObject: A?
}

extension A: Validatable {
    static func validations(_ validations: inout Validations) {
        validations.add("name", as: String.self, is: .ascii)
        validations.add("optionalNestedObject", required: false, A.validations)
    }
}

The validator now validates the nested object only if optionalNestedObject != nil.

If the nested object is non-optional, you can still omit the required argument.

validations.add("nestedObject", B.validations)

Copy link
Member

@Joannis Joannis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! I'm sorry that we didn't get to review your PR yet, but it looks great.

@Joannis Joannis added the semver-patch Internal changes only label Jun 22, 2020
@Joannis
Copy link
Member

Joannis commented Jun 22, 2020

@seeppp before merging this we'll need to update the message of the main post of this PR. I gave our release bot a semver-patch, meaning that a patch version release is created once this PR is merged. This will carry over the release notes from the main issue. Therefore, the format of the main message should change to reflect the changes/fixes, rather than a description of the problem this solves.

If you need an example, check out this (closed & merged) PR by Tanner: #2400

Let me know if you need help, by mentioning me here or on Discord.

@MrLotU MrLotU merged commit de17edc into vapor:master Jun 24, 2020
@tanner0101
Copy link
Member

These changes are now available in 4.10.3

@tanner0101 tanner0101 added this to Awaiting Review in Vapor 4 via automation Jul 14, 2020
@tanner0101 tanner0101 added the enhancement New feature or request label Jul 14, 2020
@tanner0101 tanner0101 moved this from Awaiting Review to Done in Vapor 4 Jul 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request semver-patch Internal changes only
Projects
Vapor 4
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

4 participants