-
Notifications
You must be signed in to change notification settings - Fork 156
feat(parser): implement safeParse
option
#2244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
b8af398
first draft on safeParse with major refactoring
am29d f1ea49e
add safeParse
am29d d108dcf
fixed sns tests
am29d bdb46cc
bump coverage
am29d 1ce5edb
remove throw error and return ParsedResult
am29d 9fca18a
remove one level to reduce complexity score
am29d 2b9a555
make static methods readonly
am29d d9b6de5
simplified cryptic ternary operation into something readble
am29d 266543c
Update packages/parser/src/parserDecorator.ts
am29d 96bd1b1
merged
am29d 06c1903
simplify export
am29d 7b4833a
add invisible character for decorator rendering
am29d 584800b
fix docs and tests
am29d c77d7a7
Update packages/parser/src/parserDecorator.ts
am29d c64a15d
add comment with description
am29d d0b2c5a
remove context
am29d 30d33cb
remove unintentional safeParse export
am29d a37f647
add examples to parse standalone function
am29d File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,40 @@ | ||
import { parse } from './envelope.js'; | ||
import { z, ZodSchema } from 'zod'; | ||
import { Envelope } from './envelope.js'; | ||
import { z, type ZodSchema } from 'zod'; | ||
import { APIGatewayProxyEventSchema } from '../schemas/apigw.js'; | ||
import type { ParsedResult } from '../types/parser.js'; | ||
|
||
/** | ||
* API Gateway envelope to extract data within body key | ||
*/ | ||
export const apiGatewayEnvelope = <T extends ZodSchema>( | ||
data: unknown, | ||
schema: T | ||
): z.infer<T> => { | ||
const parsedEnvelope = APIGatewayProxyEventSchema.parse(data); | ||
if (!parsedEnvelope.body) { | ||
throw new Error('Body field of API Gateway event is undefined'); | ||
export class ApiGatewayEnvelope extends Envelope { | ||
public static parse<T extends ZodSchema>( | ||
data: unknown, | ||
schema: T | ||
): z.infer<T> { | ||
return super.parse(APIGatewayProxyEventSchema.parse(data).body, schema); | ||
} | ||
|
||
return parse(parsedEnvelope.body, schema); | ||
}; | ||
public static safeParse<T extends ZodSchema>( | ||
data: unknown, | ||
schema: T | ||
): ParsedResult<unknown, z.infer<T>> { | ||
const parsedEnvelope = APIGatewayProxyEventSchema.safeParse(data); | ||
if (!parsedEnvelope.success) { | ||
return { | ||
...parsedEnvelope, | ||
originalEvent: data, | ||
}; | ||
} | ||
|
||
const parsedBody = super.safeParse(parsedEnvelope.data.body, schema); | ||
|
||
if (!parsedBody.success) { | ||
return { | ||
...parsedBody, | ||
originalEvent: data, | ||
}; | ||
} | ||
|
||
return parsedBody; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,40 @@ | ||
import { parse } from './envelope.js'; | ||
import { z, ZodSchema } from 'zod'; | ||
import { z, type ZodSchema } from 'zod'; | ||
import { APIGatewayProxyEventV2Schema } from '../schemas/apigwv2.js'; | ||
import { Envelope } from './envelope.js'; | ||
import type { ParsedResult } from '../types/index.js'; | ||
|
||
/** | ||
* API Gateway V2 envelope to extract data within body key | ||
*/ | ||
export const apiGatewayV2Envelope = <T extends ZodSchema>( | ||
data: unknown, | ||
schema: T | ||
): z.infer<T> => { | ||
const parsedEnvelope = APIGatewayProxyEventV2Schema.parse(data); | ||
if (!parsedEnvelope.body) { | ||
throw new Error('Body field of API Gateway event is undefined'); | ||
export class ApiGatewayV2Envelope extends Envelope { | ||
public static parse<T extends ZodSchema>( | ||
data: unknown, | ||
schema: T | ||
): z.infer<T> { | ||
return super.parse(APIGatewayProxyEventV2Schema.parse(data).body, schema); | ||
} | ||
|
||
return parse(parsedEnvelope.body, schema); | ||
}; | ||
public static safeParse<T extends ZodSchema>( | ||
data: unknown, | ||
schema: T | ||
): ParsedResult { | ||
const parsedEnvelope = APIGatewayProxyEventV2Schema.safeParse(data); | ||
if (!parsedEnvelope.success) { | ||
return { | ||
...parsedEnvelope, | ||
originalEvent: data, | ||
}; | ||
} | ||
|
||
const parsedBody = super.safeParse(parsedEnvelope.data.body, schema); | ||
|
||
if (!parsedBody.success) { | ||
return { | ||
...parsedBody, | ||
originalEvent: data, | ||
}; | ||
} | ||
|
||
return parsedBody; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,70 @@ | ||
import { z, ZodSchema } from 'zod'; | ||
import { z, type ZodSchema } from 'zod'; | ||
import type { ParsedResult } from '../types/parser.js'; | ||
|
||
/** | ||
* Abstract function to parse the content of the envelope using provided schema. | ||
* Both inputs are provided as unknown by the user. | ||
* We expect the data to be either string that can be parsed to json or object. | ||
* @internal | ||
* @param data data to parse | ||
* @param schema schema | ||
*/ | ||
export const parse = <T extends ZodSchema>( | ||
data: unknown, | ||
schema: T | ||
): z.infer<T>[] => { | ||
if (typeof data === 'string') { | ||
return schema.parse(JSON.parse(data)); | ||
} else if (typeof data === 'object') { | ||
return schema.parse(data); | ||
} else | ||
throw new Error( | ||
`Invalid data type for envelope. Expected string or object, got ${typeof data}` | ||
); | ||
}; | ||
export class Envelope { | ||
/** | ||
* Abstract function to parse the content of the envelope using provided schema. | ||
* Both inputs are provided as unknown by the user. | ||
* We expect the data to be either string that can be parsed to json or object. | ||
* @internal | ||
* @param data data to parse | ||
* @param schema schema | ||
*/ | ||
public static readonly parse = <T extends ZodSchema>( | ||
data: unknown, | ||
schema: T | ||
): z.infer<T> => { | ||
if (typeof data === 'string') { | ||
return schema.parse(JSON.parse(data)); | ||
} else if (typeof data === 'object') { | ||
return schema.parse(data); | ||
} else | ||
throw new Error( | ||
`Invalid data type for envelope. Expected string or object, got ${typeof data}` | ||
); | ||
}; | ||
|
||
/** | ||
* Abstract function to safely parse the content of the envelope using provided schema. | ||
* safeParse is used to avoid throwing errors, thus we catuch all errors and wrap them in the result. | ||
* @param input | ||
* @param schema | ||
*/ | ||
public static readonly safeParse = <T extends ZodSchema>( | ||
input: unknown, | ||
schema: T | ||
): ParsedResult<unknown, z.infer<T>> => { | ||
try { | ||
if (typeof input !== 'object' && typeof input !== 'string') { | ||
return { | ||
success: false, | ||
error: new Error( | ||
`Invalid data type for envelope. Expected string or object, got ${typeof input}` | ||
), | ||
originalEvent: input, | ||
}; | ||
} | ||
|
||
const parsed = schema.safeParse( | ||
typeof input === 'string' ? JSON.parse(input) : input | ||
); | ||
|
||
return parsed.success | ||
? { | ||
success: true, | ||
data: parsed.data, | ||
} | ||
: { | ||
success: false, | ||
error: parsed.error, | ||
originalEvent: input, | ||
}; | ||
} catch (e) { | ||
return { | ||
success: false, | ||
error: e as Error, | ||
originalEvent: input, | ||
}; | ||
} | ||
}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,41 @@ | ||
import { parse } from './envelope.js'; | ||
import { z, ZodSchema } from 'zod'; | ||
import { EventBridgeSchema } from '../schemas/eventbridge.js'; | ||
import { Envelope } from './envelope.js'; | ||
import { z, type ZodSchema } from 'zod'; | ||
import { EventBridgeSchema } from '../schemas/index.js'; | ||
import type { ParsedResult } from '../types/index.js'; | ||
|
||
/** | ||
* Envelope for EventBridge schema that extracts and parses data from the `detail` key. | ||
*/ | ||
export const eventBridgeEnvelope = <T extends ZodSchema>( | ||
data: unknown, | ||
schema: T | ||
): z.infer<T> => { | ||
return parse(EventBridgeSchema.parse(data).detail, schema); | ||
}; | ||
export class EventBridgeEnvelope extends Envelope { | ||
public static parse<T extends ZodSchema>( | ||
data: unknown, | ||
schema: T | ||
): z.infer<T> { | ||
return super.parse(EventBridgeSchema.parse(data).detail, schema); | ||
} | ||
|
||
public static safeParse<T extends ZodSchema>( | ||
data: unknown, | ||
schema: T | ||
): ParsedResult { | ||
const parsedEnvelope = EventBridgeSchema.safeParse(data); | ||
|
||
if (!parsedEnvelope.success) { | ||
return { | ||
...parsedEnvelope, | ||
originalEvent: data, | ||
}; | ||
} | ||
|
||
const parsedDetail = super.safeParse(parsedEnvelope.data.detail, schema); | ||
|
||
if (!parsedDetail.success) { | ||
return { | ||
...parsedDetail, | ||
originalEvent: data, | ||
}; | ||
} | ||
|
||
return parsedDetail; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.