@aircall/exception-zod
Advanced tools
Comparing version 0.1.2 to 0.1.5
@@ -0,3 +1,6 @@ | ||
import { UserInputException } from '@aircall/exception'; | ||
declare const zodMiddleware: <T extends (...args: any) => Promise<any>>(fn: T) => (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>>>; | ||
declare const toZodException: (error: Error) => Error | UserInputException; | ||
export { zodMiddleware }; | ||
export { toZodException, zodMiddleware }; |
@@ -12,63 +12,68 @@ 'use strict'; | ||
} catch (error) { | ||
if (!(error instanceof zod.z.ZodError)) { | ||
throw error; | ||
} | ||
const inputException = new exception.UserInputException(); | ||
error.issues.forEach((issue) => { | ||
const path = issue.path.join("."); | ||
switch (issue.code) { | ||
case "invalid_type": | ||
inputException.addIssue( | ||
new exception.InvalidTypeException( | ||
{ path, received: issue.received, expected: issue.expected }, | ||
issue.message | ||
) | ||
); | ||
break; | ||
case "invalid_string": | ||
inputException.addIssue( | ||
new exception.InvalidStringException( | ||
{ | ||
path, | ||
validation: issue.validation | ||
}, | ||
issue.message | ||
) | ||
); | ||
break; | ||
case "invalid_enum_value": | ||
inputException.addIssue( | ||
new exception.InvalidEnumException( | ||
{ | ||
path, | ||
received: issue.received.toString(), | ||
options: issue.options.map((option) => option.toString()) | ||
}, | ||
issue.message | ||
) | ||
); | ||
break; | ||
case "too_small": | ||
inputException.addIssue( | ||
new exception.TooSmallException( | ||
{ path, type: issue.type, minimum: issue.minimum }, | ||
issue.message | ||
) | ||
); | ||
break; | ||
case "too_big": | ||
inputException.addIssue( | ||
new exception.TooBigException( | ||
{ path, type: issue.type, maximum: issue.maximum }, | ||
issue.message | ||
) | ||
); | ||
break; | ||
} | ||
}); | ||
throw inputException; | ||
const err = toZodException(error); | ||
throw err; | ||
} | ||
}; | ||
}; | ||
var toZodException = (error) => { | ||
if (!(error instanceof zod.z.ZodError)) { | ||
return error; | ||
} | ||
const inputException = new exception.UserInputException(); | ||
error.issues.forEach((issue) => { | ||
const path = issue.path.join("."); | ||
switch (issue.code) { | ||
case "invalid_type": | ||
inputException.addIssue( | ||
new exception.InvalidTypeException( | ||
{ path, received: issue.received, expected: issue.expected }, | ||
issue.message | ||
) | ||
); | ||
break; | ||
case "invalid_string": | ||
inputException.addIssue( | ||
new exception.InvalidStringException( | ||
{ | ||
path, | ||
validation: issue.validation | ||
}, | ||
issue.message | ||
) | ||
); | ||
break; | ||
case "invalid_enum_value": | ||
inputException.addIssue( | ||
new exception.InvalidEnumException( | ||
{ | ||
path, | ||
received: issue.received.toString(), | ||
options: issue.options.map((option) => option.toString()) | ||
}, | ||
issue.message | ||
) | ||
); | ||
break; | ||
case "too_small": | ||
inputException.addIssue( | ||
new exception.TooSmallException( | ||
{ path, type: issue.type, minimum: issue.minimum }, | ||
issue.message | ||
) | ||
); | ||
break; | ||
case "too_big": | ||
inputException.addIssue( | ||
new exception.TooBigException( | ||
{ path, type: issue.type, maximum: issue.maximum }, | ||
issue.message | ||
) | ||
); | ||
break; | ||
} | ||
}); | ||
return inputException; | ||
}; | ||
exports.toZodException = toZodException; | ||
exports.zodMiddleware = zodMiddleware; |
{ | ||
"name": "@aircall/exception-zod", | ||
"version": "0.1.2", | ||
"version": "0.1.5", | ||
"main": "dist/index.js", | ||
@@ -32,7 +32,7 @@ "module": "dist/index.mjs", | ||
"devDependencies": { | ||
"@aircall/exception": "0.1.1", | ||
"@aircall/exception-gql": "0.1.1", | ||
"@aircall/exception-rest": "0.1.1", | ||
"@aircall/test-sequencer": "1.0.0", | ||
"@aircall/tsconfig": "1.4.0", | ||
"@aircall/exception": "0.1.4", | ||
"@aircall/exception-gql": "0.2.3", | ||
"@aircall/exception-rest": "0.1.4", | ||
"@aircall/test-sequencer": "1.0.2", | ||
"@aircall/tsconfig": "1.4.2", | ||
"@size-limit/preset-small-lib": "8.1.0", | ||
@@ -51,7 +51,6 @@ "@types/jest": "29.5.2", | ||
"dev": "pnpm build --watch", | ||
"posttest": "pnpm run size", | ||
"size": "size-limit", | ||
"test:ci": "pnpm run test --testSequencer @aircall/test-sequencer/parallel-ci-sequencer.js --passWithNoTests", | ||
"test:ci": "pnpm run test --shard ${CI_NODE_INDEX}/${CI_NODE_TOTAL} --passWithNoTests --silent", | ||
"test": "jest --passWithNoTests" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
12264
152