Comparing version 0.1.2 to 0.1.3
@@ -114,3 +114,3 @@ "use strict"; | ||
const _defBuilder = | ||
({ async }) => | ||
({ async, validationErrorHandler }) => | ||
(...args) => { | ||
@@ -126,2 +126,7 @@ const sigs = args.slice(0, -1); | ||
const message = `Expected ${(0, number_1.humanizeNaturalNumbers)(availableArgumentLengths)} arguments, but got ${args.length}`; | ||
if (validationErrorHandler) { | ||
validationErrorHandler(new TypeError(message)); | ||
$matchedMorphedArguments = args; | ||
return sigs[0]; | ||
} | ||
throw new TypeError(message); | ||
@@ -180,3 +185,10 @@ } | ||
.filter(({ message: m }) => m !== "ARG_LENGTH_NOT_MATCH"); | ||
if (errors.length === 1) throw new TypeError(errors[0].message); | ||
if (errors.length === 1) { | ||
if (validationErrorHandler) { | ||
validationErrorHandler(new TypeError(errors[0].message)); | ||
$matchedMorphedArguments = args; | ||
return errors[0].sig; | ||
} | ||
throw new TypeError(errors[0].message); | ||
} | ||
let message = "No overload "; | ||
@@ -196,2 +208,7 @@ if (fn.name) message += `of function '${fn.name}' `; | ||
message = message.trimEnd(); | ||
if (validationErrorHandler) { | ||
validationErrorHandler(new TypeError(message)); | ||
$matchedMorphedArguments = args; | ||
return errors[0].sig; | ||
} | ||
throw new TypeError(message); | ||
@@ -224,2 +241,6 @@ }; | ||
message = (0, string_1.capitalize)(message); | ||
if (validationErrorHandler) { | ||
validationErrorHandler(new TypeError(message)); | ||
return r; | ||
} | ||
throw new TypeError(message); | ||
@@ -249,2 +270,4 @@ }; | ||
unwrap: () => f, | ||
onValidationError: (handler) => | ||
_defBuilder({ async, validationErrorHandler: handler })(...args), | ||
matchArguments: (...args) => { | ||
@@ -251,0 +274,0 @@ try { |
@@ -100,3 +100,3 @@ import { type } from "arktype"; | ||
const _defBuilder = | ||
({ async }) => | ||
({ async, validationErrorHandler }) => | ||
(...args) => { | ||
@@ -112,2 +112,7 @@ const sigs = args.slice(0, -1); | ||
const message = `Expected ${humanizeNaturalNumbers(availableArgumentLengths)} arguments, but got ${args.length}`; | ||
if (validationErrorHandler) { | ||
validationErrorHandler(new TypeError(message)); | ||
$matchedMorphedArguments = args; | ||
return sigs[0]; | ||
} | ||
throw new TypeError(message); | ||
@@ -166,3 +171,10 @@ } | ||
.filter(({ message: m }) => m !== "ARG_LENGTH_NOT_MATCH"); | ||
if (errors.length === 1) throw new TypeError(errors[0].message); | ||
if (errors.length === 1) { | ||
if (validationErrorHandler) { | ||
validationErrorHandler(new TypeError(errors[0].message)); | ||
$matchedMorphedArguments = args; | ||
return errors[0].sig; | ||
} | ||
throw new TypeError(errors[0].message); | ||
} | ||
let message = "No overload "; | ||
@@ -182,2 +194,7 @@ if (fn.name) message += `of function '${fn.name}' `; | ||
message = message.trimEnd(); | ||
if (validationErrorHandler) { | ||
validationErrorHandler(new TypeError(message)); | ||
$matchedMorphedArguments = args; | ||
return errors[0].sig; | ||
} | ||
throw new TypeError(message); | ||
@@ -210,2 +227,6 @@ }; | ||
message = capitalize(message); | ||
if (validationErrorHandler) { | ||
validationErrorHandler(new TypeError(message)); | ||
return r; | ||
} | ||
throw new TypeError(message); | ||
@@ -235,2 +256,4 @@ }; | ||
unwrap: () => f, | ||
onValidationError: (handler) => | ||
_defBuilder({ async, validationErrorHandler: handler })(...args), | ||
matchArguments: (...args) => { | ||
@@ -237,0 +260,0 @@ try { |
{ | ||
"name": "safunc", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Create runtime-validated functions for both synchronous and asynchronous ones with ease, supporting optional parameters and overloaded signatures with smart type inference in TypeScript", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -349,2 +349,8 @@ <h1 align="center">Safunc</h1> | ||
// Use `onValidationError` to provide alternative error handling instead of just throwing a `TypeError` | ||
const range2 = range.onValidationError(console.error); | ||
range2(1, 3.5); // => [1, 2, 3] - The function is returned as the errors are handled by a custom handler | ||
// This time, the error message is printed to the console and no error is thrown | ||
// If you still want to throw an error instead of returning the function, you can rethrow it in the custom handler | ||
// Use `Safunc#matchArguments` to get the matched `Sig` for the given arguments | ||
@@ -351,0 +357,0 @@ range.matchArguments(3); // => sig1 |
@@ -236,2 +236,8 @@ import type { Eq, Fn } from "./tools/common"; | ||
/** | ||
* Provides an error handler for validation errors instead of throwing them. | ||
* @param handler The error handler. | ||
* @returns | ||
*/ | ||
onValidationError: (handler: (e: TypeError) => void) => Safunc<F>; | ||
/** | ||
* Get the matched `Sig` for given arguments. | ||
@@ -238,0 +244,0 @@ * @param args Arguments to match. |
2170590
3529
474