File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ import { GreaterThenValidator } from "./validators/operators/gt.ts";
2323import { GreaterThenOrEqualValidator } from "./validators/operators/gte.ts" ;
2424import { InequalityValidator } from "./validators/operators/inequality.ts" ;
2525import { InstanceValidator } from "./validators/operators/instanceof.ts" ;
26+ import { NotValidator } from "./validators/operators/not.ts" ;
2627import { OrValidator } from "./validators/operators/or.ts" ;
2728import { TypeValidator } from "./validators/operators/typeof.ts" ;
2829import { ValidDateValidator } from "./validators/date/valid_date.ts" ;
@@ -42,6 +43,7 @@ export const lte = /* @__PURE__ */ lazy(LessThenOrEqualValidator);
4243export const gt = /* @__PURE__ */ lazy ( GreaterThenValidator ) ;
4344export const gte = /* @__PURE__ */ lazy ( GreaterThenOrEqualValidator ) ;
4445export const ne = /* @__PURE__ */ lazy ( InequalityValidator ) ;
46+ export const not = /* @__PURE__ */ lazy ( NotValidator ) ;
4547export const or = /* @__PURE__ */ lazy ( OrValidator ) ;
4648export const and = /* @__PURE__ */ lazy ( AndValidator ) ;
4749
Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ export {
2020 minCount ,
2121 ne ,
2222 nonEmpty ,
23+ not ,
2324 nullish ,
2425 number ,
2526 object ,
Original file line number Diff line number Diff line change 1+ // Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
2+ // This module is browser compatible.
3+
4+ import { shouldBeBut } from "../utils.ts" ;
5+ import { Reporter , ValidationFailure , Validator } from "../../types.ts" ;
6+
7+ export interface ReportContext < In = unknown > {
8+ input : In ;
9+ }
10+
11+ export class NotValidator < in In = unknown , In_ extends In = In >
12+ extends Reporter < ReportContext < In > >
13+ implements Validator < In , In_ > {
14+ validator : Validator < In , In_ > ;
15+
16+ constructor ( validator : Validator < In , In_ > ) {
17+ super ( ) ;
18+ super . expect ( shouldBeBut ) ;
19+ this . validator = validator ;
20+ }
21+
22+ is ( input : In ) : input is In_ {
23+ return ! this . validator . is ( input ) ;
24+ }
25+
26+ * validate ( input : In ) : Iterable < ValidationFailure > {
27+ if ( ! this . is ( input ) ) yield new ValidationFailure ( this . report ( { input } ) ) ;
28+ }
29+
30+ override toString ( ) : string {
31+ return `not ${ this . validator } ` ;
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments