Pretty format webhook payload example + unify expected body validation (#13034)
## Webhook Expected Body is automatically pretty formatted https://github.com/user-attachments/assets/0ca7d621-0c6e-4bef-903f-859efd02f9cc ## Expected body fields can't contain spaces in keys' name https://github.com/user-attachments/assets/b68d36a6-acd4-4ba2-a99a-5857e30c8582 Closes https://github.com/twentyhq/core-team-issues/issues/1117
This commit is contained in:
committed by
GitHub
parent
92576aec0f
commit
43c3d114bb
+36
@@ -0,0 +1,36 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const schema = z
|
||||
.record(z.any())
|
||||
.refine((data) => Object.keys(data).every((key) => !key.match(/\s/)), {
|
||||
message: 'JSON keys cannot contain spaces',
|
||||
});
|
||||
|
||||
export const parseAndValidateVariableFriendlyStringifiedJson = (
|
||||
expectedJson: string,
|
||||
) => {
|
||||
let value: unknown;
|
||||
|
||||
try {
|
||||
value = JSON.parse(expectedJson);
|
||||
} catch (error) {
|
||||
return {
|
||||
isValid: false,
|
||||
error: String(error),
|
||||
} as const;
|
||||
}
|
||||
|
||||
const parsingResult = schema.safeParse(value);
|
||||
|
||||
if (parsingResult.success) {
|
||||
return {
|
||||
isValid: true,
|
||||
data: parsingResult.data,
|
||||
} as const;
|
||||
}
|
||||
|
||||
return {
|
||||
isValid: false,
|
||||
error: parsingResult.error.issues[0].message,
|
||||
} as const;
|
||||
};
|
||||
Reference in New Issue
Block a user