one
Version:
One is a new React Framework that makes Vite serve both native and web.
85 lines (84 loc) • 3.02 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: !0 });
}, __copyProps = (to, from, except, desc) => {
if (from && typeof from == "object" || typeof from == "function")
for (let key of __getOwnPropNames(from))
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
var validateParams_exports = {};
__export(validateParams_exports, {
ParamValidationError: () => ParamValidationError,
RouteValidationError: () => RouteValidationError,
validateParams: () => validateParams,
zodParamValidator: () => zodParamValidator
});
module.exports = __toCommonJS(validateParams_exports);
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;
}
//# sourceMappingURL=validateParams.js.map