fix: make schema more lenient while parsing public data structures

This commit is contained in:
Andrew Bastin
2023-11-14 21:24:25 +05:30
parent e93a37c711
commit b1982d74a6
6 changed files with 163 additions and 84 deletions

View File

@@ -43,7 +43,7 @@
"io-ts": "^2.2.20",
"lodash": "^4.17.21",
"parser-ts": "^0.7.0",
"verzod": "^0.1.1",
"verzod": "^0.2.2",
"zod": "^3.22.4"
}
}

View File

@@ -2,9 +2,9 @@ import { z } from "zod"
import { defineVersion } from "verzod"
export const GQLHeader = z.object({
key: z.string(),
value: z.string(),
active: z.boolean()
key: z.string().catch(""),
value: z.string().catch(""),
active: z.boolean().catch(true)
})
export type GQLHeader = z.infer<typeof GQLHeader>
@@ -13,7 +13,7 @@ export const V1_SCHEMA = z.object({
v: z.literal(1),
name: z.string(),
url: z.string(),
headers: z.array(GQLHeader),
headers: z.array(GQLHeader).catch([]),
query: z.string(),
variables: z.string(),
})

View File

@@ -11,8 +11,8 @@ export type HoppGQLAuthNone = z.infer<typeof HoppGQLAuthNone>
export const HoppGQLAuthBasic = z.object({
authType: z.literal("basic"),
username: z.string(),
password: z.string()
username: z.string().catch(""),
password: z.string().catch("")
})
export type HoppGQLAuthBasic = z.infer<typeof HoppGQLAuthBasic>
@@ -20,7 +20,7 @@ export type HoppGQLAuthBasic = z.infer<typeof HoppGQLAuthBasic>
export const HoppGQLAuthBearer = z.object({
authType: z.literal("bearer"),
token: z.string()
token: z.string().catch("")
})
export type HoppGQLAuthBearer = z.infer<typeof HoppGQLAuthBearer>
@@ -28,12 +28,12 @@ export type HoppGQLAuthBearer = z.infer<typeof HoppGQLAuthBearer>
export const HoppGQLAuthOAuth2 = z.object({
authType: z.literal("oauth-2"),
token: z.string(),
oidcDiscoveryURL: z.string(),
authURL: z.string(),
accessTokenURL: z.string(),
clientID: z.string(),
scope: z.string()
token: z.string().catch(""),
oidcDiscoveryURL: z.string().catch(""),
authURL: z.string().catch(""),
accessTokenURL: z.string().catch(""),
clientID: z.string().catch(""),
scope: z.string().catch("")
})
export type HoppGQLAuthOAuth2 = z.infer<typeof HoppGQLAuthOAuth2>
@@ -41,9 +41,9 @@ export type HoppGQLAuthOAuth2 = z.infer<typeof HoppGQLAuthOAuth2>
export const HoppGQLAuthAPIKey = z.object({
authType: z.literal("api-key"),
key: z.string(),
value: z.string(),
addTo: z.string()
key: z.string().catch(""),
value: z.string().catch(""),
addTo: z.string().catch("Headers")
})
export type HoppGQLAuthAPIKey = z.infer<typeof HoppGQLAuthAPIKey>
@@ -68,7 +68,7 @@ const V2_SCHEMA = z.object({
name: z.string(),
url: z.string(),
headers: z.array(GQLHeader),
headers: z.array(GQLHeader).catch([]),
query: z.string(),
variables: z.string(),

View File

@@ -52,6 +52,7 @@ export const HoppRESTRequest = createVersionedEntity({
export type HoppRESTRequest = InferredEntity<typeof HoppRESTRequest>
// TODO: Handle the issue with the preRequestScript and testScript type check failures on pre-commit
const HoppRESTRequestEq = Eq.struct<HoppRESTRequest>({
id: undefinedEq(S.Eq),
v: S.Eq,
@@ -59,11 +60,11 @@ const HoppRESTRequestEq = Eq.struct<HoppRESTRequest>({
body: lodashIsEqualEq,
endpoint: S.Eq,
headers: mapThenEq(
(arr) => arr.filter((h) => h.key !== "" && h.value !== ""),
(arr) => arr.filter((h: any) => h.key !== "" && h.value !== ""),
lodashIsEqualEq
),
params: mapThenEq(
(arr) => arr.filter((p) => p.key !== "" && p.value !== ""),
(arr) => arr.filter((p: any) => p.key !== "" && p.value !== ""),
lodashIsEqualEq
),
method: S.Eq,

View File

@@ -10,7 +10,7 @@ export const FormDataKeyValue = z.object({
z.union([
z.object({
isFile: z.literal(true),
value: z.array(z.instanceof(Blob))
value: z.array(z.instanceof(Blob).nullable())
}),
z.object({
isFile: z.literal(false),
@@ -31,11 +31,11 @@ export type HoppRESTReqBodyFormData = z.infer<typeof HoppRESTReqBodyFormData>
export const HoppRESTReqBody = z.union([
z.object({
contentType: z.literal(null),
body: z.literal(null)
body: z.literal(null).catch(null)
}),
z.object({
contentType: z.literal("multipart/form-data"),
body: FormDataKeyValue
body: z.array(FormDataKeyValue).catch([])
}),
z.object({
contentType: z.union([
@@ -48,7 +48,7 @@ export const HoppRESTReqBody = z.union([
z.literal("text/html"),
z.literal("text/plain"),
]),
body: z.string()
body: z.string().catch("")
})
])
@@ -62,36 +62,36 @@ export type HoppRESTAuthNone = z.infer<typeof HoppRESTAuthNone>
export const HoppRESTAuthBasic = z.object({
authType: z.literal("basic"),
username: z.string(),
password: z.string(),
username: z.string().catch(""),
password: z.string().catch(""),
})
export type HoppRESTAuthBasic = z.infer<typeof HoppRESTAuthBasic>
export const HoppRESTAuthBearer = z.object({
authType: z.literal("bearer"),
token: z.string(),
token: z.string().catch(""),
})
export type HoppRESTAuthBearer = z.infer<typeof HoppRESTAuthBearer>
export const HoppRESTAuthOAuth2 = z.object({
authType: z.literal("oauth-2"),
token: z.string(),
oidcDiscoveryURL: z.string(),
authURL: z.string(),
accessTokenURL: z.string(),
clientID: z.string(),
scope: z.string(),
token: z.string().catch(""),
oidcDiscoveryURL: z.string().catch(""),
authURL: z.string().catch(""),
accessTokenURL: z.string().catch(""),
clientID: z.string().catch(""),
scope: z.string().catch(""),
})
export type HoppRESTAuthOAuth2 = z.infer<typeof HoppRESTAuthOAuth2>
export const HoppRESTAuthAPIKey = z.object({
authType: z.literal("api-key"),
key: z.string(),
value: z.string(),
addTo: z.string(),
key: z.string().catch(""),
value: z.string().catch(""),
addTo: z.string().catch("Headers"),
})
export type HoppRESTAuthAPIKey = z.infer<typeof HoppRESTAuthAPIKey>
@@ -112,9 +112,9 @@ export type HoppRESTAuth = z.infer<typeof HoppRESTAuth>
export const HoppRESTParams = z.array(
z.object({
key: z.string(),
value: z.string(),
active: z.boolean()
key: z.string().catch(""),
value: z.string().catch(""),
active: z.boolean().catch(true)
})
)
@@ -122,9 +122,9 @@ export type HoppRESTParams = z.infer<typeof HoppRESTParams>
export const HoppRESTHeaders = z.array(
z.object({
key: z.string(),
value: z.string(),
active: z.boolean()
key: z.string().catch(""),
value: z.string().catch(""),
active: z.boolean().catch(true)
})
)
@@ -139,8 +139,8 @@ const V1_SCHEMA = z.object({
endpoint: z.string(),
params: HoppRESTParams,
headers: HoppRESTHeaders,
preRequestScript: z.string(),
testScript: z.string(),
preRequestScript: z.string().catch(""),
testScript: z.string().catch(""),
auth: HoppRESTAuth,