UNPKG

next-sanity

Version:
37 lines (36 loc) 1.36 kB
import { validateApiPerspective } from "@sanity/client"; function sanitizePerspective(_perspective, fallback) { const perspective = typeof _perspective === "string" && _perspective.includes(",") ? _perspective.split(",") : _perspective; try { validateApiPerspective(perspective); return perspective === "raw" ? fallback : perspective; } catch (err) { console.warn(`Invalid perspective:`, _perspective, perspective, err); return fallback; } } /** * Editing variants are referenced by their bare variant id (e.g. `Ab12cd34`), * never the full variant document id (`_.variants.Ab12cd34`, rejected since * dots are not allowed). A single id, never comma-separated or an array. */ const VARIANT_ID_PATTERN = /^[A-Za-z0-9_-]+$/; /** * Unlike `sanitizePerspective` there is no fallback: an invalid or missing * variant means "no variant selected" and resolves to `undefined`. */ function sanitizeVariant(_variant) { if (typeof _variant !== "string") { if (typeof _variant !== "undefined" && _variant !== null) console.warn(`Invalid variant:`, _variant); return; } const variant = _variant.trim(); if (variant === "") return; if (!VARIANT_ID_PATTERN.test(variant)) { console.warn(`Invalid variant:`, _variant); return; } return variant; } export { sanitizePerspective as n, sanitizeVariant as t }; //# sourceMappingURL=sanitizeVariant.js.map