Comments (3)
Hi, I'm glad you liked the library.
Guard.Argument
overloads expect the value and name of a single argument, and failed preconditions throw exceptions for that specific argument. The reason why () => myStuff || myOtherStuff
doesn't work is that it does not represent a single argument.
var a = 1;
var b = 2;
// The following lambda expression is a MemberExpression that represents the variable "a".
// Guard.Argument compiles that expression to get the value, 1.
Guard.Argument(() => a);
// The following lambda expression is a BinaryExpression that adds "a" and "b".
// It doesn't represent a single parameter which Guard can get the value and name of.
Guard.Argument(() => a + b);
If your method accept multiple arguments, you need to have a Guard.Argument(...)
call for each. This allows the ParamName
and Message
properties of the exceptions thrown to be unique per argument.
If you're sure that you want to threat a combination of arguments as a single argument, you can use the Guard.Argument
overload that accepts the argument value and name as separate parameters.
void Foo(bool a, bool b)
{
// Here we specify a combination of values, and a custom name
// to be included in exception messages.
Guard.Argument(a || b, "a or b").True();
}
Update: However, I would recommend this approach instead:
void Foo(bool a, bool b)
{
// Here, both a and b can be false if the other one is not.
// Since "a" is the first argument, we'll blame "b" if it's not consistent with "a".
if (!a)
Guard.Argument(() => b).True("b can only false when a is true.");
}
I hope this helps, please let me know if you have any more questions.
from guard.
ah so the 'e' in question that it's complaining about is the name of the argument? Yep that helps. The check was to make sure a particular JSON fragment had one of two options in it or not. A sanity check basically. I appreciate the help!
from guard.
Kind of.
e
is the lambda expression you specify. It's body is a MemberExpression when you write it like () => foo
. A member expression has a name we can use (foo). We can also compile the expression to get its value, so we can have both the name and value of the argument - it's just for convenience over the overload that accepts the name and value separately.
But when you write e
like () => foo || bar
, it's body becomes a BinaryExpression. Now it's the logical OR result of two different member expressions. We can still compile it and get a value but we now don't have a name, because the expression doesn't point to a single named argument.
Is the JSON fragment a Newtonsoft JObject that you accept as a parameter? Maybe you can use member guards to validate it. But I guess writing a "JSON to model" parser function and using Guard.Wrap
with it would make more sense. Though you'd have to throw exceptions manually for invalid JSON values in that function.
I hope this clarifies things.
from guard.
Related Issues (20)
- Interop NotNullWhenAttribute leaks HOT 1
- Support for both directory and file path validation HOT 2
- How to return the Exception instead of throwing it HOT 3
- Problem with "InitContainsDuplicate" on .NET < 4.7.2 HOT 1
- Multiple checks with same custom message HOT 3
- How to Guard an ICollection member HOT 1
- CA1062 warning HOT 5
- Support validation/guard-clauses for collection items HOT 1
- Guard variants for nullable types HOT 2
- how to validata the string is a valid email address? HOT 1
- NotDefault limited to struct HOT 1
- Unacceptable performance HOT 3
- Add support for 'CallerArgumentExpression' attribute HOT 9
- Confirm parameter is one of list of Types HOT 1
- Binary Backward Compatibility HOT 2
- Document inclusivity of InRange HOT 2
- Similar fluent-style guard library for javascript
- Enhanced String char based guards. HOT 2
- 'NotNull' validation should be available for unconstrained generic types HOT 2
- Status on v2 of the library HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from guard.