one
Version:
One is a new React Framework that makes Vite serve both native and web.
100 lines (99 loc) • 3.44 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 validateSearch_exports = {};
__export(validateSearch_exports, {
SearchValidationError: () => SearchValidationError,
parseSearchString: () => parseSearchString,
validateSearch: () => validateSearch,
withFallback: () => withFallback,
zodSearchValidator: () => zodSearchValidator
});
module.exports = __toCommonJS(validateSearch_exports);
class SearchValidationError extends Error {
routerCode = "VALIDATE_SEARCH";
issues;
constructor(message, issues = []) {
super(message), this.name = "SearchValidationError", this.issues = issues;
}
}
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 parseSearchString(search) {
const params = {};
return new URLSearchParams(search).forEach((value, key) => {
const existing = params[key];
existing === void 0 ? params[key] = value : Array.isArray(existing) ? existing.push(value) : params[key] = [existing, value];
}), params;
}
function validateSearch(validator, input) {
if (isStandardSchema(validator)) {
const result = validator["~standard"].validate(input);
if (result.issues)
throw new SearchValidationError(
"Search param validation failed",
result.issues
);
return result.value;
}
if (isZodLikeSchema(validator)) {
if (validator.safeParse) {
const result = validator.safeParse(input);
if (!result.success)
throw new SearchValidationError("Search param validation failed", [result.error]);
return result.data;
}
try {
return validator.parse(input);
} catch (error) {
throw new SearchValidationError("Search param validation failed", [error]);
}
}
if (isValidatorFn(validator))
try {
return validator(input);
} catch (error) {
throw new SearchValidationError("Search param validation failed", [error]);
}
throw new Error("Invalid validator provided to validateSearch");
}
function zodSearchValidator(schema) {
return schema;
}
function withFallback(schema, fallbackValue) {
return {
parse(value) {
try {
return schema.parse(value);
} catch {
return fallbackValue;
}
},
safeParse(value) {
try {
return { success: !0, data: schema.parse(value) };
} catch {
return { success: !0, data: fallbackValue };
}
}
};
}
//# sourceMappingURL=validateSearch.js.map