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

Adds OptionalValidator #75

Merged
1 commit merged into from
Apr 25, 2018
Merged

Adds OptionalValidator #75

1 commit merged into from
Apr 25, 2018

Conversation

ghost
Copy link

@ghost ghost commented Apr 10, 2018

Summary

Closes #62 Optional validator by adding OptionalValidator.

Details

The OptionalValidator has two stages of conditions. First the primaryCondition is checked. If it is met, then the subsequent condition are checked for violations. If the primaryCondition is not met, then no violation is returned.

As an example, say you only want violations to show if there is text in the field, but otherwise ignore it as it is an optional field in a form. In this case you would use a PresentCondition as the primaryCondition and your subsequent validation requirements as the conditions.

Resolves #62 `Optional validator`
@ghost ghost merged commit 9425181 into master Apr 25, 2018
@ghost ghost deleted the optional-validator branch April 25, 2018 06:22
Copy link

@madhikarma madhikarma left a comment

Choose a reason for hiding this comment

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

LGTM, just a question

conditions = [PresentCondition()]
}

public init(conditions: [Condition], primaryCondition: Condition = PresentCondition()) {

Choose a reason for hiding this comment

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

Is there a use case for this one? i.e. a PresentCondition being primary followed by a single PrimaryCondition in the conditions array


public func checkConditions(_ text: String?) -> [Condition]? {
if primaryCondition.check(text) {
let violations = conditions.filter { !($0.check(text)) }

Choose a reason for hiding this comment

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

This is fine, I'm thinking if there are other ways to do this e.g. using compactMap but don't want it to become too terse or hard to reason with / understand either.

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant