Is it possible to pass variables to a enum?
I have a class that validates strings using a "ValidationRequirements" data class and returns an Enum error message if its invalid.
Depending on what kind of requirements are passed the enum should adjust the string to match it.
ValidationRequirements:
data class ValidationRequirements(
//--------CONFIG_REQUIREMENTS--------
val PREFIX_STRING: String,
val REQUIRED_LENGTH: Int,
val MAXIMUM_LENGTH: Int,
val REQUIRE_SPECIAL_CHARACTERS: Boolean,
val REQUIRE_DIGITS: Boolean,
val REQUIRE_LOWER_CASE: Boolean,
val REQUIRE_UPPER_CASE: Boolean,
val REQUIRE_SPACE_SEPARATION: Boolean
)
Enum:
enum class StringStrengthReport(private val errorMessage: String) {
INVALID_EMAIL_FORMAT("Invalid email formatn"),
TO_SHORT(" requires at least 6 charactersn"),
LOWER_CASE_REQUIRED(" requires at least one lower case charactern"),
UPPER_CASE_REQUIRED(" requires at least one upper case charactern"),
DIGIT_REQUIRED(" requires at least one numeric charactern"),
SPECIAL_CHARACTER_REQUIRED(" requires at least one special character (i.e !&?#%)n"),
MAX_LENGTH_EXCEEDED(" max length is 32 charactersn"),
NULL_STRING(TO_SHORT.errorMessage),
VALID(" is valid!n");
}
Is it possible to pass the requirements to change min/max length for different kind of strings?
add a comment |
I have a class that validates strings using a "ValidationRequirements" data class and returns an Enum error message if its invalid.
Depending on what kind of requirements are passed the enum should adjust the string to match it.
ValidationRequirements:
data class ValidationRequirements(
//--------CONFIG_REQUIREMENTS--------
val PREFIX_STRING: String,
val REQUIRED_LENGTH: Int,
val MAXIMUM_LENGTH: Int,
val REQUIRE_SPECIAL_CHARACTERS: Boolean,
val REQUIRE_DIGITS: Boolean,
val REQUIRE_LOWER_CASE: Boolean,
val REQUIRE_UPPER_CASE: Boolean,
val REQUIRE_SPACE_SEPARATION: Boolean
)
Enum:
enum class StringStrengthReport(private val errorMessage: String) {
INVALID_EMAIL_FORMAT("Invalid email formatn"),
TO_SHORT(" requires at least 6 charactersn"),
LOWER_CASE_REQUIRED(" requires at least one lower case charactern"),
UPPER_CASE_REQUIRED(" requires at least one upper case charactern"),
DIGIT_REQUIRED(" requires at least one numeric charactern"),
SPECIAL_CHARACTER_REQUIRED(" requires at least one special character (i.e !&?#%)n"),
MAX_LENGTH_EXCEEDED(" max length is 32 charactersn"),
NULL_STRING(TO_SHORT.errorMessage),
VALID(" is valid!n");
}
Is it possible to pass the requirements to change min/max length for different kind of strings?
add a comment |
I have a class that validates strings using a "ValidationRequirements" data class and returns an Enum error message if its invalid.
Depending on what kind of requirements are passed the enum should adjust the string to match it.
ValidationRequirements:
data class ValidationRequirements(
//--------CONFIG_REQUIREMENTS--------
val PREFIX_STRING: String,
val REQUIRED_LENGTH: Int,
val MAXIMUM_LENGTH: Int,
val REQUIRE_SPECIAL_CHARACTERS: Boolean,
val REQUIRE_DIGITS: Boolean,
val REQUIRE_LOWER_CASE: Boolean,
val REQUIRE_UPPER_CASE: Boolean,
val REQUIRE_SPACE_SEPARATION: Boolean
)
Enum:
enum class StringStrengthReport(private val errorMessage: String) {
INVALID_EMAIL_FORMAT("Invalid email formatn"),
TO_SHORT(" requires at least 6 charactersn"),
LOWER_CASE_REQUIRED(" requires at least one lower case charactern"),
UPPER_CASE_REQUIRED(" requires at least one upper case charactern"),
DIGIT_REQUIRED(" requires at least one numeric charactern"),
SPECIAL_CHARACTER_REQUIRED(" requires at least one special character (i.e !&?#%)n"),
MAX_LENGTH_EXCEEDED(" max length is 32 charactersn"),
NULL_STRING(TO_SHORT.errorMessage),
VALID(" is valid!n");
}
Is it possible to pass the requirements to change min/max length for different kind of strings?
I have a class that validates strings using a "ValidationRequirements" data class and returns an Enum error message if its invalid.
Depending on what kind of requirements are passed the enum should adjust the string to match it.
ValidationRequirements:
data class ValidationRequirements(
//--------CONFIG_REQUIREMENTS--------
val PREFIX_STRING: String,
val REQUIRED_LENGTH: Int,
val MAXIMUM_LENGTH: Int,
val REQUIRE_SPECIAL_CHARACTERS: Boolean,
val REQUIRE_DIGITS: Boolean,
val REQUIRE_LOWER_CASE: Boolean,
val REQUIRE_UPPER_CASE: Boolean,
val REQUIRE_SPACE_SEPARATION: Boolean
)
Enum:
enum class StringStrengthReport(private val errorMessage: String) {
INVALID_EMAIL_FORMAT("Invalid email formatn"),
TO_SHORT(" requires at least 6 charactersn"),
LOWER_CASE_REQUIRED(" requires at least one lower case charactern"),
UPPER_CASE_REQUIRED(" requires at least one upper case charactern"),
DIGIT_REQUIRED(" requires at least one numeric charactern"),
SPECIAL_CHARACTER_REQUIRED(" requires at least one special character (i.e !&?#%)n"),
MAX_LENGTH_EXCEEDED(" max length is 32 charactersn"),
NULL_STRING(TO_SHORT.errorMessage),
VALID(" is valid!n");
}
Is it possible to pass the requirements to change min/max length for different kind of strings?
asked Nov 22 '18 at 15:47
Joel BroströmJoel Broström
174110
174110
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Use sealed class instead:
sealed class StringStrengthReport(private val message: String) {
override fun toString() = message
}
class InvalidEmailFormat(): StringStrengthReport("Invalid email format")
class TooShort(val minLength: Int): StringStrengthReport("Requires at least $minLength characters")
Results in:
println(InvalidEmailFormat()) // Invalid email format
println(TooShort(7)) // Requires at least 7 characters
add a comment |
If you need to pass variables to enum, you actually need data class, not the enum, from my point of view.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434435%2fis-it-possible-to-pass-variables-to-a-enum%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use sealed class instead:
sealed class StringStrengthReport(private val message: String) {
override fun toString() = message
}
class InvalidEmailFormat(): StringStrengthReport("Invalid email format")
class TooShort(val minLength: Int): StringStrengthReport("Requires at least $minLength characters")
Results in:
println(InvalidEmailFormat()) // Invalid email format
println(TooShort(7)) // Requires at least 7 characters
add a comment |
Use sealed class instead:
sealed class StringStrengthReport(private val message: String) {
override fun toString() = message
}
class InvalidEmailFormat(): StringStrengthReport("Invalid email format")
class TooShort(val minLength: Int): StringStrengthReport("Requires at least $minLength characters")
Results in:
println(InvalidEmailFormat()) // Invalid email format
println(TooShort(7)) // Requires at least 7 characters
add a comment |
Use sealed class instead:
sealed class StringStrengthReport(private val message: String) {
override fun toString() = message
}
class InvalidEmailFormat(): StringStrengthReport("Invalid email format")
class TooShort(val minLength: Int): StringStrengthReport("Requires at least $minLength characters")
Results in:
println(InvalidEmailFormat()) // Invalid email format
println(TooShort(7)) // Requires at least 7 characters
Use sealed class instead:
sealed class StringStrengthReport(private val message: String) {
override fun toString() = message
}
class InvalidEmailFormat(): StringStrengthReport("Invalid email format")
class TooShort(val minLength: Int): StringStrengthReport("Requires at least $minLength characters")
Results in:
println(InvalidEmailFormat()) // Invalid email format
println(TooShort(7)) // Requires at least 7 characters
answered Nov 22 '18 at 16:49
Alexey SoshinAlexey Soshin
6,8051512
6,8051512
add a comment |
add a comment |
If you need to pass variables to enum, you actually need data class, not the enum, from my point of view.
add a comment |
If you need to pass variables to enum, you actually need data class, not the enum, from my point of view.
add a comment |
If you need to pass variables to enum, you actually need data class, not the enum, from my point of view.
If you need to pass variables to enum, you actually need data class, not the enum, from my point of view.
answered Nov 22 '18 at 17:21
samaromkusamaromku
624
624
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434435%2fis-it-possible-to-pass-variables-to-a-enum%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown