Skip to content

Commit f5a22de

Browse files
Merge pull request #1 from chuganzy/type-guard
Add a type guard to Validator
2 parents 18dab0a + c9d5585 commit f5a22de

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+49
-49
lines changed

index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export class AppStoreServerAPIClient {
130130
if (!validator.validate(responseBody)) {
131131
throw new Error("Unexpected response body format")
132132
}
133-
return responseBody as T
133+
return responseBody
134134
});
135135
} else {
136136
return r.json().then(responseBody => {

jwt_verification.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export class SignedDataVerifier {
145145
throw new VerificationException(VerificationStatus.INVALID_CERTIFICATE)
146146
}
147147
try {
148-
const decodedJWT = jsonwebtoken.decode(jwt) as T
148+
const decodedJWT = jsonwebtoken.decode(jwt)
149149
if (!validator.validate(decodedJWT)) {
150150
throw new VerificationException(VerificationStatus.FAILURE)
151151
}

models/AccountTenure.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export enum AccountTenure {
1919
}
2020

2121
export class AccountTenureValidator implements Validator<AccountTenure> {
22-
validate(obj: any): boolean {
22+
validate(obj: any): obj is AccountTenure {
2323
return Object.values(AccountTenure).includes(obj)
2424
}
2525
}

models/AppTransaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export interface AppTransaction {
9090

9191
export class AppTransactionValidator implements Validator<AppTransaction> {
9292
static readonly environmentValidator = new EnvironmentValidator()
93-
validate(obj: any): boolean {
93+
validate(obj: any): obj is AppTransaction {
9494
if ((typeof obj['appAppleId'] !== 'undefined') && !(typeof obj['appAppleId'] === "number")) {
9595
return false
9696
}

models/AutoRenewStatus.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export enum AutoRenewStatus {
1313
}
1414

1515
export class AutoRenewStatusValidator implements Validator<AutoRenewStatus> {
16-
validate(obj: any): boolean {
16+
validate(obj: any): obj is AutoRenewStatus {
1717
return Object.values(AutoRenewStatus).includes(obj)
1818
}
1919
}

models/CheckTestNotificationResponse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface CheckTestNotificationResponse {
2727

2828
export class CheckTestNotificationResponseValidator implements Validator<CheckTestNotificationResponse> {
2929
static readonly sendAttemptItemValidator = new SendAttemptItemValidator()
30-
validate(obj: any): boolean {
30+
validate(obj: any): obj is CheckTestNotificationResponse {
3131
if ((typeof obj['signedPayload'] !== 'undefined') && !(typeof obj['signedPayload'] === "string" || obj['signedPayload'] instanceof String)) {
3232
return false
3333
}

models/ConsumptionStatus.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export enum ConsumptionStatus {
1515
}
1616

1717
export class ConsumptionStatusValidator implements Validator<ConsumptionStatus> {
18-
validate(obj: any): boolean {
18+
validate(obj: any): obj is ConsumptionStatus {
1919
return Object.values(ConsumptionStatus).includes(obj)
2020
}
2121
}

models/Data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export interface Data {
5656

5757
export class DataValidator implements Validator<Data> {
5858
static readonly environmentValidator = new EnvironmentValidator()
59-
validate(obj: any): boolean {
59+
validate(obj: any): obj is Data {
6060
if ((typeof obj['environment'] !== 'undefined') && !(DataValidator.environmentValidator.validate(obj['environment']))) {
6161
return false
6262
}

models/DeliveryStatus.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export enum DeliveryStatus {
1717
}
1818

1919
export class DeliveryStatusValidator implements Validator<DeliveryStatus> {
20-
validate(obj: any): boolean {
20+
validate(obj: any): obj is DeliveryStatus {
2121
return Object.values(DeliveryStatus).includes(obj)
2222
}
2323
}

models/Environment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export enum Environment {
1313
}
1414

1515
export class EnvironmentValidator implements Validator<Environment> {
16-
validate(obj: any): boolean {
16+
validate(obj: any): obj is Environment {
1717
return Object.values(Environment).includes(obj)
1818
}
1919
}

0 commit comments

Comments
 (0)