mirror of https://github.com/lukechilds/ow.git
Sam Verschueren
7 years ago
committed by
Sindre Sorhus
3 changed files with 40 additions and 0 deletions
@ -0,0 +1,22 @@ |
|||
import { Predicate, Validator, validatorSymbol } from '../predicates/predicate'; |
|||
|
|||
/** |
|||
* Operator which inverts all the validations. |
|||
* |
|||
* @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
|
|||
const fn = validator.validator; |
|||
const message = validator.message; |
|||
|
|||
validator.message = (x: any) => `[NOT] ${message(x)}`; |
|||
validator.validator = (x: any) => !fn(x); |
|||
|
|||
predicate[validatorSymbol].push(validator); |
|||
|
|||
return predicate; |
|||
}; |
|||
|
|||
return predicate; |
|||
}; |
@ -0,0 +1,9 @@ |
|||
import test from 'ava'; |
|||
import * as m from '..'; |
|||
|
|||
test('not', t => { |
|||
t.notThrows(() => m(1, m.number.not.infinite)); |
|||
t.notThrows(() => m(1, m.number.not.infinite.greaterThan(5))); |
|||
t.notThrows(() => m('foo!', m.string.not.alphanumeric)); |
|||
t.throws(() => m('', m.string.not.empty), '[NOT] Expected string to be empty, got ``'); |
|||
}); |
Loading…
Reference in new issue