diff --git a/readme.md b/readme.md index 21ad493..01a81ae 100644 --- a/readme.md +++ b/readme.md @@ -132,7 +132,7 @@ The following predicates are available on every type. #### not -Inverts the following predicates. +Inverts the following predicate. ```ts ow(1, ow.number.not.infinite); diff --git a/source/lib/operators/not.ts b/source/lib/operators/not.ts index 1513ddb..2c9f79a 100644 --- a/source/lib/operators/not.ts +++ b/source/lib/operators/not.ts @@ -1,12 +1,14 @@ import {Predicate, validatorSymbol} from '../predicates/predicate'; /** - * Operator which inverts all the validations. + * Operator which inverts the following validation. * * @hidden * @param predictate Predicate to wrap inside the operator. */ export const not = >(predicate: P) => { + const originalAddValidator = predicate.addValidator; + predicate.addValidator = validator => { const fn = validator.validator; const message = validator.message; @@ -16,6 +18,8 @@ export const not = >(predicate: P) => { predicate[validatorSymbol].push(validator); + predicate.addValidator = originalAddValidator; + return predicate; }; diff --git a/source/test/test.ts b/source/test/test.ts index a742b4d..fa3844c 100644 --- a/source/test/test.ts +++ b/source/test/test.ts @@ -2,12 +2,13 @@ import test from 'ava'; import m from '..'; test('not', t => { + t.notThrows(() => m('foo!', m.string.not.alphanumeric)); t.notThrows(() => m(1, m.number.not.infinite)); - t.notThrows(() => m(1, m.number.not.infinite.greaterThan(5))); + t.notThrows(() => m(1, m.number.not.infinite.not.greaterThan(5))); + t.throws(() => m(6, m.number.not.infinite.not.greaterThan(5))); t.notThrows(() => m('foo!', m.string.not.alphabetical)); t.notThrows(() => m('foo!', m.string.not.alphanumeric)); t.notThrows(() => m('foo!', m.string.label('foo').not.alphanumeric)); - t.notThrows(() => m('foo!', m.string.not.label('foo').alphanumeric)); t.notThrows(() => m('foo!', m.string.not.alphanumeric.label('foo'))); t.notThrows(() => m('FOO!', m.string.not.lowercase)); t.notThrows(() => m('foo!', m.string.not.uppercase));