File tree Expand file tree Collapse file tree 5 files changed +29
-8
lines changed Expand file tree Collapse file tree 5 files changed +29
-8
lines changed Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ type FlattenedIssue = {
14
14
// ----------
15
15
16
16
export class VError extends Error {
17
- private issues : Issue [ ] ;
17
+ private readonly issues : Issue [ ] ;
18
18
19
19
constructor ( errors : Issue [ ] ) {
20
20
const message = JSON . stringify ( errors , null , 2 ) ;
Original file line number Diff line number Diff line change 1
1
function _includes ( inputs : string [ ] , value : string | undefined ) : boolean {
2
- return value !== undefined && inputs . includes ( value ) ;
2
+ return inputs ? .includes ( value ) ;
3
3
}
4
4
5
5
function _isNumber ( input : string | undefined ) : boolean {
6
6
return ! Number . isNaN ( Number ( input ) ) ;
7
7
}
8
8
9
9
function _isNotEmpty ( input : string | undefined ) : boolean {
10
- return input !== undefined && '' !== input . trim ( ) ;
10
+ return '' !== input ? .trim ( ) ;
11
11
}
12
12
13
13
function _isOptional ( ) : boolean {
14
14
return true ;
15
15
}
16
16
17
17
function _startsWith ( input : string | undefined , prefix : string ) : boolean {
18
- return input !== undefined && input . startsWith ( prefix ) ;
18
+ return input ? .startsWith ( prefix ) ;
19
19
}
20
20
21
21
// --------------------------------------------------------------------------------
Original file line number Diff line number Diff line change @@ -56,7 +56,7 @@ function _isMultipleURL(input: string | undefined): boolean {
56
56
57
57
function _isPostgresURL ( input : string | undefined ) : boolean {
58
58
const regex =
59
- / ^ p o s t g r e s q l : \/ \/ ( [ a - z A - Z 0 - 9 ] + ) : ( [ a - z A - Z 0 - 9 ] + ) @ ( l o c a l h o s t | [ 0 - 9 . ] + ) : ( [ 0 - 9 ] { 1 , 5 } ) \/ ( [ a - z A - Z 0 - 9 ] + ) ( \? s c h e m a = ) ? ( [ a - z A - Z 0 - 9 ] + ) / ;
59
+ / ^ p o s t g r e s q l : \/ \/ ( [ a - z A - Z 0 - 9 ] + ) : ( [ a - z A - Z 0 - 9 ] + ) @ ( l o c a l h o s t | [ 0 - 9 . ] + ) : ( \d { 1 , 5 } ) \/ ( [ a - z A - Z 0 - 9 ] + ) ( \? s c h e m a = ) ? ( [ a - z A - Z 0 - 9 ] + ) / ;
60
60
61
61
return regex . test ( input ) ;
62
62
}
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ export type SafeParseReturnType<T> =
28
28
| { success : false ; data : null ; error : VError } ;
29
29
30
30
class StringBuilder {
31
- #validators: Validator < string > [ ] = [ ] ;
31
+ readonly #validators: Validator < string > [ ] = [ ] ;
32
32
33
33
enum ( inputs : string [ ] ) : this {
34
34
this . #validators. push ( _enum ( inputs ) ) ;
@@ -147,11 +147,10 @@ export type InferShape<T extends SchemaShape> = {
147
147
export class ObjectBuilder < T extends SchemaShape > {
148
148
#errors: Issue [ ] = [ ] ;
149
149
150
- constructor ( private shape : T ) { }
150
+ constructor ( private readonly shape : T ) { }
151
151
152
152
safeParse ( value : object ) : SafeParseReturnType < InferShape < T > > {
153
153
this . #errors = Object . entries ( this . shape ) . reduce ( ( issues , [ key , validator ] ) => {
154
- // const parsed = validator.safeParse(value[key]);
155
154
const parsed = validator . safeParse ( ( value as Record < string , string > ) [ key ] ) ;
156
155
157
156
if ( ! parsed . success ) {
Original file line number Diff line number Diff line change
1
+ import { v } from '../core' ;
2
+
3
+ import * as dotenv from 'dotenv' ;
4
+ dotenv . config ( {
5
+ path : './config/test/.env'
6
+ } ) ;
7
+
8
+ test ( 'Protocol Success' , ( ) => {
9
+ const mockSchema = v . schema ( {
10
+ API_GATEWAY_PROTOCOL : v . str ( ) . protocol ( )
11
+ } ) ;
12
+
13
+ expect ( mockSchema . safeParse ( process . env ) . success ) . toBeTruthy ( ) ;
14
+ } ) ;
15
+
16
+ test ( 'Protocol Error' , ( ) => {
17
+ const mockSchema = v . schema ( {
18
+ API_GATEWAY_PROTOCOL_SECURE : v . str ( ) . protocol ( )
19
+ } ) ;
20
+
21
+ expect ( mockSchema . safeParse ( process . env ) . success ) . toBeFalsy ( ) ;
22
+ } ) ;
You can’t perform that action at this time.
0 commit comments