one
Version:
One is a new React Framework that makes Vite serve both native and web.
66 lines (65 loc) • 2.04 kB
JavaScript
class ParamValidationError extends Error {
routerCode = "VALIDATE_PARAMS";
issues;
params;
constructor(message, params, issues = []) {
super(message);
this.name = "ParamValidationError";
this.params = params;
this.issues = issues;
}
}
class RouteValidationError extends Error {
routerCode = "VALIDATE_ROUTE";
details;
constructor(message, details) {
super(message);
this.name = "RouteValidationError";
this.details = details;
}
}
function isStandardSchema(value) {
return typeof value === "object" && value !== null && "~standard" in value && typeof value["~standard"]?.validate === "function";
}
function isZodLikeSchema(value) {
return typeof value === "object" && value !== null && typeof value.parse === "function";
}
function isValidatorFn(value) {
return typeof value === "function";
}
function validateParams(validator, input) {
if (isStandardSchema(validator)) {
const result = validator["~standard"].validate(input);
if (result.issues) {
throw new ParamValidationError("Route param validation failed", input, result.issues);
}
return result.value;
}
if (isZodLikeSchema(validator)) {
if (validator.safeParse) {
const result = validator.safeParse(input);
if (!result.success) {
throw new ParamValidationError("Route param validation failed", input, [result.error]);
}
return result.data;
}
try {
return validator.parse(input);
} catch (error) {
throw new ParamValidationError("Route param validation failed", input, [error]);
}
}
if (isValidatorFn(validator)) {
try {
return validator(input);
} catch (error) {
throw new ParamValidationError("Route param validation failed", input, [error]);
}
}
throw new Error("Invalid validator provided to validateParams");
}
function zodParamValidator(schema) {
return schema;
}
export { ParamValidationError, RouteValidationError, validateParams, zodParamValidator };
//# sourceMappingURL=validateParams.mjs.map