File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44import { TypeValidator } from "./validators/type.ts" ;
55import { InstanceValidator } from "./validators/instance.ts" ;
66import { DictionaryValidator } from "./validators/object/dictionary.ts" ;
7+ import { OptionalValidator } from "./validators/object/optional.ts" ;
78import { NullishValidator } from "./validators/nullish.ts" ;
89import { IntegerValidator } from "./validators/number/integer.ts" ;
910import { PositiveNumberValidator } from "./validators/number/positive_number.ts" ;
@@ -28,6 +29,7 @@ export const boolean = /* @__PURE__ */ new TypeValidator("boolean");
2829export const instance = /* @__PURE__ */ lazy ( InstanceValidator ) ;
2930export const type = /* @__PURE__ */ lazy ( TypeValidator ) ;
3031export const object = /* @__PURE__ */ lazy ( DictionaryValidator ) ;
32+ export const optional = /* @__PURE__ */ lazy ( OptionalValidator ) ;
3133export const nullish = /* @__PURE__ */ new NullishValidator ( ) ;
3234export const eq = /* @__PURE__ */ lazy ( EqualityValidator ) ;
3335export const lt = /* @__PURE__ */ lazy ( LessValidator ) ;
Original file line number Diff line number Diff line change 11// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
22// This module is browser compatible.
33
4- export { isUndefined } from "https://deno.land/x/isx@1.3.1/is_undefined.ts" ;
54export { isEmpty } from "https://deno.land/x/isx@1.3.1/iterable/is_empty.ts" ;
65export { isNotEmpty } from "https://deno.land/x/isx@1.3.1/iterable/is_not_empty.ts" ;
76export { isNonNullable } from "https://deno.land/x/isx@1.3.1/is_non_nullable.ts" ;
Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ export {
2020 nullish ,
2121 number ,
2222 object ,
23+ optional ,
2324 pattern ,
2425 positive ,
2526 string ,
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 { filterKeys } from "../../deps.ts" ;
5+ import { DictionaryValidator } from "./dictionary.ts" ;
6+ import {
7+ Assert ,
8+ AssertiveValidator ,
9+ Validation ,
10+ ValidationError ,
11+ } from "../../types.ts" ;
12+
13+ export class OptionalValidator <
14+ In extends Record < string , unknown > = Record < string , unknown > ,
15+ In_ extends In = In ,
16+ > implements AssertiveValidator < Partial < In > , Partial < In_ > > {
17+ declare [ Assert . symbol ] : Partial < In_ > ;
18+ constructor (
19+ public validators :
20+ & { [ k in keyof In ] : Validation < In [ k ] , In_ [ k ] > }
21+ & { [ k in keyof In_ ] : Validation < In_ [ k ] > } ,
22+ ) { }
23+
24+ * validate ( input : Partial < In > ) : Iterable < ValidationError > {
25+ const validators = filterKeys (
26+ this . validators ,
27+ hasInput ,
28+ ) as typeof this . validators ;
29+
30+ function hasInput ( key : string ) : boolean {
31+ return key in input ;
32+ }
33+
34+ yield * new DictionaryValidator < In , In_ > ( validators ) . validate ( input as In ) ;
35+ }
36+ }
You can’t perform that action at this time.
0 commit comments