-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #256 from HubSpot/normalize_data_before_validation
Normalize Slack form element presentational data
- Loading branch information
Showing
16 changed files
with
481 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
slack-base/src/main/java/com/hubspot/slack/client/models/dialog/form/elements/HasLabel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.hubspot.slack.client.models.dialog.form.elements; | ||
|
||
public interface HasLabel { | ||
String getLabel(); | ||
} |
7 changes: 7 additions & 0 deletions
7
...k-base/src/main/java/com/hubspot/slack/client/models/dialog/form/elements/HasOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.hubspot.slack.client.models.dialog.form.elements; | ||
|
||
import java.util.List; | ||
|
||
public interface HasOptions { | ||
List<SlackFormOption> getOptions(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
.../hubspot/slack/client/models/dialog/form/elements/SlackDialogFormElementLengthLimits.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.hubspot.slack.client.models.dialog.form.elements; | ||
|
||
public enum SlackDialogFormElementLengthLimits { | ||
MAX_LABEL_LENGTH(24), | ||
MAX_NAME_LENGTH(300), | ||
MAX_PLACEHOLDER_LENGTH(150), | ||
MAX_HINT_LENGTH(150), | ||
MAX_TEXT_ELEMENT_VALUE_LENGTH(150), | ||
MAX_TEXT_AREA_ELEMENT_VALUE_LENGTH(3000), | ||
MAX_OPTION_LABEL_LENGTH(75), | ||
MAX_OPTION_VALUE_LENGTH(75), | ||
MAX_OPTIONS_NUMBER(100), | ||
MAX_OPTION_GROUPS_NUMBER(100); | ||
|
||
private final int limit; | ||
|
||
SlackDialogFormElementLengthLimits(int limit) { | ||
this.limit = limit; | ||
} | ||
|
||
public int getLimit() { | ||
return limit; | ||
} | ||
} |
29 changes: 16 additions & 13 deletions
29
...ain/java/com/hubspot/slack/client/models/dialog/form/elements/SlackFormOptionGroupIF.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,40 @@ | ||
package com.hubspot.slack.client.models.dialog.form.elements; | ||
|
||
import java.util.List; | ||
|
||
import org.immutables.value.Value.Check; | ||
import org.immutables.value.Value.Immutable; | ||
|
||
import com.fasterxml.jackson.databind.PropertyNamingStrategy.SnakeCaseStrategy; | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
import com.google.common.base.Strings; | ||
import com.hubspot.immutables.style.HubSpotStyle; | ||
import com.hubspot.slack.client.models.dialog.form.elements.helpers.SlackDialogElementNormalizer; | ||
|
||
@Immutable | ||
@HubSpotStyle | ||
@JsonNaming(SnakeCaseStrategy.class) | ||
public interface SlackFormOptionGroupIF { | ||
String getLabel(); | ||
List<SlackFormOption> getOptions(); | ||
|
||
public interface SlackFormOptionGroupIF extends HasLabel, HasOptions { | ||
@Check | ||
default void validate() { | ||
String label = getLabel(); | ||
int numOptionGroups = getOptions().size(); | ||
default SlackFormOptionGroupIF validate() { | ||
SlackFormOptionGroupIF normalized = SlackDialogElementNormalizer.normalize(this); | ||
String label = normalized.getLabel(); | ||
int numOptions = normalized.getOptions().size(); | ||
|
||
if (Strings.isNullOrEmpty(label)) { | ||
throw new IllegalStateException("Must provide a label"); | ||
} | ||
|
||
if (label.length() > 75) { | ||
throw new IllegalStateException("Label cannot exceed 75 chars - '" + label + "'"); | ||
int maxOptionLabelLength = SlackDialogFormElementLengthLimits.MAX_OPTION_LABEL_LENGTH.getLimit(); | ||
if (label.length() > maxOptionLabelLength) { | ||
String errorMessage = String.format("Label cannot exceed %s chars - '%s'", maxOptionLabelLength, label); | ||
throw new IllegalStateException(errorMessage); | ||
} | ||
|
||
if (numOptionGroups > 100) { | ||
throw new IllegalStateException("Cannot have more than 100 option groups. Has " + numOptionGroups); | ||
int maxOptionsNumber = SlackDialogFormElementLengthLimits.MAX_OPTIONS_NUMBER.getLimit(); | ||
if (numOptions > maxOptionsNumber) { | ||
String errorMessage = String.format("Cannot have more than %s option groups. Has %s", maxOptionsNumber, numOptions); | ||
throw new IllegalStateException(errorMessage); | ||
} | ||
|
||
return normalized; | ||
} | ||
} |
Oops, something went wrong.