Skip to content

Commit 8d1893d

Browse files
committed
Prepare testing
Signed-off-by: rockito10 <[email protected]>
1 parent 6d23060 commit 8d1893d

File tree

5 files changed

+29
-8
lines changed

5 files changed

+29
-8
lines changed

config/core/error.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type FlattenedIssue = {
1414
// ----------
1515

1616
export class VError extends Error {
17-
private issues: Issue[];
17+
private readonly issues: Issue[];
1818

1919
constructor(errors: Issue[]) {
2020
const message = JSON.stringify(errors, null, 2);

config/core/helpers/str-helpers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
function _includes(inputs: string[], value: string | undefined): boolean {
2-
return value !== undefined && inputs.includes(value);
2+
return inputs?.includes(value);
33
}
44

55
function _isNumber(input: string | undefined): boolean {
66
return !Number.isNaN(Number(input));
77
}
88

99
function _isNotEmpty(input: string | undefined): boolean {
10-
return input !== undefined && '' !== input.trim();
10+
return '' !== input?.trim();
1111
}
1212

1313
function _isOptional(): boolean {
1414
return true;
1515
}
1616

1717
function _startsWith(input: string | undefined, prefix: string): boolean {
18-
return input !== undefined && input.startsWith(prefix);
18+
return input?.startsWith(prefix);
1919
}
2020

2121
// --------------------------------------------------------------------------------

config/core/helpers/url-helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function _isMultipleURL(input: string | undefined): boolean {
5656

5757
function _isPostgresURL(input: string | undefined): boolean {
5858
const regex =
59-
/^postgresql:\/\/([a-zA-Z0-9]+):([a-zA-Z0-9]+)@(localhost|[0-9.]+):([0-9]{1,5})\/([a-zA-Z0-9]+)(\?schema=)?([a-zA-Z0-9]+)/;
59+
/^postgresql:\/\/([a-zA-Z0-9]+):([a-zA-Z0-9]+)@(localhost|[0-9.]+):(\d{1,5})\/([a-zA-Z0-9]+)(\?schema=)?([a-zA-Z0-9]+)/;
6060

6161
return regex.test(input);
6262
}

config/core/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export type SafeParseReturnType<T> =
2828
| { success: false; data: null; error: VError };
2929

3030
class StringBuilder {
31-
#validators: Validator<string>[] = [];
31+
readonly #validators: Validator<string>[] = [];
3232

3333
enum(inputs: string[]): this {
3434
this.#validators.push(_enum(inputs));
@@ -147,11 +147,10 @@ export type InferShape<T extends SchemaShape> = {
147147
export class ObjectBuilder<T extends SchemaShape> {
148148
#errors: Issue[] = [];
149149

150-
constructor(private shape: T) {}
150+
constructor(private readonly shape: T) {}
151151

152152
safeParse(value: object): SafeParseReturnType<InferShape<T>> {
153153
this.#errors = Object.entries(this.shape).reduce((issues, [key, validator]) => {
154-
// const parsed = validator.safeParse(value[key]);
155154
const parsed = validator.safeParse((value as Record<string, string>)[key]);
156155

157156
if (!parsed.success) {

config/test/config.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
});

0 commit comments

Comments
 (0)