Keyword "validate" suggestion #1814
Replies: 10 comments
-
What would be the advantage of your proposal compared to a combination of generics, pattern matching and maybe You could rewrite public void NotEmpty<T>(T param, string errorMsg)
{
if (param is string s && s != "")
{
s += " very good";
// do something with s here (typesafe!)
}
throw new Exception(errorMsg);
} or public void NotEmpty(dynamic param, string errorMsg)
{
if (param is string s && s != "")
param += " very good";
// do something with param here (not typesafe)
throw new Exception(errorMsg);
} |
Beta Was this translation helpful? Give feedback.
-
KEYWORD sequential Model People
Validator methods
Object
List
Primitive[]
dynamic
In this example I want to demonstrate that a method if called within the sequential it does not run on how much it does not reach its turn, this scenario would be for validations and testing in methods a native form of summarizing code and grouping a particular operation. |
Beta Was this translation helpful? Give feedback.
-
I don't get the point of it at all. In your example: public class MyValidate
{
public void NotEmpty(validate any param, string errorMsg)
{
if (param == typeof(string))
{
if (param != String.Empty)
param += " very good";
}
throw new Exception(errorMsg);
}
} There's so many flaws here. For one, checking if Also, for The biggest reason why I dislike the feature, is why not just do this: public class MyValidate
{
public void NotEmpty(string param, string errorMsg)
{
if (param != String.Empty)
param += " very good";
throw new Exception(errorMsg);
}
} Or use |
Beta Was this translation helpful? Give feedback.
-
@AustinBryan |
Beta Was this translation helpful? Give feedback.
-
Could you explain what you find insufficient in the existing mechanisms the language already has for "data validation"? |
Beta Was this translation helpful? Give feedback.
-
Current Code
My approach - Implementation example In this case, validate returns a Boolean if all the test is true so ´ was satisfied with validation if a test is false then everything else is invalid!
|
Beta Was this translation helpful? Give feedback.
-
if (e.Lenght == 0) throw new ArgumentException("E-mail address is required.");
if (e.IndexOf("@") == -1) throw new ArgumentException("Email require @");
if (e.Regex("pattern_valid_email")) throw new ArgumentException("Email invalid!");
return true; This has the virtues of:
|
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi I think that example is supposed to return false if one of the tests fails (rather than throwing exceptions) and provide the error message as an out parameter. The problem being that the method doesn't appear to have an out parameter so I can't figure out of |
Beta Was this translation helpful? Give feedback.
-
in that case, you can just do: if (e.Lenght == 0) return (false, "E-mail address is required.");
if (e.IndexOf("@") == -1) return (false, "Email require @");
if (e.Regex("pattern_valid_email")) return (false, "Email invalid!");
return (true, null); |
Beta Was this translation helpful? Give feedback.
-
It can also be done with a simple semantic type:
The This isn't theoretical code - it's written with a semantic type for validation that I wrote some time ago for my blog. So, no need for special syntax in C# - just some library design. My |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have suggestion process of new keywords for Sin data manipulation in C#
keywords:
validate
: Keyword for validations of a variable .with
: keyword to signal which class will be initialized.any
: Accepts any given type as [int, string, bool ... ] Only when together with the keyword validateExample Class of a validator
Example validator
or
Output
Exception: Maximum caracter
In this example step a string to validate with less than 8 characters but in the validation is added more then when the second method is executed that counts the number of characters it already has a number greater than 8 then causes an exception
Beta Was this translation helpful? Give feedback.
All reactions