Skip to content

Commit

Permalink
chore: clean up zod type
Browse files Browse the repository at this point in the history
  • Loading branch information
Utsab Chowdhury authored and Utsab Chowdhury committed Feb 5, 2024
1 parent 93947db commit d2e65f4
Showing 1 changed file with 39 additions and 73 deletions.
112 changes: 39 additions & 73 deletions src/types/zodTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ export const ProxyV1RequestSchema = z.object({
destinationConfig: z.record(z.unknown()),
});

const validateStatTags = (data: any) => {
if (!isHttpStatusSuccess(data.status)) {
return isDefinedAndNotNullAndNotEmpty(data.statTags);
}
return true;
};

const validateAuthErrorCategory = (data: any) => {
if (!isHttpStatusSuccess(data.status)) {
return isDefinedAndNotNullAndNotEmpty(data.authErrorCategory);
}
return true;

Check warning on line 70 in src/types/zodTypes.ts

View check run for this annotation

Codecov / codecov/patch

src/types/zodTypes.ts#L70

Added line #L70 was not covered by tests
};

export const DeliveryV0ResponseSchema = z
.object({
status: z.number(),
Expand All @@ -64,19 +78,11 @@ export const DeliveryV0ResponseSchema = z
statTags: z.record(z.unknown()).optional(),
authErrorCategory: z.string().optional(),
})
.refine(
(data) => {
if (!isHttpStatusSuccess(data.status)) {
return isDefinedAndNotNullAndNotEmpty(data.statTags);
}
return true;
},
{
// eslint-disable-next-line sonarjs/no-duplicate-string
message: "statTags can't be empty when status is not a 2XX",
path: ['statTags'], // Pointing out which field is invalid
},
);
.refine(validateStatTags, {
// eslint-disable-next-line sonarjs/no-duplicate-string
message: "statTags can't be empty when status is not a 2XX",
path: ['statTags'], // Pointing out which field is invalid
});

export const DeliveryV0ResponseSchemaForOauth = z
.object({
Expand All @@ -86,30 +92,14 @@ export const DeliveryV0ResponseSchemaForOauth = z
statTags: z.record(z.unknown()).optional(),
authErrorCategory: z.string().optional(),
})
.refine(
(data) => {
if (!isHttpStatusSuccess(data.status)) {
return isDefinedAndNotNullAndNotEmpty(data.statTags);
}
return true;
},
{
message: "statTags can't be empty when status is not a 2XX",
path: ['statTags'], // Pointing out which field is invalid
},
)
.refine(
(data) => {
if (!isHttpStatusSuccess(data.status)) {
return isDefinedAndNotNullAndNotEmpty(data.authErrorCategory);
}
return true;
},
{
message: "authErrorCategory can't be empty when status is not a 2XX",
path: ['authErrorCategory'], // Pointing out which field is invalid
},
);
.refine(validateStatTags, {
message: "statTags can't be empty when status is not a 2XX",
path: ['statTags'], // Pointing out which field is invalid
})
.refine(validateAuthErrorCategory, {
message: "authErrorCategory can't be empty when status is not a 2XX",
path: ['authErrorCategory'], // Pointing out which field is invalid
});

const DeliveryJobStateSchema = z.object({
error: z.string(),
Expand All @@ -125,18 +115,10 @@ export const DeliveryV1ResponseSchema = z
authErrorCategory: z.string().optional(),
response: z.array(DeliveryJobStateSchema),
})
.refine(
(data) => {
if (!isHttpStatusSuccess(data.status)) {
return isDefinedAndNotNullAndNotEmpty(data.statTags);
}
return true;
},
{
message: "statTags can't be empty when status is not a 2XX",
path: ['statTags'], // Pointing out which field is invalid
},
);
.refine(validateStatTags, {
message: "statTags can't be empty when status is not a 2XX",
path: ['statTags'], // Pointing out which field is invalid
});

export const DeliveryV1ResponseSchemaForOauth = z
.object({
Expand All @@ -146,27 +128,11 @@ export const DeliveryV1ResponseSchemaForOauth = z
authErrorCategory: z.string().optional(),
response: z.array(DeliveryJobStateSchema),
})
.refine(
(data) => {
if (!isHttpStatusSuccess(data.status)) {
return isDefinedAndNotNullAndNotEmpty(data.statTags);
}
return true;
},
{
message: "statTags can't be empty when status is not a 2XX",
path: ['statTags'], // Pointing out which field is invalid
},
)
.refine(
(data) => {
if (!isHttpStatusSuccess(data.status)) {
return isDefinedAndNotNullAndNotEmpty(data.authErrorCategory);
}
return true;
},
{
message: "authErrorCategory can't be empty when status is not a 2XX",
path: ['authErrorCategory'], // Pointing out which field is invalid
},
);
.refine(validateStatTags, {
message: "statTags can't be empty when status is not a 2XX",
path: ['statTags'], // Pointing out which field is invalid
})
.refine(validateAuthErrorCategory, {
message: "authErrorCategory can't be empty when status is not a 2XX",
path: ['authErrorCategory'], // Pointing out which field is invalid
});

0 comments on commit d2e65f4

Please sign in to comment.