Skip to content

Follow the 'required' argument for nested validations

Compare
Choose a tag to compare
@tanner0101 tanner0101 released this 24 Jun 08:19
de17edc
This patch was authored by @seeppp and released by @MrLotU.

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)