-
-
Notifications
You must be signed in to change notification settings - Fork 106
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
Add not
predicate
#12
Conversation
We could give it a different name, like
Not a big fan of that. It tries to make it too "English". And it makes it positional. The point is that each property is a modifier of everything. The position doesn't matter. ow(10, m.number.not.infinite.greaterThan(5)); is just meant as a shortcut for ow(10, m.number.notType(ow.infinite.greaterThan(5))); |
Any preference?
Makes sense! Regarding the ow(10, ow.number.notType(ow.number.infinite.greaterThan(5))); |
I also refactored the code and used a Proxy now. I believe this is a more elegant way of solving the problem :). Feel free if you think the previous implementation is more straightforward. It was just nice to experiment with Proxy :p. One thing I don't handle currently, is negating the error message. ow(1, ow.number.not.lessThan(5))
//=> Expected 1 to be less than 5 Unless we specify a negated error message ourselves, I can only think of one way of solving this. By prefixing the error message with
|
I thought about this again, maybe I like |
Proxy is slightly an overkill here, but it does make the inverted thing less coupled. I could go either way, so let's go with Proxy since you prefer it. I'm surprised the test pass on Node.js 4 though? It doesn't support Proxy. Does TS try to polyfill proxies or something?
Let's just do this for now. We can improve the error messages later on. |
Like I said, if you find the first implementation more straightforward, happy to revert it. It was just nice to experiment with Proxy :). You're right, I don't think it works on Node.js 4. If that is a prerequisite, we should probably have to revert the Proxy thing. |
Then I think we should revert. It would be nice to be able to use this in some libs that have to stay Node.js 4 compatible, like AVA and XO. |
Let's defer this to later. It's not an important feature. |
I agree! I will have a look to make sure it keeps working on Node 4 👍 . |
I refactored the |
* @param predictate Predicate to wrap inside the operator. | ||
*/ | ||
export const not = <T extends Predicate>(predicate: T) => { | ||
predicate['addValidator'] = (validator: Validator<any>) => { // tslint:disable-line:no-string-literal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because addValidator
is marked protected
, I had to use the predicate['addValidator']
workaround.
Another thing we can do, which I believe is better, is by not marking addValidator
with protected
but with an @internal
comment. This way, it doesn't end up in the type definitions and we don't have to use this workaround.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another thing we can do, which I believe is better, is by not marking addValidator with protected but with an @internal comment. This way, it doesn't end up in the type definitions and we don't have to use this workaround
Sounds good!
Looking good now :) |
Looking into negating predicates. This PR adds the following syntax.
I noticed 2 suggestion in the validator ideas.
not
(Inverts the following predicates)not(type)
(Inverts just the argument) (Example:ow.string.not(ow.empty)
)This is not possible to implement. We can not have
get not()
together withnot(predicate: Predicate)
in one class.Also for the
not(type)
example, it would look likeow.string.not(ow.string.empty)
. It's perfectly possible to restrict the argument to only being aStringPredicate
orNumberPredicate
depending on the chain, but.empty
lives inside theStringPredicate
so has to be accessed viaow.string
. Something likeow.string.not(x => x.empty)
wherex
is of typeStringPredicate
would be possible.However, we might reconsider the current implementation. Maybe we should just let it negate the first following predicate.
To me, this reads like
Just throwing some ideas here though. Feel free to reject this for now. Going to think about this a little bit more as well. Not entirely keen on the way it's implemented.